defmodule ChesstrainerWeb.PlayComponents do @moduledoc """ The shared board and side panel for playing an endgame. Used by the standalone play screen and by a training session, which adds the "Next puzzle" action via `session={true}`. """ use ChesstrainerWeb, :html alias ChesstrainerWeb.Board.PositionText alias ChesstrainerWeb.EndgameLive.PlayCore attr :endgame, :any, required: true attr :fen, :string, default: nil attr :status, :atom, required: true attr :blocked_reason, :string, default: nil attr :best_move, :string, default: nil attr :plies, :list, required: true attr :line, :list, required: true attr :reviewing, :boolean, required: true attr :review_index, :integer, required: true attr :review_tablebase, :any, default: nil attr :review_blocked, :string, default: nil attr :awaiting_reply, :boolean, required: true attr :last_grade, :atom, default: nil attr :awaiting_best_line, :boolean, default: false attr :session, :boolean, default: false attr :move_input, :string, default: "" attr :move_input_error, :string, default: nil attr :last_check, :boolean, default: false attr :last_checkmate, :boolean, default: false attr :rating, :any, default: nil attr :rating_delta, :any, default: nil def play_surface(assigns) do assigns = assigns |> assign( :can_input, assigns.status == :playing and not assigns.awaiting_reply and not assigns.reviewing ) |> assign(:move_hint, move_hint(assigns.status, assigns.awaiting_reply)) ~H"""
{board_announcement(assigns)}
{PositionText.summary(position_fen(assigns))}
<.input id="move-input" name="move" value={@move_input} label="Your move" placeholder="Nf3 or g1f3" autocomplete="off" aria-describedby="move-input-help" disabled={not @can_input} class="font-mono" />

SAN or UCI, e.g. Nf3 or g1f3.

{@move_hint}

<.button type="submit" class="mt-6" disabled={not @can_input}>Play move
<.button id="review-start" phx-click="review_start" disabled={@review_index <= 0} title="Back to start" aria-label="Back to start" > {"<<"} <.button id="review-prev" phx-click="review_prev" disabled={@review_index <= 0} title="Previous position" aria-label="Previous position" > ← <.button id="review-next" phx-click="review_next" disabled={@review_index >= length(@line)} title="Next position" aria-label="Next position" > → <.button id="review-end" phx-click="review_end" disabled={@review_index >= length(@line)} title="Go to end" aria-label="Go to end" > {">>"}
<.button :if={(@reviewing and @review_tablebase) && PlayCore.best_move_text(@review_tablebase)} phx-click="play_best_line" disabled={@awaiting_best_line} > Play best line <.button :if={@reviewing and @line != @plies} phx-click="back_to_game"> Back to game <.button :if={@status in [:solved, :failed] and not @reviewing} phx-click="review" > <.icon name="hero-eye" /> Review <.button phx-click="reset" variant="primary"> <.icon name="hero-arrow-path" /> {if @status in [:solved, :failed], do: "Try again", else: "Reset"} <.button :if={@session and @status in [:solved, :failed]} id="next-puzzle-button" phx-click="next_puzzle" variant="primary" > Next puzzle
<%= if @reviewing do %>
<%= cond do %> <% @review_blocked -> %>

Best move unavailable here

{@review_blocked}

<% @review_tablebase -> %>

Best move:

{PlayCore.best_move_text(@review_tablebase) || "—"}

<%= if played_ply = played_ply_at(@line, @review_index) do %>

Played: {played_ply.san}

<% end %> <.tablebase_options tablebase={@review_tablebase} /> <% true -> %>

No best move.

<% end %>
<% else %>
<%= cond do %> <% @status == :loading -> %>

Loading tablebase…

<% @status == :blocked -> %>

Tablebase unavailable

{@blocked_reason}

<% @status == :solved -> %>

{if @endgame.result == :draw, do: "Draw held!", else: "Solved!"}

<% @status == :failed -> %>

Failed

{if @endgame.result == :draw, do: "That move loses the draw.", else: "That move throws away the win."}

<%= if @best_move && @plies != [] do %>

You played {List.last(@plies).san}; best was: {@best_move}

<% end %> <% @awaiting_reply -> %>

Thinkin...

<% @last_grade == :optimal -> %>

Optimal move

<% @last_grade == :winning -> %>

{if @endgame.result == :draw, do: "Keeps the draw — but not the sharpest", else: "Winning — but not the sharpest"}

<% @last_grade == :losing -> %>

{if @endgame.result == :draw, do: "Losing move — this loses the draw", else: "Losing move — this throws away the win"}

<% @last_grade == :unknown -> %>

Could not grade that move

<% true -> %>

Make a move.

You play {colour_name(@endgame.color)}

<% end %>
Puzzle rating: {round(@endgame.rating)} Your rating {round(@rating)} <.rating_change :if={@rating_delta} id="rating-change" delta={@rating_delta} />
<% plies = display_plies(@reviewing, @line, @plies) %>

Moves:

<%= if plies == [] do %>

No Moves

<% else %> <%= for {[white_ply, black_ply], move_no} <- move_rows(plies, @endgame.color) do %>
{move_no}. <%= if white_ply do %> <.ply ply={white_ply} reviewing={@reviewing} review_index={@review_index} result={@endgame.result} /> <% else %> ... <% end %> <%= if black_ply do %> <.ply ply={black_ply} reviewing={@reviewing} review_index={@review_index} result={@endgame.result} /> <% end %>
<% end %> <% end %>
<% end %>
""" end attr :tablebase, :any, required: true # The reviewed position's moves grouped like lichess's tablebase explorer: # each heading is the outcome for the side to move, one table per outcome. defp tablebase_options(assigns) do assigns = assign(assigns, :groups, tablebase_groups(assigns.tablebase)) ~H"""

Tablebase

{label}

{dtz_text(move)}
""" end defp tablebase_groups(%{moves: moves}) when is_list(moves) do [ {"Winning", "text-success-text", :win}, {"Drawn", "text-muted", :draw}, {"Losing", "text-error-text", :loss}, {"Unknown", "text-muted", :unknown} ] |> Enum.flat_map(fn {label, class, outcome} -> case Enum.filter(moves, &(mover_outcome(&1.category) == outcome)) do [] -> [] moves -> [{label, class, moves}] end end) end defp tablebase_groups(_tablebase), do: [] # A move's category is the outcome for the side to move *after* the move, so # the mover's outcome is its flip. defp mover_outcome(:loss), do: :win defp mover_outcome(:win), do: :loss defp mover_outcome(:draw), do: :draw defp mover_outcome(_), do: :unknown defp dtz_text(%{dtz: nil}), do: "" defp dtz_text(%{dtz: dtz}), do: "DTZ #{abs(dtz)}" attr :ply, :map, required: true attr :reviewing, :boolean, required: true attr :review_index, :integer, required: true attr :result, :atom, required: true defp ply(assigns) do ~H""" <%= if @reviewing do %> <% else %> {@ply.san} — {ply_title(@ply, @result)} <% end %> """ end defp move_rows(plies, color) do padded = if color in [:black, "black"], do: [nil | plies], else: plies padded |> Enum.chunk_every(2, 2, [nil]) |> Enum.with_index(1) end defp display_plies(true, line, _plies), do: line defp display_plies(false, _line, plies), do: plies defp colour_name(:white), do: "White" defp colour_name(:black), do: "Black" defp colour_name(colour), do: colour |> to_string() |> String.capitalize() defp current_ply?(%{index: index}, review_index), do: index + 1 == review_index defp played_ply_at(line, review_index) do case Enum.at(line, review_index) do %{analysis: true} -> nil ply -> ply end end defp ply_class(%{best_line: true}), do: "text-info italic" defp ply_class(%{analysis: true}), do: "text-info" defp ply_class(ply), do: grade_class(ply.grade) defp ply_title(%{best_line: true}, _result), do: "Best line" defp ply_title(%{analysis: true}, _result), do: "Tablebase move" defp ply_title(ply, result), do: grade_label(ply.grade, result) defp grade_class(:optimal), do: "text-success-text" defp grade_class(:winning), do: "text-warning-text" defp grade_class(:losing), do: "text-error-text" defp grade_class(_), do: "text-body" defp grade_label(nil, _result), do: nil defp grade_label(:optimal, _result), do: "Optimal move" defp grade_label(:winning, :draw), do: "Keeps the draw — but not the sharpest" defp grade_label(:winning, _result), do: "Winning — but not the sharpest" defp grade_label(:losing, :draw), do: "Loses the draw" defp grade_label(:losing, _result), do: "Throws away the win" defp grade_label(:unknown, _result), do: "Not graded" defp move_hint(:loading, _awaiting), do: "Loading the tablebase…" defp move_hint(:blocked, _awaiting), do: "The tablebase is unavailable, so moves cannot be entered." defp move_hint(status, _awaiting) when status in [:solved, :failed], do: "This puzzle is finished. Reset or try again." defp move_hint(_status, true), do: "Waiting for the opponent's reply." defp move_hint(_status, _awaiting), do: nil # While the opponent's move is in flight the server's `fen` still points at # the position before it, so describe the move the player just made instead. defp position_fen(%{awaiting_reply: true, plies: plies}) do case List.last(plies) do %{fen_after: fen} -> fen _ -> nil end end defp position_fen(%{fen: fen}) when is_binary(fen), do: fen defp position_fen(%{endgame: %{fen: fen}}), do: fen defp board_announcement(assigns) do cond do assigns.status == :loading -> "Loading the tablebase." assigns.status == :blocked -> "Tablebase unavailable. #{assigns.blocked_reason || "Moves cannot be graded."}" assigns.reviewing -> review_announcement(assigns) assigns.status == :solved -> join_sentences([solved_sentence(assigns), check_sentence(assigns)]) assigns.status == :failed -> join_sentences(["Failed.", failure_sentence(assigns), best_move_sentence(assigns)]) assigns.awaiting_reply -> join_sentences([ "Move #{length(assigns.plies)}: you played #{last_san(assigns.plies)}.", grade_sentence(assigns), check_sentence(assigns), "Waiting for the opponent's reply." ]) assigns.plies == [] -> "You play #{colour_name(assigns.endgame.color)}. #{colour_name(assigns.endgame.color)} to move." true -> case List.last(assigns.plies) do %{grade: nil} -> join_sentences([ "Move #{length(assigns.plies)}: opponent played #{last_san(assigns.plies)}.", check_sentence(assigns), "Your move." ]) _ -> join_sentences([ "Move #{length(assigns.plies)}: you played #{last_san(assigns.plies)}.", grade_sentence(assigns), check_sentence(assigns), "Your move." ]) end end end defp review_announcement(assigns) do previous = Enum.at(assigns.line, assigns.review_index - 1) position = cond do assigns.review_index <= 0 -> "Starting position." previous -> "After #{previous.san}." true -> "Final position." end "Reviewing position #{assigns.review_index} of #{length(assigns.line)}. #{position}" end defp failure_sentence(%{endgame: %{result: :draw}}), do: "That move loses the draw." defp failure_sentence(_assigns), do: "That move throws away the win." defp solved_sentence(%{endgame: %{result: :draw}}), do: "Draw held!" defp solved_sentence(_assigns), do: "Solved!" defp best_move_sentence(%{best_move: nil}), do: "" defp best_move_sentence(%{best_move: best}), do: "Best was #{best}." defp grade_sentence(%{plies: plies, endgame: endgame}) do case List.last(plies) do %{grade: grade} -> case grade_label(grade, endgame.result) do nil -> "" label -> "#{label}." end _ -> "" end end defp check_sentence(%{last_checkmate: true}), do: "Checkmate." defp check_sentence(%{last_check: true}), do: "Check." defp check_sentence(_assigns), do: "" defp last_san(plies) do case List.last(plies) do %{san: san} when is_binary(san) -> san _ -> "a move" end end defp join_sentences(sentences) do sentences |> Enum.reject(&(&1 in [nil, ""])) |> Enum.join(" ") end end