difficulty and replay panels in the left column

Top panel: Expert / Intermediate / Beginner. Bottom: that difficulty's saved
games, best first, click to play one on the board.

── the selection lives in the URL ──

`/minesweeper/expert`, not a channel and not panel state. It survives a
refresh, it can be linked, and the two panels never pass anything between
themselves — both read `useParams`. The plugin section route already existed;
nothing had used it.

The side effect hangs off the URL too: an effect on the selected level posts
the difficulty, so arriving at `/minesweeper/expert` by click, deep link or
back button all leave the game on expert. `POST /difficulty` verifies and is
idempotent, so a repeat changes nothing. One `<Navigate replace>` after the
hooks canonicalises both the bare route and a bogus section, and it waits for
`/status` so the URL lands on what the game IS rather than a guess — which is
why `difficulty` is now part of that payload.

Levels are NavLinks so cmd-click works and the active style comes from the
router. Replay rows are buttons, because they do not navigate anywhere, they
make the game do something. That is the line navigation-audit.md draws.

── two things on the server ──

`GET /replays` reads the directory rather than any index, because the filename
IS the metadata — Minesweeper X names a recording `<Lvl> <secs> - <date>.mvr`
and keeps no catalogue. A name that does not parse is skipped rather than
guessed at. Ordering is the server's: a loss is never "best" however short it
was, so wins rank by time and losses fall to the end, still watchable.

`POST /replay` drives Extras > Play Recording... and TYPES the filename into a
file dialog, which is why it only accepts a name `isKnownReplay` just matched
against the directory. An unchecked client string reaching `xdotool type` can
carry a path, a wildcard, or a newline that submits something else. Verified by
the dialog closing, not by the window size — a beginner replay leaves the
window beginner-sized, which is also what nothing-happened looks like.

Verified end to end against the running session: the expert list comes back
ranked 58.63 through 82.06 with losses after, playing 58.63 starts on the
board, and `{"file":"../../../etc/passwd"}` is a 404.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Officer-Session: de3ed4e6-f88a-429c-94e6-a760336ba83b
This commit is contained in:
2026-08-23 23:23:44 +00:00
co-authored by Claude Opus 5
parent b94c1a6557
commit 5d62668883
8 changed files with 343 additions and 12 deletions
+61
View File
@@ -0,0 +1,61 @@
import { readdirSync } from 'node:fs';
import { GAME_DIR } from './paths';
import type { Difficulty } from './menu';
/**
* The saved games, read off disk rather than out of the game.
*
* Minesweeper X names a recording `<Lvl> <seconds> - <date> <time>[ (Lost)].mvr` and keeps no index, so
* the filename IS the metadata. That is fine here — it is the game's own format, not one we chose, and
* parsing it beats the alternative of a sidecar database that has to be kept in step with a directory
* the game writes to behind our back.
*
* A name that does not parse is skipped rather than guessed at. Something the user dropped in the folder
* by hand should not appear in a ranked list with an invented time.
*/
const LEVELS: Record<string, Difficulty> = { Beg: 'beginner', Int: 'intermediate', Exp: 'expert' };
/** `Exp 58.63 - Feb 4 2026 00.16.29 AM.mvr`, with an optional ` (Lost)` before the extension. */
const NAME = /^(Beg|Int|Exp) (\d+(?:\.\d+)?) - (.+?)( \(Lost\))?\.mvr$/;
export type Replay = {
/** The filename, which is also the id. Never built by the client — see the guard in `index.ts`. */
file: string;
level: Difficulty;
/** Seconds, as the game recorded them. */
time: number;
/** The game's own date string, kept verbatim rather than parsed into something with a timezone. */
played: string;
lost: boolean;
};
export function listReplays(level?: Difficulty): Replay[] {
let names: string[];
try {
names = readdirSync(GAME_DIR);
} catch {
return []; // no prefix yet, which is the same answer as no replays
}
const replays: Replay[] = [];
for (const file of names) {
const match = NAME.exec(file);
if (!match) continue;
const parsed = LEVELS[match[1]!];
if (!parsed || (level && parsed !== level)) continue;
replays.push({ file, level: parsed, time: Number(match[2]), played: match[3]!, lost: !!match[4] });
}
// Best first, and a loss is never "best" however short it was — a 0.32s loss is a misclick, not a
// record. So wins rank by time and losses fall to the end, most recent first, still watchable.
return replays.sort((a, b) => {
if (a.lost !== b.lost) return a.lost ? 1 : -1;
if (a.lost) return b.played.localeCompare(a.played);
return a.time - b.time;
});
}
/** Whether a name is a replay this directory actually holds. The only way a client-supplied name is used. */
export const isKnownReplay = (file: unknown): file is string =>
typeof file === 'string' && listReplays().some((r) => r.file === file);