Commit Graph
20 Commits
Author SHA1 Message Date
owenrees eea8e48685 Make the play screen current-user aware (#38) 2026-11-03 21:41:56 +01:00
owenrees 7cb7298236 Add user accounts, roles, and admin gating
- Scaffold password auth (email or username) with phx.gen.auth; no magic links
- Add role (member/moderator/admin/super_admin) and plan (free/premium) enums
- Gate /admin behind admin-or-above via plug + on_mount; hide the nav link
- Add mix accounts.promote for CLI role changes
- Disable email confirmation by confirming accounts at registration

Closes #3
2026-11-03 15:48:50 +01:00
owenrees cb35351f8c Wire Fathom into the local tablebase backend (#30)
Add Backend.Fathom: probes positions with five or fewer pieces via the Fathom NIF, maps WDL/DTZ to the trainer structs (flipping per-move perspective), generates SAN, sorts moves best-first, and falls back to Lichess on any failure. The local directory is configured with TABLEBASE_PATH; local answers are no longer cached.

Add Tablebase.normalize_wdl/1 and flip/1, plus tests for the WDL mapping, SAN, checkmate, fallback, and a cache-free end-to-end local probe.
2026-11-03 14:23:46 +01:00
owenrees 3fadd4397c Key the tablebase cache by position instead of full FEN (#28)
Add FEN.position_key/1 (board, side, castling and en-passant; clocks dropped) and have Tablebase.new/1 key cache reads and writes on it, so transpositions and clock-only differences share an entry. The Lichess category is a position property, so dropping the clocks is safe.

Update the cache docs and tests, and give the play fixture's after_first/after_losing distinct positions since they previously differed only by the halfmove clock.
2026-11-03 13:09:45 +01:00
owenrees 515196857f Route tablebase lookups through a piece-count backend seam (#27)
Add a Backend behaviour, a Lichess implementation, and a Router that picks a backend by piece count: five or fewer to the local slot, six or seven to the remote slot, and eight or more short-circuited as not covered with no network call. Both slots point at Lichess for now; the local Syzygy backend replaces the local slot when it lands.

Map :not_covered to the existing 'not covered by the tablebase' message.
2026-11-03 12:51:08 +01:00
owenrees fa71783e55 Show best moves and the optimal line in review
Closes #19
2026-11-02 21:45:06 +01:00
owenrees 1e62b13d94 Show the full game and review it after an attempt
Closes #18
2026-11-02 21:26:20 +01:00
owenrees 6d0d750ca7 Default new endgames to rating 1500
Closes #17
2026-11-02 21:26:19 +01:00
owenrees 7658450573 Full-width list/show pages and trim the admin table
List and show pages now use a wide container (max-w-7xl) while the form and
play pages keep the narrow column. The admin table drops Fen, Message, Notes,
Attempted and Solved — all already shown on the show page — so it fits
without right-overflow.

Closes #16
2026-11-02 11:04:04 +01:00
owenrees 49c3cddffb Group and filter the player endgame picker
The player-facing /endgames list now groups endgames under collapsible Key
headers and supports the same Theme/Key filtering as the admin list, showing
each endgame's Themes. The filter/group state is shared with the admin list
via ChesstrainerWeb.EndgameList.

Closes #14
2026-11-02 10:24:29 +01:00
owenrees d231860f14 Search, filter and group the admin endgames list
The admin list now groups endgames under collapsible Key headers and can be
filtered by Theme (search + tappable chips) and Key. Group counts and
filtering run in SQL, and a group's endgames are fetched only when it is
expanded, so the list stays fast as the dataset grows.

- Add Endgames.list_keys/0, list_key_groups/1 and list_endgames_for_key/2.
- Add the shared ChesstrainerWeb.EndgameList filter bar and grouped list
  component (to be reused by the player picker).
- Rework EndgameLive.Index around server-side group state.

Closes #15
2026-11-02 10:21:22 +01:00
owenrees ca65207baa Derive canonical material Key from the FEN
The Endgame key is now a canonical, colour-agnostic material signature derived
from the FEN instead of a hand-typed field: each side's pieces, strongest side
first by material value (e.g. "KQ v KR"). This fixes the seed ordering
inconsistency and guarantees the key always matches the position.

- Add Chesstrainer.Key.from_fen/1.
- Compute the key in the Endgame changeset; drop the manual input and its
  regex validation.
- Index endgames.key and derive keys in the seeds.
- Document the Key term and the decision (ADR 0003).

Closes #13
2026-11-02 09:22:01 +01:00
owenrees cd2e7f0164 Rename Tag to Theme across code and schema
Rename the endgame-label concept from Tag to Theme to match the glossary:
the context becomes Chesstrainer.Themes, the schema Chesstrainer.Themes.Theme,
and the Endgame association is :themes. The category enum
(endgame/opening/tactic) is preserved.

Rewrite the original migrations in place so themes / endgames_themes
(with theme_id) are created directly, instead of adding a transition
migration.

Closes #12
2026-11-02 09:14:17 +01:00
owenrees 645f65f2a3 Add multi-theme tagging to endgames in the admin
#11: endgames can carry multiple themes (endgame tags) chosen from a
searchable, autocompleting picker with inline creation. Tags are staged
in the form and saved atomically, so they work on both New and Edit.

Tag names are trimmed and case-insensitively unique per category. Admin
Show and Index display themes, and the seeds ship a starter vocabulary.

The theme picker has its own form: LiveView requires phx-change inputs to
live inside a form, and a separate theme form keeps Enter from
submitting the endgame form.
2026-11-01 22:22:09 +01:00
owenrees be23f1d7f8 Play draw endgames and solve on a rule draw
#10: drawn positions are graded against the draw as the target — a move
that keeps it passes, a losing move fails, and a rule draw reported by
chess.js (insufficient material, repetition, or 50-move) solves the
puzzle. The promotion cutoff now applies only to win puzzles.
2026-11-01 22:04:21 +01:00
owenrees c5989628bc End the attempt on solve or fail and count attempts
#9: solve the puzzle on checkmate or a winning promotion, fail
immediately on a losing move (revealing the missed best move), and
increment times_attempted when an attempt starts and times_solved when
it is solved. Terminal states lock play; reset starts a new attempt.
2026-11-01 22:01:19 +01:00
owenrees 248504cc58 Play the opponent's optimal defence after a passing move
#8: after a passing move, fetch the post-move position's tablebase and
reply with moves[0] (the defender's optimal defence). The client applies
and animates the reply, reports the resulting FEN, and the server fetches
that position's tablebase before resuming the user's turn.

Losing moves and terminal positions do not trigger a reply; a failed
post-reply fetch blocks the attempt. The board stays locked during the
reply round-trip so play cannot get out of sync.
2026-11-01 21:45:32 +01:00
owenrees 758629565b Implement tablebase move grading in the play view
#7: prefetch the tablebase at mount and grade a played move locally
against its moves list (optimal / winning / losing), blocking the
attempt when the tablebase is rate-limited or unavailable. The client
now reports prev_fen and promotion, and the trainer board locks after a
graded move until reset.

Fix the 429 clause ordering in the tablebase parser so the cooldown
actually fires, and use the HTTPS endpoint.

Add dialyxir to the dev/test deps.
2026-11-01 21:37:27 +01:00
owenrees 8b894465cc Implement tablebase parsing fixes and LFU cache
#5: normalise the full lichess tablebase category vocabulary (cursed-win
and blessed-loss classify as draws), return {:ok, tablebase} on a cache
hit, and unwrap the tablebase in game_from_fen.

#6: replace the LRU cache with an LFU cache storing
{key, value, frequency, last_access}, evicting the least-frequently-used
entry (LRU tie-break) and halving every frequency on a weekly sweep.
Persistence moves to a versioned v2 dets file, dropping the legacy
backup; the cache restores synchronously at startup.

Also updates stale tags enum and Play LiveView tests, and drops the
tracked legacy cache backup.
2026-11-01 21:24:51 +01:00
owenrees 7717278561 Feature/endgame play (#1)
* create endgame play section

* add cache tests

* normalise FENs - trim and fuill move to 1 on addition

* remove svg on root and allow navbar to exist across all pages

* update nav items
2026-08-11 22:38:18 +02:00