1
0
Fork 0
polaris-task-force/src/lib/minigames/bomperator/views.ts
Z8MB1E bfb5cea3c1 feat(minigames): add Ordnance Disposal (bomperator) multiplayer drill
Add a 2-8 player free-for-all Bomberman-style charge-clearing drill at
/minigames/bomperator, category multiplayer. Pure engine plus a single-instance
in-memory match registry, mirroring the call-for-fire-versus split but upgraded
from turn-based to a fixed ~10 Hz tick loop.

Engine (src/lib/minigames/bomperator): seeded RNG via state.rngState/gridSeed;
grid is 15x13 with border, even/even pillars and seeded crates; classic pickups
bomb/range/speed/kick. Movement is grid-locked with sub-cell interpolation,
charges fuse about 2.5s, cross-flames stop at walls, crates are destroyed and
charges chain-detonate and kick-push. Round ends at one alive; sudden death
spawns random explosions after 150s with a draw guard.

Registry (registry.ts): one setInterval per live match started on startMatch and
cleared on complete/dissolve; globalThis-anchored map; holds seats across a
reconnect window then eliminates; below two active seats it dissolves. On a
manual end, broadcasts a bomperator-terminated event so remaining players see a
toast, then awards rewards via stats.ts (minigame:bomperator, profile group
bomperator: matchesPlayed/matchWins/roundsWon/kills/xpEarned).

Clients POST rate-limited inputs and poll fetchBomperatorMatch; the SSE bus pushes
snapshots with a guarded slow-poll fallback. Registry row ships as upcoming so the
page renders the coming-soon shell until an admin flips it active. Adds the
migration for the stats group, the minigame:bomperator event type, XP
aggregation on the profile, generated types, and real-time event wiring.
2026-10-16 00:32:52 -04:00

86 lines
2.5 KiB
TypeScript

/**
* 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<CellType, number> = { empty: 0, solid: 1, crate: 2 };
function encodeGrid(grid: readonly CellType[]): number[] {
const out = new Array<number>(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;
}