From e63b7b906995078c6f050b384fef1ec45f2b06ee Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Sat, 7 Apr 2018 19:16:29 -0400 Subject: [PATCH] Add won/lost to the games list --- assets/css/_game_list.scss | 1 + assets/css/_variables.scss | 2 ++ lib/chess/store/game.ex | 5 ++++ lib/chess_web/templates/game/index.html.eex | 4 +-- lib/chess_web/views/game_view.ex | 32 ++++++++++++++++++++- 5 files changed, 41 insertions(+), 3 deletions(-) diff --git a/assets/css/_game_list.scss b/assets/css/_game_list.scss index 6629bd1..af6f663 100644 --- a/assets/css/_game_list.scss +++ b/assets/css/_game_list.scss @@ -7,4 +7,5 @@ .your-turn { font-weight: bold; + background-color: $your-turn-background-color; } diff --git a/assets/css/_variables.scss b/assets/css/_variables.scss index 1ae402c..785ae57 100644 --- a/assets/css/_variables.scss +++ b/assets/css/_variables.scss @@ -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%); diff --git a/lib/chess/store/game.ex b/lib/chess/store/game.ex index cfa04ce..a412f33 100644 --- a/lib/chess/store/game.ex +++ b/lib/chess/store/game.ex @@ -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) diff --git a/lib/chess_web/templates/game/index.html.eex b/lib/chess_web/templates/game/index.html.eex index a209df1..3de4738 100644 --- a/lib/chess_web/templates/game/index.html.eex +++ b/lib/chess_web/templates/game/index.html.eex @@ -14,10 +14,10 @@ class: "btn btn-default btn-xs" %> - <%= if your_turn?(@conn, game), do: gettext("Your turn") %> + <%= state(@conn, game) %> - Started on <%= Timex.format!(game.inserted_at, "%b %e at %I:%M %P", :strftime) %> + <%= won_lost(@conn, game) %> <%= link gettext("Delete"), diff --git a/lib/chess_web/views/game_view.ex b/lib/chess_web/views/game_view.ex index 612fb9f..23a55f7 100644 --- a/lib/chess_web/views/game_view.ex +++ b/lib/chess_web/views/game_view.ex @@ -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