mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Add won/lost to the games list
This commit is contained in:
parent
5bc43465ff
commit
e63b7b9069
@ -7,4 +7,5 @@
|
|||||||
|
|
||||||
.your-turn {
|
.your-turn {
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
|
background-color: $your-turn-background-color;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,6 +18,8 @@ $white-square-color: #bbb;
|
|||||||
$selected-square-color: #0cf;
|
$selected-square-color: #0cf;
|
||||||
$available-square-color: #6f0;
|
$available-square-color: #6f0;
|
||||||
|
|
||||||
|
$your-turn-background-color: rgba($white, 0.1);
|
||||||
|
|
||||||
$game-state-background-color: darken($black-square-color, 10%);
|
$game-state-background-color: darken($black-square-color, 10%);
|
||||||
|
|
||||||
$square-outline-color: darken($black-square-color, 20%);
|
$square-outline-color: darken($black-square-color, 20%);
|
||||||
|
|||||||
@ -69,6 +69,11 @@ defmodule Chess.Store.Game do
|
|||||||
end
|
end
|
||||||
def validate_king_in_check(changeset, _, _), do: changeset
|
def validate_king_in_check(changeset, _, _), do: changeset
|
||||||
|
|
||||||
|
def game_over?(game) do
|
||||||
|
game.state == "checkmate" ||
|
||||||
|
game.state == "stalemate"
|
||||||
|
end
|
||||||
|
|
||||||
def ordered(query) do
|
def ordered(query) do
|
||||||
query
|
query
|
||||||
|> order_by([game], desc: game.inserted_at)
|
|> order_by([game], desc: game.inserted_at)
|
||||||
|
|||||||
@ -14,10 +14,10 @@
|
|||||||
class: "btn btn-default btn-xs" %>
|
class: "btn btn-default btn-xs" %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<%= if your_turn?(@conn, game), do: gettext("Your turn") %>
|
<%= state(@conn, game) %>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
Started on <%= Timex.format!(game.inserted_at, "%b %e at %I:%M %P", :strftime) %>
|
<%= won_lost(@conn, game) %>
|
||||||
</td>
|
</td>
|
||||||
<td class="text-right">
|
<td class="text-right">
|
||||||
<%= link gettext("Delete"),
|
<%= link gettext("Delete"),
|
||||||
|
|||||||
@ -1,10 +1,32 @@
|
|||||||
defmodule ChessWeb.GameView do
|
defmodule ChessWeb.GameView do
|
||||||
use ChessWeb, :view
|
use ChessWeb, :view
|
||||||
|
|
||||||
|
alias Chess.Store.Game
|
||||||
|
|
||||||
import Chess.Auth, only: [current_user: 1]
|
import Chess.Auth, only: [current_user: 1]
|
||||||
|
|
||||||
|
def won_lost(conn, game) do
|
||||||
|
if game_over?(game) && game.state == "checkmate" do
|
||||||
|
your_turn?(conn, game) && "You lost" || "You won"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def game_over?(game) do
|
||||||
|
Game.game_over?(game)
|
||||||
|
end
|
||||||
|
|
||||||
|
def state(conn, game) do
|
||||||
|
cond do
|
||||||
|
Game.game_over?(game) ->
|
||||||
|
states[game.state]
|
||||||
|
your_turn?(conn, game) ->
|
||||||
|
"Your turn"
|
||||||
|
true -> nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def turn_class(conn, game) do
|
def turn_class(conn, game) do
|
||||||
if your_turn?(conn, game) do
|
if your_turn?(conn, game) && !Game.game_over?(game) do
|
||||||
"your-turn"
|
"your-turn"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -24,4 +46,12 @@ defmodule ChessWeb.GameView do
|
|||||||
game.user
|
game.user
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp states do
|
||||||
|
%{
|
||||||
|
"checkmate" => "Checkmate!",
|
||||||
|
"stalemate" => "Stalemate",
|
||||||
|
"check" => "Check",
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user