mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-01 17:23:33 +00:00
181368f962
## Summary The game simulation runs **client-side**, so the server can't directly see what's happening in a running game. This adds a way for the admin bot to observe a live game: clients report a live stats snapshot every ~10s, the server reaches consensus on it (reusing the winner's vote mechanism), and a new admin-bot endpoint serves it. ## How it works 1. **`LiveStatsController`** (client) emits a snapshot every **100 turns** (~10s at 100ms/turn) — only deterministic sim values, with players sorted by clientID, so in-sync clients produce an identical payload. 2. The snapshot is sent as a new **`live_stats`** wire message wrapping a `LiveStats` object (`turn` + per-human-player `tilesOwned`/`troops`/`gold`/`isAlive`/`team`). 3. **`GameServer.handleLiveStats`** tallies a per-turn **IP-weighted majority vote** — the same consensus the winner uses — and keeps the latest agreed snapshot. 4. **`GET /api/adminbot/game/:id/stats`** returns it, enriched with usernames the server already holds. `liveStats` is `null` until the first consensus. The winner's vote tally was extracted into a small reusable **`VoteRound`** (`src/server/VoteTally.ts`) and is now used for both winner and live-stats consensus. Names are deliberately **excluded** from the voted payload (they vary per client under name anonymization, which would break exact-match consensus); the server joins `clientID → username` instead. ## Changes - `src/server/VoteTally.ts` *(new)* — reusable IP-weighted `VoteRound` - `src/core/Schemas.ts` — `PlayerLiveStatsSchema`, `LiveStatsSchema`, `ClientSendLiveStatsSchema` + unions - `src/client/controllers/LiveStatsController.ts` *(new)* — per-100-turn snapshot reporter - `src/client/Transport.ts` — `SendLiveStatsEvent` + sender - `src/client/hud/GameRenderer.ts` — register the controller - `src/server/GameServer.ts` — refactor winner onto `VoteRound`; add live-stats consensus + `liveStats()` accessor - `src/server/AdminBotRoutes.ts` — `GET …/stats` endpoint ## Testing - **Unit:** `tests/server/VoteTally.test.ts` (majority/dedup/ties), `tests/server/LiveStats.test.ts` (consensus, disagreement, per-client dedup, stale-turn rejection, turn advance, out-of-sync exclusion, + endpoint 200/404/400). Full suite green (`npm test`), typecheck + lint clean. - **Manual e2e** against the dev server: created an admin-bot game, joined it in a browser, force-started via `toggle_game_start_timer`, and confirmed `GET …/stats` returned the consensus snapshot with username enrichment and an advancing `turn`. Also verified wrong-worker → 400 and missing-key → 401. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
35 lines
1.4 KiB
TypeScript
35 lines
1.4 KiB
TypeScript
// IP-weighted single-round vote used to reach consensus on a value that the
|
|
// authoritative simulation only exists for on the clients (which run the game),
|
|
// not the server. Clients each vote for a candidate value; a candidate wins once
|
|
// a strict majority of the electorate's unique IPs back it.
|
|
//
|
|
// Used both for end-of-game winner consensus and for periodic running-stats
|
|
// consensus (see GameServer).
|
|
export class VoteRound<T> {
|
|
private candidates = new Map<string, { value: T; ips: Set<string> }>();
|
|
|
|
// Records a vote for `value` (identified by the stable string `key`) from
|
|
// `ip`. Repeat votes from the same IP for the same candidate are idempotent.
|
|
// Returns the candidate's unique-IP vote count after the vote.
|
|
add(key: string, value: T, ip: string): number {
|
|
let candidate = this.candidates.get(key);
|
|
if (candidate === undefined) {
|
|
candidate = { value, ips: new Set() };
|
|
this.candidates.set(key, candidate);
|
|
}
|
|
candidate.ips.add(ip);
|
|
return candidate.ips.size;
|
|
}
|
|
|
|
// Returns the winning value once some candidate holds a strict majority of
|
|
// `totalUniqueIPs` (votes * 2 >= total), else null.
|
|
result(totalUniqueIPs: number): { value: T; votes: number } | null {
|
|
for (const candidate of this.candidates.values()) {
|
|
if (candidate.ips.size * 2 >= totalUniqueIPs) {
|
|
return { value: candidate.value, votes: candidate.ips.size };
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|