Skip to content

Store finished games and show the most popular moves - #3

Merged
devin-ai-integration[bot] merged 6 commits into
mainfrom
devin/1795083333-game-archive
Nov 19, 2026
Merged

devin-ai-integration[bot] merged 6 commits into
mainfrom
devin/1795083333-game-archive

Conversation

@Coobyk

@Coobyk Coobyk commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds a server-side archive of finished games so the site can report the most popular moves, results and endings per variant. It uses a new SQLite-backed Durable Object (GameArchive, one global instance, migration v2), not PostHog: the data lives next to the existing GameRoom and needs no new account or secret.

Ingestion

  • Online games: GameRoom.archive() runs when a game ends by a move, a resignation or a timeout, and sends the entry straight to the archive.
  • Local and bot games: watchFinishedGames() (src/lib/archiveWatcher.ts, started in App) is a store subscription. It fires only on the step from a running game to its finished continuation, so reloads, view switches and finished games opened from a link are never sent. It posts POST /api/archive with { variant, moves: MoveRequest[], ending, source }. The Worker replays every move from createInitialState(variant) through resolveMove (the legality check pulled out of GameRoom into src/lib/replay.ts, so both paths use the same rules). It keeps the game only if the moves end it, or if the client says resign/timeout and the moves left the game open. Each IP can post 20 games per 10 minutes.
  • Nothing that identifies a player is stored. Players can switch sharing off in the panel (goonchess:share-games).

Storage (worker/archive.ts)

  • games: one row per game (variant key, source, winner index, status, SAN moves).
  • variants: running totals per variant.
  • position_moves: (variant_key, position, move) → games, wins, draws, for the first 24 plies (EXPLORER_DEPTH).
  • variantKey = sha256 of packVariant(variant) with no random seed. positionHash = sha256 of positionKey(state), plus the kinds of any rolled Random Chess pieces.

Read API: no-store. The panel refetches whenever this page stores a game.

  • GET /api/stats/moves?variant=&position=
  • GET /api/stats/summary?variant=
  • GET /api/stats/summary

Other changes

  • BOARD_LIGHT/BOARD_DARK moved to src/lib/boardShades.ts, so the Worker can import variantCodec without DOM types.
  • allowCreate is now built from a generic rateLimiter.

Testing

  • npx tsgo -b, npm test (new tests in src/lib/archive.test.ts), oxlint and npm run build all pass, and wrangler deploy --dry-run shows the ARCHIVE binding.
  • Local wrangler dev smoke test: posted Fool's Mate twice, then read /api/stats/moves and /api/stats/summary, which returned the expected counts. A truncated, unfinished game was rejected with not_finished.

GoonBot opened this PR for Coobyk
Implemented in session Add game storage backend with move analytics
Review with Devin · Open in Devin Desktop


@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 4 findings. Devin automatically fixed all 4 findings.

Fixed by Devin (4)
  • Missing archives for mobile games — Submission moved out of the panel into a store subscription (archiveWatcher) started in App, so it runs on every layout and view.
  • Incorrect finished-game deduplication — Games are sent only on the transition from a running game to its finished continuation, so neither remounts nor same-shaped restarts affect dedup.
  • Merged Random Chess explorer positions — positionHash appends the rolled kinds' names in board order when any rolled piece is on the board.
  • Variant totals frozen across completed games — Summary and explorer refetch whenever a game is stored; edge cache removed.

View findings in Devin Review · Follow Devin's work

Devin Review

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Browser E2E test passed. Setup: the built SPA, a local Wrangler Worker and a fresh SQLite Durable Object.

  • Local games: Fool's Mate showed f3 at 100% with the correct Black/checkmate totals, no reload needed. A second mate split e4 and f3 50/50, and playing f3 then showed e5 at 100%.
  • Sharing switch: with it off, a finished game was not counted, and the switch stayed off after reload.
  • Narrow layout (960px): a game finished with Stats closed was counted once, and not sent again after Maker → Play or a reload.
  • Bot game: a 62-ply Bot vs Bot Racing Kings game was stored once, and the explorer stopped at ply 24 as intended.
  • Online resignation: both open rooms refreshed their totals within about 2s, no reload needed.
Empty archive Two games, sharing off
Narrow Stats after reload Bot game summary
Online room after resignation Fresh Play tab

Not tested: the hosted deployment, and games ending by timeout or draw.

Written by Devin

@devin-ai-integration
devin-ai-integration Bot merged commit 48eaf7c into main Nov 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant