- {turn_label(@status, @awaiting_reply)} - - You play {colour_name(@endgame.color)} - -
-Loading tablebase…
<% @status == :blocked -> %> -Tablebase unavailable
-{@blocked_reason}
+Tablebase unavailable
+{@blocked_reason}
<% @status == :solved -> %>{if @endgame.result == :draw, do: "Draw held!", else: "Solved!"}
<% @status == :failed -> %> -Failed
-+
Failed
+{if @endgame.result == :draw, do: "That move loses the draw.", else: "That move throws away the win."} @@ -161,17 +229,17 @@ defmodule ChesstrainerWeb.PlayComponents do
<% end %> <% @awaiting_reply -> %> -Opponent is replying…
+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"} @@ -179,7 +247,8 @@ defmodule ChesstrainerWeb.PlayComponents do <% @last_grade == :unknown -> %>
Could not grade that move
<% true -> %> -Make a move to have it graded.
+Make a move.
+You play {colour_name(@endgame.color)}
<% end %>Puzzle rating: {round(@endgame.rating)} @@ -188,6 +257,8 @@ defmodule ChesstrainerWeb.PlayComponents do
{label}
| {move.san} | +|
| + + | {dtz_text(move)} | @@ -266,7 +342,7 @@ defmodule ChesstrainerWeb.PlayComponents do [ {"Winning", "text-success-text", :win}, {"Drawn", "text-muted", :draw}, - {"Losing", "text-error", :loss}, + {"Losing", "text-error-text", :loss}, {"Unknown", "text-muted", :unknown} ] |> Enum.flat_map(fn {label, class, outcome} -> @@ -308,9 +384,11 @@ defmodule ChesstrainerWeb.PlayComponents do current_ply?(@ply, @review_index) && "underline" ]} title={ply_title(@ply, @result)} + aria-current={current_ply?(@ply, @review_index) && "step"} data-grade={@ply.grade} > {@ply.san} + — {ply_title(@ply, @result)} <% else %> {@ply.san} + — {ply_title(@ply, @result)} <% end %> """ @@ -335,13 +414,6 @@ defmodule ChesstrainerWeb.PlayComponents do defp display_plies(true, line, _plies), do: line defp display_plies(false, _line, plies), do: plies - defp turn_label(:playing, false), do: "Your move" - defp turn_label(:playing, true), do: "Opponent's move" - defp turn_label(:solved, _awaiting_reply), do: "Solved" - defp turn_label(:failed, _awaiting_reply), do: "Finished" - defp turn_label(:blocked, _awaiting_reply), do: "Unavailable" - defp turn_label(_status, _awaiting_reply), do: "Loading…" - defp colour_name(:white), do: "White" defp colour_name(:black), do: "Black" defp colour_name(colour), do: colour |> to_string() |> String.capitalize() @@ -364,8 +436,8 @@ defmodule ChesstrainerWeb.PlayComponents do 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" - defp grade_class(:losing), do: "text-error" + 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 @@ -375,4 +447,128 @@ defmodule ChesstrainerWeb.PlayComponents do 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 diff --git a/lib/chesstrainer_web/controllers/page_html/home.html.heex b/lib/chesstrainer_web/controllers/page_html/home.html.heex index 8f78792..5a3cfe8 100644 --- a/lib/chesstrainer_web/controllers/page_html/home.html.heex +++ b/lib/chesstrainer_web/controllers/page_html/home.html.heex @@ -1,16 +1,15 @@