1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00
chess/lib/chess_web/views/game_view.ex
Dan Barber bedd6605ef
Redesign whole site in B&W
* Forms and board tweaks for small screen sizes
* Re-style game status indicators
* Re-style viewing/offline indicator
* Better eyecons 👁
* Re-organise CSS according to ITCSS principles
* Pick new font from Google Fonts
* Fix up tests
* Move some things into partials
2018-09-16 14:02:52 -04:00

68 lines
1.3 KiB
Elixir

defmodule ChessWeb.GameView do
use ChessWeb, :view
alias Chess.GameState
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) &&
gettext("You lost") ||
gettext("You won")
end
end
def game_over?(game) do
GameState.game_over?(game)
end
def state(conn, game) do
cond do
GameState.game_over?(game) ->
states()[game.state]
your_turn?(conn, game) ->
gettext("Your turn")
true -> nil
end
end
def turn_class(conn, game) do
if your_turn?(conn, game) && !GameState.game_over?(game) do
"games-list__your-turn"
end
end
def your_turn?(conn, game) do
player_colour(conn, game) == game.turn
end
def player_colour(conn, game) do
current_user(conn).id == game.user_id && "white" || "black"
end
def player(game, user_id) do
if game.user_id == user_id do
"white"
else
"black"
end
end
def opponent(game, user_id) do
if game.user_id == user_id do
game.opponent
else
game.user
end
end
defp states do
%{
"checkmate" => gettext("Checkmate!"),
"stalemate" => gettext("Stalemate"),
"check" => gettext("Check"),
}
end
end