mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
* commented out lesson and learning strip until lessons are established * reword the hero section * change layout and working on puzzle section - remove top div as it was redundant * amend flash message on replay * switch cancel session for return home * remove system theme toggle since this is the default, leaving only light and dark toggles * add keyboard and screen reader support for play (#72) First draft covering the accessibility work in #72:\n\n- labelled SAN/UCI move input, validated against the tablebase and applied\n by the board hook, plus an accessible promotion picker\n- live-region announcements for moves, checks, checkmates and solve/fail,\n and a hidden text summary of the position\n- accessible labels, focus ring, skip link, keyboard-friendly review table\n and non-colour grade cues\n- WCAG AA text contrast roles and a responsive play surface\n- regression tests for move entry, announcements and link-style CTAs
This commit is contained in:
@@ -6,6 +6,8 @@ defmodule ChesstrainerWeb.Board.BoardComponent do
|
||||
use Phoenix.Component
|
||||
import ChesstrainerWeb.Board.SquareComponent
|
||||
|
||||
alias ChesstrainerWeb.Board.PositionText
|
||||
|
||||
attr :game, :map, required: true
|
||||
|
||||
attr :myself, :any, default: nil
|
||||
@@ -41,7 +43,11 @@ defmodule ChesstrainerWeb.Board.BoardComponent do
|
||||
|
||||
~H"""
|
||||
<div class={["m-auto left-0 right-0", @class]}>
|
||||
<div class="border border-2 border-zinc-700 grid grid-rows-8 grid-cols-8">
|
||||
<div
|
||||
class="border border-2 border-zinc-700 grid grid-rows-8 grid-cols-8"
|
||||
role="img"
|
||||
aria-label={"Chess position. #{PositionText.summary(Map.get(@game, :fen))}"}
|
||||
>
|
||||
<%= for rank <- @ranks do %>
|
||||
<%= for file <- @files do %>
|
||||
<% square = {file, rank} %>
|
||||
|
||||
@@ -12,7 +12,7 @@ defmodule ChesstrainerWeb.Board.PieceComponent do
|
||||
|
||||
~H"""
|
||||
<%= if @path do %>
|
||||
<img src={@path} class={@class} />
|
||||
<img src={@path} class={@class} alt="" aria-hidden="true" />
|
||||
<% end %>
|
||||
"""
|
||||
end
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
defmodule ChesstrainerWeb.Board.PositionText do
|
||||
@moduledoc """
|
||||
Plain-language descriptions of a chess position for assistive technology.
|
||||
|
||||
A chessground board is a picture to a screen reader, so the play surface and
|
||||
the static boards use this text as their accessible name or description.
|
||||
"""
|
||||
|
||||
alias Chesstrainer.FEN
|
||||
|
||||
@files ~w(a b c d e f g h)
|
||||
|
||||
@names %{
|
||||
"k" => "king",
|
||||
"q" => "queen",
|
||||
"r" => "rook",
|
||||
"b" => "bishop",
|
||||
"n" => "knight",
|
||||
"p" => "pawn"
|
||||
}
|
||||
|
||||
@doc """
|
||||
Describes whose turn it is and where every piece stands.
|
||||
|
||||
Returns a generic description when the FEN cannot be read.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> ChesstrainerWeb.Board.PositionText.summary("8/8/3k4/8/8/3K1R2/8/8 w - - 0 1")
|
||||
"White to move. White: king on d3, rook on f3. Black: king on d6."
|
||||
"""
|
||||
@spec summary(String.t() | nil) :: String.t()
|
||||
def summary(fen) when is_binary(fen) do
|
||||
case FEN.board(fen) do
|
||||
nil ->
|
||||
"Chess position."
|
||||
|
||||
rows ->
|
||||
["#{turn(fen)}." | piece_groups(rows)]
|
||||
|> Enum.join(" ")
|
||||
end
|
||||
end
|
||||
|
||||
def summary(_fen), do: "Chess position."
|
||||
|
||||
defp turn(fen) do
|
||||
case FEN.color_from_fen(fen) do
|
||||
:white -> "White to move"
|
||||
:black -> "Black to move"
|
||||
_ -> "Side to move unknown"
|
||||
end
|
||||
end
|
||||
|
||||
defp piece_groups(rows) do
|
||||
["White", "Black"]
|
||||
|> Enum.map(fn label -> group(label, rows) end)
|
||||
|> Enum.reject(&is_nil/1)
|
||||
end
|
||||
|
||||
defp group(label, rows) do
|
||||
colour = if label == "White", do: :white, else: :black
|
||||
|
||||
case pieces(rows, colour) do
|
||||
[] -> nil
|
||||
pieces -> "#{label}: #{Enum.join(pieces, ", ")}."
|
||||
end
|
||||
end
|
||||
|
||||
defp pieces(rows, colour) do
|
||||
rows
|
||||
|> Enum.with_index()
|
||||
|> Enum.flat_map(fn {row, rank_index} ->
|
||||
row
|
||||
|> Enum.with_index()
|
||||
|> Enum.flat_map(fn
|
||||
{nil, _file_index} ->
|
||||
[]
|
||||
|
||||
{piece, file_index} ->
|
||||
if piece_colour(piece) == colour do
|
||||
square = "#{Enum.at(@files, file_index)}#{8 - rank_index}"
|
||||
["#{Map.fetch!(@names, String.downcase(piece))} on #{square}"]
|
||||
else
|
||||
[]
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
defp piece_colour(piece) do
|
||||
if piece == String.upcase(piece), do: :white, else: :black
|
||||
end
|
||||
end
|
||||
@@ -112,7 +112,7 @@ defmodule ChesstrainerWeb.CoreComponents do
|
||||
<.button phx-click="go" variant="primary">Send!</.button>
|
||||
<.button navigate={~p"/"}>Home</.button>
|
||||
"""
|
||||
attr :rest, :global, include: ~w(href navigate patch method download name value disabled)
|
||||
attr :rest, :global, include: ~w(href navigate patch method download name value disabled type)
|
||||
attr :class, :any
|
||||
attr :variant, :string, values: ~w(primary secondary ghost danger)
|
||||
slot :inner_block, required: true
|
||||
@@ -123,7 +123,7 @@ defmodule ChesstrainerWeb.CoreComponents do
|
||||
"secondary" => "border border-base-300 bg-base-100 text-base-content hover:bg-base-200",
|
||||
"ghost" => "text-base-content hover:bg-base-200",
|
||||
"danger" => "bg-error text-error-content hover:bg-error/90",
|
||||
nil => "bg-primary/15 text-primary hover:bg-primary/25"
|
||||
nil => "bg-primary/15 text-primary-text hover:bg-primary/25"
|
||||
}
|
||||
|
||||
assigns =
|
||||
@@ -338,7 +338,7 @@ defmodule ChesstrainerWeb.CoreComponents do
|
||||
# Helper used by inputs to generate form errors
|
||||
defp error(assigns) do
|
||||
~H"""
|
||||
<p class="mt-1.5 flex gap-2 items-center text-sm text-error">
|
||||
<p class="mt-1.5 flex gap-2 items-center text-sm text-error-text">
|
||||
<.icon name="hero-exclamation-circle" class="size-5" />
|
||||
{render_slot(@inner_block)}
|
||||
</p>
|
||||
@@ -479,8 +479,8 @@ defmodule ChesstrainerWeb.CoreComponents do
|
||||
"inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-xs font-medium",
|
||||
@size == "sm" && "px-1.5 text-[0.7rem]",
|
||||
@color == "neutral" && "bg-base-200 text-base-content",
|
||||
@color == "primary" && "bg-primary/15 text-primary",
|
||||
@color == "warning" && "bg-warning/15 text-warning",
|
||||
@color == "primary" && "bg-primary/15 text-primary-text",
|
||||
@color == "warning" && "bg-warning/15 text-warning-text",
|
||||
@class
|
||||
]}
|
||||
{@rest}
|
||||
|
||||
@@ -136,6 +136,7 @@ defmodule ChesstrainerWeb.EndgameList do
|
||||
phx-click="remove_theme"
|
||||
phx-value-id={theme.id}
|
||||
aria-label={"Remove #{theme.name}"}
|
||||
class="px-1"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
|
||||
@@ -12,6 +12,7 @@ defmodule ChesstrainerWeb.LandingComponents do
|
||||
|
||||
alias Chesstrainer.FEN
|
||||
alias Chesstrainer.Themes
|
||||
alias ChesstrainerWeb.Board.PositionText
|
||||
|
||||
@button_variants %{
|
||||
nav: "nav-cta",
|
||||
@@ -84,9 +85,9 @@ defmodule ChesstrainerWeb.LandingComponents do
|
||||
<section class="mailing-list" id="mailing-list">
|
||||
<div class="mailing-list-copy">
|
||||
<span class="section-kicker">Mailing list</span>
|
||||
<h2>One good endgame idea, now and then.</h2>
|
||||
<h2>Stay in the loop.</h2>
|
||||
<p>
|
||||
Occasional emails about new puzzles and features. We'll ask you to confirm your address first.
|
||||
Get occasional endgame ideas, new puzzles, and feature updates delivered to your inbox. (Please confirm your email after signing up.)
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@@ -180,14 +181,21 @@ defmodule ChesstrainerWeb.LandingComponents do
|
||||
assigns = assign(assigns, :squares, squares)
|
||||
|
||||
~H"""
|
||||
<div class="chessboard" aria-label="Chess position">
|
||||
<div
|
||||
class="chessboard"
|
||||
role="img"
|
||||
aria-label={"Chess position. #{PositionText.summary(assigns.fen)}"}
|
||||
>
|
||||
<%= for square <- @squares do %>
|
||||
<div class={[
|
||||
"square",
|
||||
if(square.light, do: "light", else: "dark"),
|
||||
square.selected? && "selected",
|
||||
square.target? && "target"
|
||||
]}>
|
||||
<div
|
||||
class={[
|
||||
"square",
|
||||
if(square.light, do: "light", else: "dark"),
|
||||
square.selected? && "selected",
|
||||
square.target? && "target"
|
||||
]}
|
||||
aria-hidden="true"
|
||||
>
|
||||
<span :if={square.label_rank} class="rank">{square.rank}</span>
|
||||
<span :if={square.label_file} class="file">{square.file}</span>
|
||||
<span
|
||||
|
||||
@@ -42,7 +42,7 @@ defmodule ChesstrainerWeb.Layouts do
|
||||
|
||||
def app(assigns) do
|
||||
~H"""
|
||||
<main class="px-4 py-20 sm:px-6 lg:px-8">
|
||||
<main id="main-content" tabindex="-1" class="px-4 py-20 sm:px-6 lg:px-8">
|
||||
<div class={["mx-auto space-y-4", if(@wide, do: "max-w-7xl", else: "max-w-2xl")]}>
|
||||
{render_slot(@inner_block)}
|
||||
</div>
|
||||
@@ -235,7 +235,10 @@ defmodule ChesstrainerWeb.Layouts do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Provides the system / light / dark colour-mode switch.
|
||||
Provides the light / dark colour-mode switch.
|
||||
|
||||
With no saved preference the page follows the OS setting; only the explicit
|
||||
light and dark modes are selectable here.
|
||||
|
||||
The click handling and `aria-checked` syncing live in `theme_script.html.heex`,
|
||||
which runs before paint and also on pages without LiveView (the landing page
|
||||
@@ -250,40 +253,31 @@ defmodule ChesstrainerWeb.Layouts do
|
||||
aria-label="Colour mode"
|
||||
>
|
||||
<div
|
||||
class="absolute w-1/3 h-full rounded-full border border-base-200 bg-base-100 brightness-200 left-0 [[data-theme=light]_&]:left-1/3 [[data-theme=dark]_&]:left-2/3 [[data-theme-source=system]_&]:!left-0 transition-[left]"
|
||||
class="absolute w-1/2 h-full rounded-full border border-base-200 bg-base-100 brightness-200 left-0 [[data-theme=dark]_&]:left-1/2 transition-[left]"
|
||||
aria-hidden="true"
|
||||
>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="flex p-2 cursor-pointer w-1/3 justify-center rounded-full focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
|
||||
data-phx-theme="system"
|
||||
role="radio"
|
||||
aria-checked="true"
|
||||
aria-label="System colour mode"
|
||||
>
|
||||
<.icon name="hero-computer-desktop-micro" class="size-4 opacity-75 hover:opacity-100" />
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="flex p-2 cursor-pointer w-1/3 justify-center rounded-full focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
|
||||
class="flex p-2 cursor-pointer w-1/2 justify-center rounded-full focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
|
||||
data-phx-theme="light"
|
||||
role="radio"
|
||||
aria-checked="false"
|
||||
aria-checked="true"
|
||||
aria-label="Light colour mode"
|
||||
tabindex="0"
|
||||
>
|
||||
<.icon name="hero-sun-micro" class="size-4 opacity-75 hover:opacity-100" />
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="flex p-2 cursor-pointer w-1/3 justify-center rounded-full focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
|
||||
class="flex p-2 cursor-pointer w-1/2 justify-center rounded-full focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
|
||||
data-phx-theme="dark"
|
||||
role="radio"
|
||||
aria-checked="false"
|
||||
aria-label="Dark colour mode"
|
||||
tabindex="-1"
|
||||
>
|
||||
<.icon name="hero-moon-micro" class="size-4 opacity-75 hover:opacity-100" />
|
||||
</button>
|
||||
|
||||
@@ -67,6 +67,12 @@
|
||||
<Layouts.theme_script />
|
||||
</head>
|
||||
<body>
|
||||
<a
|
||||
href="#main-content"
|
||||
class="sr-only focus:not-sr-only focus:fixed focus:left-4 focus:top-4 focus:z-50 focus:rounded-md focus:bg-base-100 focus:px-4 focus:py-2 focus:text-base-content focus:shadow-lg"
|
||||
>
|
||||
Skip to main content
|
||||
</a>
|
||||
<Layouts.navbar current_scope={@current_scope} />
|
||||
{@inner_content}
|
||||
<Layouts.footer />
|
||||
|
||||
@@ -16,14 +16,15 @@
|
||||
}
|
||||
};
|
||||
|
||||
const selectedMode = () => {
|
||||
const source = document.documentElement.getAttribute("data-theme-source");
|
||||
return source === "system" ? "system" : document.documentElement.getAttribute("data-theme");
|
||||
};
|
||||
// There is no system button, so report the effective theme. This keeps the
|
||||
// visible light/dark control in sync when the page is following the OS.
|
||||
const selectedMode = () => document.documentElement.getAttribute("data-theme");
|
||||
|
||||
const syncChecked = () => {
|
||||
document.querySelectorAll("[data-phx-theme]").forEach((button) => {
|
||||
button.setAttribute("aria-checked", String(button.dataset.phxTheme === selectedMode()));
|
||||
const checked = button.dataset.phxTheme === selectedMode();
|
||||
button.setAttribute("aria-checked", String(checked));
|
||||
button.tabIndex = checked ? 0 : -1;
|
||||
});
|
||||
};
|
||||
|
||||
@@ -56,6 +57,25 @@
|
||||
if (button) setMode(button.dataset.phxTheme);
|
||||
});
|
||||
|
||||
// Arrow keys move between the radio options, as a radiogroup should.
|
||||
document.addEventListener("keydown", (event) => {
|
||||
const button = event.target instanceof Element && event.target.closest("[data-phx-theme]");
|
||||
if (!button) return;
|
||||
|
||||
if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return;
|
||||
|
||||
event.preventDefault();
|
||||
const buttons = Array.from(document.querySelectorAll("[data-phx-theme]"));
|
||||
const offset = event.key === "ArrowRight" ? 1 : -1;
|
||||
const index = (buttons.indexOf(button) + offset + buttons.length) % buttons.length;
|
||||
const next = buttons[index];
|
||||
|
||||
if (next) {
|
||||
setMode(next.dataset.phxTheme);
|
||||
next.focus();
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener("storage", (event) => {
|
||||
if (event.key === "phx:theme") setMode(event.newValue || "system");
|
||||
});
|
||||
|
||||
@@ -8,9 +8,11 @@ defmodule ChesstrainerWeb.PlayComponents do
|
||||
|
||||
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
|
||||
@@ -24,26 +26,98 @@ defmodule ChesstrainerWeb.PlayComponents do
|
||||
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
|
||||
|
||||
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"""
|
||||
<div class="flex gap-2 bg-base-200 p-4 rounded-xl shadow-inner">
|
||||
<div class="w-100 flex flex-col gap-2">
|
||||
<div
|
||||
id="endgame-board"
|
||||
phx-hook="ChessBoard"
|
||||
phx-update="ignore"
|
||||
data-endgame-id={@endgame.id}
|
||||
class="w-100 h-100"
|
||||
>
|
||||
<div class="flex flex-col gap-2 rounded-xl bg-base-200 p-4 shadow-inner sm:flex-row">
|
||||
<div class="flex w-full flex-col gap-2 sm:w-100">
|
||||
<div class="relative">
|
||||
<div
|
||||
id="endgame-board"
|
||||
phx-hook="ChessBoard"
|
||||
phx-update="ignore"
|
||||
data-endgame-id={@endgame.id}
|
||||
class="aspect-square w-full"
|
||||
aria-hidden="true"
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div :if={@reviewing} id="review-controls" class="flex items-center gap-2">
|
||||
<div
|
||||
id="board-announcer"
|
||||
role="status"
|
||||
aria-live="polite"
|
||||
aria-atomic="true"
|
||||
class="sr-only"
|
||||
>
|
||||
{board_announcement(assigns)}
|
||||
</div>
|
||||
<div id="position-summary" class="sr-only">
|
||||
{PositionText.summary(position_fen(assigns))}
|
||||
</div>
|
||||
|
||||
<form
|
||||
:if={not @reviewing}
|
||||
id="move-input-form"
|
||||
phx-submit="submit_move"
|
||||
phx-change="move_input"
|
||||
phx-debounce="200"
|
||||
class="flex items-start gap-2"
|
||||
>
|
||||
<div class="min-w-0 flex-1">
|
||||
<.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"
|
||||
/>
|
||||
<p id="move-input-help" class="mt-1 text-xs text-muted">
|
||||
SAN or UCI, e.g. <span class="font-mono">Nf3</span>
|
||||
or <span class="font-mono">g1f3</span>.
|
||||
</p>
|
||||
<p :if={@move_hint} class="mt-1 text-xs text-muted" role="status">{@move_hint}</p>
|
||||
<p
|
||||
:if={@move_input_error}
|
||||
id="move-input-error"
|
||||
role="alert"
|
||||
class="mt-1 text-sm text-error-text"
|
||||
>
|
||||
{@move_input_error}
|
||||
</p>
|
||||
</div>
|
||||
<.button type="submit" class="mt-6" disabled={not @can_input}>Play move</.button>
|
||||
</form>
|
||||
|
||||
<div
|
||||
:if={@reviewing}
|
||||
id="review-controls"
|
||||
class="flex items-center gap-2"
|
||||
role="group"
|
||||
aria-label="Review navigation"
|
||||
>
|
||||
<.button
|
||||
id="review-start"
|
||||
phx-click="review_start"
|
||||
disabled={@review_index <= 0}
|
||||
title="Back to start"
|
||||
aria-label="Back to start"
|
||||
>
|
||||
{"<<"}
|
||||
</.button>
|
||||
@@ -52,6 +126,7 @@ defmodule ChesstrainerWeb.PlayComponents do
|
||||
phx-click="review_prev"
|
||||
disabled={@review_index <= 0}
|
||||
title="Previous position"
|
||||
aria-label="Previous position"
|
||||
>
|
||||
←
|
||||
</.button>
|
||||
@@ -60,6 +135,7 @@ defmodule ChesstrainerWeb.PlayComponents do
|
||||
phx-click="review_next"
|
||||
disabled={@review_index >= length(@line)}
|
||||
title="Next position"
|
||||
aria-label="Next position"
|
||||
>
|
||||
→
|
||||
</.button>
|
||||
@@ -68,12 +144,13 @@ defmodule ChesstrainerWeb.PlayComponents do
|
||||
phx-click="review_end"
|
||||
disabled={@review_index >= length(@line)}
|
||||
title="Go to end"
|
||||
aria-label="Go to end"
|
||||
>
|
||||
{">>"}
|
||||
</.button>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-wrap gap-2">
|
||||
<div class="flex flex-wrap gap-2" role="group" aria-label="Puzzle actions">
|
||||
<.button
|
||||
:if={(@reviewing and @review_tablebase) && PlayCore.best_move_text(@review_tablebase)}
|
||||
phx-click="play_best_line"
|
||||
@@ -105,8 +182,8 @@ defmodule ChesstrainerWeb.PlayComponents do
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="relative w-48">
|
||||
<div class="absolute inset-0 flex flex-col gap-3">
|
||||
<div class="relative w-full sm:w-48">
|
||||
<div class="flex flex-col gap-3 sm:absolute sm:inset-0">
|
||||
<%= if @reviewing do %>
|
||||
<div
|
||||
id="best-move-panel"
|
||||
@@ -128,29 +205,20 @@ defmodule ChesstrainerWeb.PlayComponents do
|
||||
<% end %>
|
||||
</div>
|
||||
<% else %>
|
||||
<div class="rounded-lg bg-canvas-raised p-3 text-sm">
|
||||
<p class="flex items-center justify-between gap-2 font-semibold">
|
||||
<span>{turn_label(@status, @awaiting_reply)}</span>
|
||||
<span class="text-xs font-normal text-muted">
|
||||
You play {colour_name(@endgame.color)}
|
||||
</span>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-lg bg-canvas-raised p-3 text-sm">
|
||||
<%= cond do %>
|
||||
<% @status == :loading -> %>
|
||||
<p class="text-muted">Loading tablebase…</p>
|
||||
<% @status == :blocked -> %>
|
||||
<p class="font-semibold text-error">Tablebase unavailable</p>
|
||||
<p class="text-error">{@blocked_reason}</p>
|
||||
<p class="font-semibold text-error-text">Tablebase unavailable</p>
|
||||
<p class="text-error-text">{@blocked_reason}</p>
|
||||
<% @status == :solved -> %>
|
||||
<p class="font-semibold text-success-text">
|
||||
{if @endgame.result == :draw, do: "Draw held!", else: "Solved!"}
|
||||
</p>
|
||||
<% @status == :failed -> %>
|
||||
<p class="font-semibold text-error">Failed</p>
|
||||
<p class="text-error">
|
||||
<p class="font-semibold text-error-text">Failed</p>
|
||||
<p class="text-error-text">
|
||||
{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
|
||||
</p>
|
||||
<% end %>
|
||||
<% @awaiting_reply -> %>
|
||||
<p class="text-muted">Opponent is replying…</p>
|
||||
<p class="text-muted">Thinkin...</p>
|
||||
<% @last_grade == :optimal -> %>
|
||||
<p class="font-semibold text-success-text">Optimal move</p>
|
||||
<% @last_grade == :winning -> %>
|
||||
<p class="font-semibold text-warning">
|
||||
<p class="font-semibold text-warning-text">
|
||||
{if @endgame.result == :draw,
|
||||
do: "Keeps the draw — but not the sharpest",
|
||||
else: "Winning — but not the sharpest"}
|
||||
</p>
|
||||
<% @last_grade == :losing -> %>
|
||||
<p class="font-semibold text-error">
|
||||
<p class="font-semibold text-error-text">
|
||||
{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 -> %>
|
||||
<p class="font-semibold text-muted">Could not grade that move</p>
|
||||
<% true -> %>
|
||||
<p class="text-muted">Make a move to have it graded.</p>
|
||||
<p class="text-muted font-bold">Make a move.</p>
|
||||
<p class="text-muted">You play {colour_name(@endgame.color)}</p>
|
||||
<% end %>
|
||||
<p :if={@status in [:solved, :failed]} class="mt-1 text-xs text-muted">
|
||||
Puzzle rating: {round(@endgame.rating)}
|
||||
@@ -188,6 +257,8 @@ defmodule ChesstrainerWeb.PlayComponents do
|
||||
|
||||
<div
|
||||
id="move-list"
|
||||
role="region"
|
||||
aria-label="Moves"
|
||||
class="flex-1 min-h-0 flex flex-col bg-canvas-raised rounded-lg p-3"
|
||||
>
|
||||
<% plies = display_plies(@reviewing, @line, @plies) %>
|
||||
@@ -243,14 +314,19 @@ defmodule ChesstrainerWeb.PlayComponents do
|
||||
<p class={["text-xs font-semibold", class]}>{label}</p>
|
||||
<table class="w-full text-left text-xs font-mono">
|
||||
<tbody>
|
||||
<tr
|
||||
:for={move <- moves}
|
||||
phx-click="review_move"
|
||||
phx-value-uci={move.uci}
|
||||
class="cursor-pointer hover:bg-base-200"
|
||||
title={"Play #{move.san}"}
|
||||
>
|
||||
<td class={["py-0.5 pr-2", class]}>{move.san}</td>
|
||||
<tr :for={move <- moves}>
|
||||
<td class={["py-0.5 pr-2", class]}>
|
||||
<button
|
||||
type="button"
|
||||
phx-click="review_move"
|
||||
phx-value-uci={move.uci}
|
||||
class="text-left hover:underline"
|
||||
title={"Play #{move.san}"}
|
||||
aria-label={"Play #{move.san}"}
|
||||
>
|
||||
{move.san}
|
||||
</button>
|
||||
</td>
|
||||
<td class="py-0.5 text-right text-muted" title="Distance to zeroing (DTZ)">
|
||||
{dtz_text(move)}
|
||||
</td>
|
||||
@@ -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}
|
||||
<span :if={ply_title(@ply, @result)} class="sr-only"> — {ply_title(@ply, @result)}</span>
|
||||
</button>
|
||||
<% else %>
|
||||
<span
|
||||
@@ -319,6 +397,7 @@ defmodule ChesstrainerWeb.PlayComponents do
|
||||
data-grade={@ply.grade}
|
||||
>
|
||||
{@ply.san}
|
||||
<span :if={ply_title(@ply, @result)} class="sr-only"> — {ply_title(@ply, @result)}</span>
|
||||
</span>
|
||||
<% 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
|
||||
|
||||
Reference in New Issue
Block a user