/** * Projection helpers for Ordnance Disposal. Free-for-all has no hidden * information, so every viewer receives the same board; a spectator passes * `yourSeat: null` and simply cannot submit input. The grid is emitted as a * compact numeric array (0 empty, 1 solid, 2 crate) to keep snapshots small. */ import { GRID_H, GRID_W } from "./constants"; import type { BomperatorState, BomperatorView, CellType, PlayerView } from "./types"; const CELL_CODE: Record = { empty: 0, solid: 1, crate: 2 }; function encodeGrid(grid: readonly CellType[]): number[] { const out = new Array(grid.length); for (let i = 0; i < grid.length; i += 1) out[i] = CELL_CODE[grid[i]]; return out; } export interface BuildViewOptions { readonly matchId: string; readonly name: string | null; readonly version: number; /** Seat of the requesting user, or null for a spectator. */ readonly yourSeat: number | null; } /** Build the full snapshot the registry serves to one poller. */ export function buildBomperatorView( state: BomperatorState, options: BuildViewOptions, ): BomperatorView { const players: PlayerView[] = state.players.map((p) => ({ seat: p.seat, userId: p.userId, name: p.name, x: round3(p.x), y: round3(p.y), dir: p.dir, alive: p.alive, invulnerable: p.invulnerableTicks > 0, bombsMax: p.bombsMax, bombsActive: countBombs(state, p.seat), range: p.range, speed: round3(p.speed), kick: p.kick, kills: p.kills, roundsWon: p.roundsWon, connected: p.connected, })); return { matchId: options.matchId, name: options.name, version: options.version, yourSeat: options.yourSeat, phase: state.phase, roundTarget: state.roundTarget, roundIndex: state.roundIndex, gridW: GRID_W, gridH: GRID_H, grid: encodeGrid(state.grid), bombs: state.bombs, flames: state.flames, drops: state.drops, players, aliveCount: state.players.filter((p) => p.alive).length, countdownTicks: state.countdownTicks, roundTimerTicks: Math.max(0, state.roundTimerTicks), suddenDeath: state.suddenDeath, roundWinnerSeat: state.roundWinnerSeat, matchWinnerSeat: state.matchWinnerSeat, endReason: state.endReason, events: state.events, lastEventId: state.lastEventId, }; } function countBombs(state: BomperatorState, seat: number): number { let n = 0; for (const b of state.bombs) if (b.ownerSeat === seat) n += 1; return n; } function round3(value: number): number { return Math.round(value * 1000) / 1000; }