mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-08-18 00:28:14 +00:00
Add live game stats endpoint to the admin bot API (#4399)
## 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>
This commit is contained in:
@@ -94,6 +94,7 @@ export type GameConfig = z.infer<typeof GameConfigSchema>;
|
||||
|
||||
export type ClientMessage =
|
||||
| ClientSendWinnerMessage
|
||||
| ClientSendLiveStatsMessage
|
||||
| ClientPingMessage
|
||||
| ClientIntentMessage
|
||||
| ClientJoinMessage
|
||||
@@ -122,6 +123,11 @@ export type ServerLobbyInfoMessage = z.infer<
|
||||
typeof ServerLobbyInfoMessageSchema
|
||||
>;
|
||||
export type ClientSendWinnerMessage = z.infer<typeof ClientSendWinnerSchema>;
|
||||
export type ClientSendLiveStatsMessage = z.infer<
|
||||
typeof ClientSendLiveStatsSchema
|
||||
>;
|
||||
export type PlayerLiveStats = z.infer<typeof PlayerLiveStatsSchema>;
|
||||
export type LiveStats = z.infer<typeof LiveStatsSchema>;
|
||||
export type ClientPingMessage = z.infer<typeof ClientPingMessageSchema>;
|
||||
export type ClientIntentMessage = z.infer<typeof ClientIntentMessageSchema>;
|
||||
export type ClientJoinMessage = z.infer<typeof ClientJoinMessageSchema>;
|
||||
@@ -279,6 +285,10 @@ export const GameConfigSchema = z.object({
|
||||
disableNavMesh: z.boolean().optional(),
|
||||
disableAlliances: z.boolean().nullable().optional(),
|
||||
disableClanTags: z.boolean().optional(),
|
||||
// Opt-in live game stats reporting for the admin bot. Off by default and has
|
||||
// no UI — the admin bot sets it when creating tournament games, since it adds
|
||||
// per-client traffic. See LiveStatsController / GameServer.handleLiveStats.
|
||||
liveStatsEnabled: z.boolean().optional(),
|
||||
anonymizeNames: z.boolean().optional(),
|
||||
// While anonymizeNames is on, clientIDs the host has granted real-name
|
||||
// visibility to (e.g. casters / observers). Everyone else stays anonymized.
|
||||
@@ -693,6 +703,32 @@ export const ClientSendWinnerSchema = z.object({
|
||||
allPlayersStats: AllPlayersStatsSchema,
|
||||
});
|
||||
|
||||
// A live snapshot of one human player at a given turn. Only deterministic sim
|
||||
// values are included so in-sync clients produce an identical snapshot that can
|
||||
// be agreed on by majority vote. gold is a decimal string because it is a
|
||||
// bigint in the engine.
|
||||
export const PlayerLiveStatsSchema = z.object({
|
||||
clientID: ID,
|
||||
tilesOwned: z.number().int().nonnegative(),
|
||||
troops: z.number(),
|
||||
gold: z.string(),
|
||||
isAlive: z.boolean(),
|
||||
team: z.string().nullable(),
|
||||
});
|
||||
|
||||
// A full live snapshot of a running game at a given turn. Reported by clients
|
||||
// (which run the sim) so the server can answer "what's happening" queries for
|
||||
// the admin bot.
|
||||
export const LiveStatsSchema = z.object({
|
||||
turn: z.number().int().nonnegative(),
|
||||
players: PlayerLiveStatsSchema.array(),
|
||||
});
|
||||
|
||||
export const ClientSendLiveStatsSchema = z.object({
|
||||
type: z.literal("live_stats"),
|
||||
stats: LiveStatsSchema,
|
||||
});
|
||||
|
||||
export const ClientHashSchema = z.object({
|
||||
type: z.literal("hash"),
|
||||
hash: z.number(),
|
||||
@@ -737,6 +773,7 @@ export const ClientRejoinMessageSchema = z.object({
|
||||
|
||||
export const ClientMessageSchema = z.discriminatedUnion("type", [
|
||||
ClientSendWinnerSchema,
|
||||
ClientSendLiveStatsSchema,
|
||||
ClientPingMessageSchema,
|
||||
ClientIntentMessageSchema,
|
||||
ClientJoinMessageSchema,
|
||||
|
||||
Reference in New Issue
Block a user