Versus mode: attack vs defend (sim, bots, server, client)

One player defends, the other plays the Internet: earns credits over time and
spends them on packets, upgrades and sabotage. Two rounds with swapped roles,
timed (the faster fall wins). Built end to end, NOT yet QA'd or balanced.

Sim (public/js/sim.js, TD.VS table): attacker economy (income, lifetime bonus,
leak payback), spawn / stage / launch convoys, 7 tech tracks, abilities (EMP,
decoy, surge, DDoS), defender tools (scan, patch, repair, quarantine), role-checked
commands, round result, role-private state. Co-op/solo unchanged (24 scripted
games replay identically).

Bots (public/js/bot.js, tools/versus.js): defender (maze + turrets) and attacker
(budgeted spender reading the tower mix); headless bot-vs-bot runner.

Server (server/versus.js, protocol doc): versus rooms with lobby / countdown /
playing / roundover / matchover, role stamping, cross-role refusal, role-private
priv messages at 4 Hz, computer seat, freeze/forfeit rules, no pause/speed.

Client (public/js/versus.js): setup dialog (role, length, vs computer or online),
countdown, defender tools row, attacker unit shop with convoy + launch, tech and
abilities, gateway choice, round/match screens, sounds, phone-first dock rows.

Status and next steps (balance: the attacker currently wins ~100% of bot rounds;
QA with two browsers; layout matrix rerun) are in docs/NEXT.md and docs/VERSUS.md.
The full planned workflow incl. balance/QA prompts is in docs/workflows/versus-full.js.

253 unit tests pass; one smoke e2e run; no other e2e per the user's request.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tb8pW2KAN4oFkh1aK1BfD8
This commit is contained in:
Claude 2026-10-11 10:22:29 +00:00
parent b4ae6cb57d
commit 2098d59c1f
23 changed files with 6166 additions and 90 deletions

View file

@ -23,7 +23,7 @@ const PID_RE = /^[A-Za-z0-9_-]{8,40}$/;
const ROOM_RE = /^[A-Za-z0-9]{4}$/;
const NAME_STRIP = new RegExp('[\\u0000-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2066-\\u2069\\ufeff]', 'g');
// Close codes for a failed join (the 'error' message carries the machine-readable code).
const JOIN_CLOSE = { bad_request: 4400, bad_map: 4400, bad_difficulty: 4400, no_room: 4404, full: 4409, too_many_rooms: 4503 };
const JOIN_CLOSE = { bad_request: 4400, bad_map: 4400, bad_difficulty: 4400, bad_mode: 4400, bad_limit: 4400, bad_rounds: 4400, no_room: 4404, full: 4409, too_many_rooms: 4503 };
const hasOwn = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
const noop = () => {};
@ -42,6 +42,8 @@ async function createServer(userOpts) {
graceMs: 60000, snapshotHz: 20, maxRooms: 50,
trustProxy: 'auto', trustedProxies: null, allowedOrigins: [], maxConnPerIp: 40, pingMs: 25000, log: defaultLog,
joinTimeoutMs: 10000, // not in the protocol's option list: lets tests shorten the 10 s join window
vsCountdownMs: 3000, // versus: the pause before each round (tests shorten it)
vsNextMs: 30000, // versus: how long a round result waits for everybody's `next` before the next round starts anyway
};
for (const k of Object.keys(userOpts || {})) if (userOpts[k] !== undefined) o[k] = userOpts[k];
o.trustProxy = parseTrustProxy(o.trustProxy);
@ -50,7 +52,7 @@ async function createServer(userOpts) {
o.snapshotHz = Math.min(60, Math.max(1, Number(o.snapshotHz) || 20));
const log = typeof o.log === 'function' ? o.log : noop;
const manager = new RoomManager({ maxRooms: o.maxRooms, graceMs: o.graceMs, snapshotHz: o.snapshotHz, log });
const manager = new RoomManager({ maxRooms: o.maxRooms, graceMs: o.graceMs, snapshotHz: o.snapshotHz, log, vsCountdownMs: o.vsCountdownMs, vsNextMs: o.vsNextMs });
const serveStatic = createStaticHandler(o.publicDir);
const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_PAYLOAD, perMessageDeflate: false });
wss.on('error', (err) => log('websocket server error: ' + (err && err.message)));
@ -207,7 +209,20 @@ async function createServer(userOpts) {
if (!TD.isDifficulty(msg.difficulty)) return failJoin(ctx, 'bad_difficulty', 'unknown difficulty');
difficulty = msg.difficulty;
}
room = manager.create(map, difficulty);
// Game mode: only a creating join carries it (like the map). Absent or null is co-op; versus also takes the round limit and the number of rounds.
let vsOpts = null;
if (msg.mode !== undefined && msg.mode !== null) {
if (msg.mode !== 'coop' && msg.mode !== 'versus') return failJoin(ctx, 'bad_mode', 'unknown mode');
if (msg.mode === 'versus') {
vsOpts = { limit: TD.VS.DEFAULT_LIMIT, rounds: TD.VS.ROUNDS };
if (msg.limit !== undefined && msg.limit !== null) {
if (typeof msg.limit !== 'number' || !TD.VS.LIMITS.includes(msg.limit)) return failJoin(ctx, 'bad_limit', 'unknown round limit');
vsOpts.limit = msg.limit;
}
if (msg.rounds !== undefined && msg.rounds !== null && msg.rounds !== TD.VS.ROUNDS) return failJoin(ctx, 'bad_rounds', 'unsupported number of rounds');
}
}
room = manager.create(map, difficulty, vsOpts);
if (!room) return failJoin(ctx, 'too_many_rooms', 'the server has reached its room limit');
why = 'created';
} else {
@ -222,6 +237,7 @@ async function createServer(userOpts) {
if (ctx.joinTimer) { clearTimeout(ctx.joinTimer); ctx.joinTimer = null; }
send(ctx, room.joinedMsg(r.player, r.resumed));
room.broadcastPlayers(ctx);
room.afterJoin(r.player, r.resumed, ctx);
room.log(name + ' ' + (r.resumed ? 'resumed' : (why || 'joined')) + ' as slot ' + r.player.slot + (r.player.host ? ' (host)' : '') + ' from ' + ctx.ip);
}