Files
chess-endgame-trainer/lib/chesstrainer_web/live/endgame_live/play.ex
T
owenrees 55d2e2a913 add puzzle training sessions with difficulty and ratings
- /train starts an ephemeral session by choosing easy/medium/hard, banded
  relative to the user's current rating (band width configurable)
- session timer, average per-puzzle time, net rating gain and per-puzzle delta
- next puzzle, plus review and retry reused from the play surface
- explicit stop/start: stopping returns to the difficulty picker
- puzzle rating shown once a puzzle completes, and a side-to-move indicator
- extract the play logic/markup into shared PlayCore and PlayComponents so the
  standalone play view and sessions share one implementation
- sticky footer; glossary entries for Difficulty and Session; ADR 0009
2026-11-05 15:44:11 +01:00

95 lines
3.1 KiB
Elixir

defmodule ChesstrainerWeb.EndgameLive.Play do
use ChesstrainerWeb, :live_view
alias Chesstrainer.Endgames
alias ChesstrainerWeb.EndgameLive.PlayCore
alias ChesstrainerWeb.PlayComponents
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash}>
<.header>
Play endgame {@endgame.key}
<:subtitle>
{@endgame.color |> Atom.to_string() |> String.capitalize()} to move · Rating: {round(
@endgame.rating
)} · Attempts: {@endgame.times_attempted ||
0} · Solved: {@endgame.times_solved || 0}<span :if={@user_rating}> · Your rating: {round(
@user_rating
)}</span>
<p class="text-xs text-muted flex items-center mt-1">
<span>FEN: {@endgame.fen}</span>
<button
id="copy-fen-btn"
phx-hook="CopyFen"
data-fen={@endgame.fen}
class="inline-flex items-center cursor-pointer"
title="Copy FEN to clipboard"
>
<.icon name="hero-document-duplicate" class="w-3.5 h-3.5" />
</button>
</p>
</:subtitle>
<:actions>
<.button navigate={~p"/endgames"}>
<.icon name="hero-arrow-left" /> Back to endgames
</.button>
</:actions>
</.header>
<PlayComponents.play_surface
endgame={@endgame}
status={@status}
blocked_reason={@blocked_reason}
best_move={@best_move}
plies={@plies}
line={@line}
reviewing={@reviewing}
review_index={@review_index}
review_tablebase={@review_tablebase}
review_blocked={@review_blocked}
awaiting_reply={@awaiting_reply}
last_grade={@last_grade}
awaiting_best_line={@awaiting_best_line}
/>
</Layouts.app>
"""
end
@impl true
def mount(%{"public_code" => public_code}, _session, socket) do
endgame = Endgames.get_endgame_by_public_code!(public_code)
{:ok, PlayCore.init(socket, endgame)}
end
@impl true
def handle_event("endgame_move_played", params, socket),
do: PlayCore.move_played(socket, params)
def handle_event("opponent_reply_applied", params, socket),
do: PlayCore.opponent_reply_applied(socket, params)
def handle_event("chess_fen_invalid", params, socket),
do: PlayCore.fen_invalid(socket, params)
def handle_event("reset", _params, socket), do: PlayCore.reset(socket)
def handle_event("review", _params, socket), do: PlayCore.review(socket)
def handle_event("review_prev", _params, socket), do: PlayCore.review_prev(socket)
def handle_event("review_next", _params, socket), do: PlayCore.review_next(socket)
def handle_event("review_step", %{"index" => index}, socket),
do: PlayCore.review_step(socket, index)
def handle_event("play_best_line", _params, socket), do: PlayCore.play_best_line(socket)
def handle_event("best_line_move_applied", params, socket),
do: PlayCore.best_line_move_applied(socket, params)
def handle_event("back_to_game", _params, socket), do: PlayCore.back_to_game(socket)
end