1
0
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:
Daniel Barber 2018-04-07 19:16:29 -04:00
parent 5bc43465ff
commit e63b7b9069
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
5 changed files with 41 additions and 3 deletions

View File

@ -7,4 +7,5 @@
.your-turn {
font-weight: bold;
background-color: $your-turn-background-color;
}

View File

@ -18,6 +18,8 @@ $white-square-color: #bbb;
$selected-square-color: #0cf;
$available-square-color: #6f0;
$your-turn-background-color: rgba($white, 0.1);
$game-state-background-color: darken($black-square-color, 10%);
$square-outline-color: darken($black-square-color, 20%);

View File

@ -69,6 +69,11 @@ defmodule Chess.Store.Game do
end
def validate_king_in_check(changeset, _, _), do: changeset
def game_over?(game) do
game.state == "checkmate" ||
game.state == "stalemate"
end
def ordered(query) do
query
|> order_by([game], desc: game.inserted_at)

View File

@ -14,10 +14,10 @@
class: "btn btn-default btn-xs" %>
</td>
<td>
<%= if your_turn?(@conn, game), do: gettext("Your turn") %>
<%= state(@conn, game) %>
</td>
<td>
Started on <%= Timex.format!(game.inserted_at, "%b %e at %I:%M %P", :strftime) %>
<%= won_lost(@conn, game) %>
</td>
<td class="text-right">
<%= link gettext("Delete"),

View File

@ -1,10 +1,32 @@
defmodule ChessWeb.GameView do
use ChessWeb, :view
alias Chess.Store.Game
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
if your_turn?(conn, game) do
if your_turn?(conn, game) && !Game.game_over?(game) do
"your-turn"
end
end
@ -24,4 +46,12 @@ defmodule ChessWeb.GameView do
game.user
end
end
defp states do
%{
"checkmate" => "Checkmate!",
"stalemate" => "Stalemate",
"check" => "Check",
}
end
end