The tally: what the ledgers add up to, in the about panel

Ported from the game kit (7aa0553). GET /api/tally scans the ledgers,
replays each duel, and returns the counts, cached a minute; the duel's
own lines come from tallyOf on the final state (wins by slaying or by
surrender, draws, wizards slain and yielded, stopped moments). The hall
fetches it when the about panel opens.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0141G6xqLeNRYEtviLWSB5Up
This commit is contained in:
Eric WagonerandClaude Fable 5.1 committed 2026-09-23 18:36:32 -04:00
1 parent 66dd3bc10e
commit 7c60d19ad3
6 files changed
+205 -1

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
// The tally: what the ledgers add up to, for the hall's about panel. Read
// from the files on demand and cached for a minute, so a curious visitor
// costs nothing and a busy hall costs one scan a minute. The counts are the
// kit's; the duel's own lines come from tallyOf.
import { resolveTurn } from '../../src/lib/game/resolve';
import { createGame, type GameState, type WizardId } from '../../src/lib/game/state';
import { tallyOf } from '../../src/lib/game/tally';
import type { LedgerLine, Store } from './store';
export interface Tally {
tablesOpened: number;
gamesBegun: number;
gamesFinished: number;
/** Distinct names of people who have taken a seat; a bot's name is not counted. */
namesSeated: number;
turnsPlayed: number;
/** Minutes at the table: gaps under ten minutes between one move and the next, summed. */
minutesAtTable: number;
longestGameTurns: number;
gamesWithBot: number;
talkLines: number;
firstGameAt: string | null;
/** The duel's own lines, summed over finished duels. */
byGame: Record<string, number>;
}
const GAP_LIMIT_MS = 10 * 60 * 1000;
const CACHE_MS = 60 * 1000;
export class Tallies {
private cached: { at: number; tally: Tally } | null = null;
constructor(private store: Store) {}
get(): Tally {
if (this.cached && Date.now() - this.cached.at < CACHE_MS) return this.cached.tally;
const tally = this.count();
this.cached = { at: Date.now(), tally };
return tally;
}
private count(): Tally {
const t: Tally = { tablesOpened: 0, gamesBegun: 0, gamesFinished: 0, namesSeated: 0, turnsPlayed: 0, minutesAtTable: 0, longestGameTurns: 0, gamesWithBot: 0, talkLines: 0, firstGameAt: null, byGame: {} };
const names = new Set<string>();
let firstStart = Infinity;
for (const id of this.store.roomIds()) {
const lines = this.store.read(id);
const first = lines[0];
if (first?.t !== 'room') continue;
t.tablesOpened += 1;
const seats: { id: WizardId; name: string; bot: boolean }[] = [];
let state: GameState | null = null;
let turns = 0;
let lastMoveAt = 0;
let hasBot = false;
let over: LedgerLine | null = null;
for (const line of lines) {
if (line.t === 'seat') {
seats.push(line);
if (line.bot) hasBot = true;
else names.add(line.name.toLowerCase());
} else if (line.t === 'unseat') seats.splice(seats.findIndex((s) => s.id === line.id), 1);
else if (line.t === 'start') {
t.gamesBegun += 1;
if (hasBot) t.gamesWithBot += 1;
if (first.createdAt < firstStart) firstStart = first.createdAt;
try {
state = createGame(Object.fromEntries(seats.map((s) => [s.id, s.name])), line.seed, line.rules ?? 1);
} catch {
state = null;
}
} else if (line.t === 'input') {
if (lastMoveAt && line.at - lastMoveAt < GAP_LIMIT_MS) t.minutesAtTable += (line.at - lastMoveAt) / 60000;
lastMoveAt = line.at;
} else if (line.t === 'turn') {
turns += 1;
if (lastMoveAt && line.at - lastMoveAt < GAP_LIMIT_MS) t.minutesAtTable += (line.at - lastMoveAt) / 60000;
lastMoveAt = line.at;
if (state) {
try {
state = resolveTurn(state, line.inputs);
} catch {
state = null;
}
}
} else if (line.t === 'chat') t.talkLines += 1;
else if (line.t === 'over') over = line;
}
t.turnsPlayed += turns;
if (turns > t.longestGameTurns) t.longestGameTurns = turns;
if (over) {
t.gamesFinished += 1;
if (state) for (const [k, v] of Object.entries(tallyOf(state))) t.byGame[k] = (t.byGame[k] ?? 0) + v;
}
}
t.namesSeated = names.size;
t.minutesAtTable = Math.round(t.minutesAtTable);
t.firstGameAt = Number.isFinite(firstStart) ? new Date(firstStart).toISOString() : null;
return t;
}
}