1One authority, many rooms
The match is computed in exactly one place: the server. Each game lives in a Room — two player slots, a waiting queue, any number of TV spectators, and one authoritative simulation.
const TICK_MS = 33; // ~30 Hz: both the sim step AND the broadcast rate this.players = [null, null]; // the two seats this.waiting = []; // queue: spectate until a seat frees this.tvs = new Set(); // passive viewers this.sim = null; // the authoritative simulation
The server (server/index.js) only routes WebSocket messages to the right room: host creates a room, join places a participant in it, and everything else (aim, ready, shell…) is routed to the room's methods. No game logic lives in the network layer.
2The protocol: intents up, snapshots down
The two directions of the WebSocket carry very different things. Phones send small intents; the server broadcasts a full game snapshot about 30 times per second.
Each tick, the room advances the simulation by a fixed step, collects the events that occurred, then broadcasts the snapshot to every subscriber of the room:
step() { this.sim.tick(DT); const events = this.sim.drainEvents(); this.broadcast({ t: 'snapshot', state: this.sim.snapshot(), events }); }
3The same simulation on every machine
The server runs under Node, with no browser or canvas. It can still compute the physics because all the logic lives in src/sim/, in pure JavaScript. The server imports the same file as the clients:
import Simulation, { PHASE } from '../src/sim/Simulation.js'; import { BIOMES } from '../src/config/biomes.js';
That's the payoff of chapter 1's architecture decision: the same physics runs on the server (the judge) and stays testable under Node. Nobody rewrites the trajectories “for the network”.
// the spectator TV never gets the players' chosen aim values shown; // only ready flags, and — once fired — the resulting projectiles. projectiles: this.projectiles .filter((p) => p.alive) .map((p) => ({ id: p.id, x: Math.round(p.x), y: Math.round(p.y), owner: p.owner })),
4Seats, queue and leaving
A room is alive: people arrive, leave, take over. The room handles these transitions without breaking the match. An arriving participant takes a free seat, otherwise they join the queue as a spectator.
const slot = this.players.findIndex((p) => p === null); if (slot !== -1) { this.assignPlayer(slot, socket, name); /* player */ } else { this.waiting.push({ socket, name }); /* spectator in queue */ }
When a player frees their seat (voluntary leave or disconnect), the front of the queue takes over (promoteInto). And if no one fills both seats mid-match, the room returns to the lobby: the TV shows the code and QR again.
this.promoteInto(i); // a waiting spectator takes the seat if (this.sim && !this.bothFilled()) this.resetToLobby();
Room object makes these transitions readable and testable.