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
2023-10-04 12:09:04 -05:00

86 lines
1.5 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
conn
|> current_user()
|> player_colour(game) == game.turn
end
def player_colour(conn, game) do
(current_user(conn).id == game.user_id && "white") || "black"
end
def files(conn, game) do
ranks(conn, game)
|> Enum.reverse()
end
def ranks(conn, game) do
if game.user_id == current_user(conn).id do
7..0
else
0..7
end
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