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:
parent
b4ae6cb57d
commit
2098d59c1f
23 changed files with 6166 additions and 90 deletions
|
|
@ -15,6 +15,24 @@
|
|||
* ctrl.difficulty the game's difficulty id (TD.DIFFICULTIES); `newGame(map, difficulty)` keeps the current one when none is given
|
||||
* ctrl.preview(x,y,routes) would a block there be legal, and what does it do to the route?
|
||||
* events ctrl.on('events', list) | 'reject' | 'mark' | 'players' | 'status' | 'joined' | 'newgame' | 'fatal' | 'rtt'
|
||||
*
|
||||
* VERSUS (attack vs defend, docs/VERSUS.md and docs/PROTOCOL.md "Versus"). Both controllers speak the same language to the UI, so the UI code
|
||||
* does not care whether the other seat is a person on the server or the computer in this tab:
|
||||
* ctrl.mode 'solo' (LocalController), 'coop' or 'versus' (what the room was created as; RemoteController learns it from the server's state)
|
||||
* ctrl.vs the newest match message { phase, round, rounds, roles:{slot:'attacker'|'defender'}, random, bot, limit, ready, frozen, countdown?, next?,
|
||||
* result?, history?, scores?, winner?, forfeit?, rematch? }, or null (not versus); event 'vs' fires on every change.
|
||||
* phase: lobby | countdown | playing | roundover | matchover (a LocalController starts in the countdown: there is no lobby)
|
||||
* ctrl.priv the newest role-private data { role, ...privateState }: attacker { credits, income, tech, staged, cd, active }, defender { cd };
|
||||
* event 'priv'. Compare priv.role with ctrl.role after a role swap (the first priv of a round replaces the old one).
|
||||
* ctrl.role my role in the current round ('attacker' | 'defender') or null
|
||||
* ctrl.view.vs the SHARED round block { t, lim, st } (round clock, limit, staged convoy size) or null; ctrl.view.mode as ctrl.mode
|
||||
* view.towers[].empT seconds of EMP left on a tower (0 when none), versus only
|
||||
* ctrl.send(cmd) also the versus commands: spawn | stage | launch | tech | ability | tool (see the protocol); the role is the controller's, never the caller's
|
||||
* ctrl.vsSend(c, v, extra) lobby / match control: 'role' | 'ready' | 'bot' | 'start' | 'next' | 'rematch'; wrappers vsRole(v) vsReady(v) vsBot(v, skill)
|
||||
* vsStart() vsNext() vsRematch(). A LocalController has no lobby: role / ready / bot / start are refused ('phase'),
|
||||
* `next` starts round 2 and `rematch` a new match with the first roles swapped.
|
||||
* ctrl.ctl(c, v) does nothing in versus (no pause, no speed); `new LocalController({ map, mode:'versus', role:'attacker'|'defender'|'random', limit, skill })`
|
||||
* plays against NWBot (js/bot.js must be loaded), `new RemoteController({ ..., mode:'versus', limit })` creates a versus room.
|
||||
*/
|
||||
(function (root) {
|
||||
'use strict';
|
||||
|
|
@ -42,6 +60,32 @@
|
|||
return c;
|
||||
}
|
||||
|
||||
// ---- match rules (the same rules as server/versus.js: test/controllers.test.js feeds both the same rounds) ----
|
||||
const vsOpposite = (r) => (r === 'attacker' ? 'defender' : 'attacker');
|
||||
const vsSwap = (roles) => ({ 0: vsOpposite(roles[0]), 1: vsOpposite(roles[1]) });
|
||||
// history entry: { round, roles:{0,1}, result:{ winner, t, integrityLost, attacker:slot, defender:slot, forfeit? } }
|
||||
function matchScores(history, names) {
|
||||
return [0, 1].map((slot) => {
|
||||
let fellAt = null, lost = 0;
|
||||
for (const h of history) {
|
||||
if (h.roles[slot] !== 'attacker') continue;
|
||||
lost += h.result.integrityLost || 0;
|
||||
if (h.result.winner === 'attacker' && !h.result.forfeit && (fellAt === null || h.result.t < fellAt)) fellAt = h.result.t;
|
||||
}
|
||||
return { slot, name: names[slot], fellAt, integrityLost: lost };
|
||||
});
|
||||
}
|
||||
function matchWinner(scores) {
|
||||
const a = scores[0], b = scores[1];
|
||||
if (a.fellAt !== null && b.fellAt !== null) return a.fellAt === b.fellAt ? null : a.fellAt < b.fellAt ? a.slot : b.slot;
|
||||
if (a.fellAt !== null) return a.slot;
|
||||
if (b.fellAt !== null) return b.slot;
|
||||
return a.integrityLost === b.integrityLost ? null : a.integrityLost > b.integrityLost ? a.slot : b.slot;
|
||||
}
|
||||
const VS_COUNTDOWN_MS = 3000; // a local match: the pause before each round (the server's default too)
|
||||
const VS_BOT_STEP = 0.25; // the computer is asked to think at about 4 Hz of game time
|
||||
const VS_PRIV_MS = 250; // role-private data at 4 Hz
|
||||
|
||||
// ws(s)://<same host>/ws, relative to the page so it also works under a proxy path prefix
|
||||
function wsUrl() {
|
||||
const u = new URL('ws', root.location.href);
|
||||
|
|
@ -60,19 +104,132 @@
|
|||
this.speed = 1; this.paused = false;
|
||||
this.players = [{ slot: 0, name: 'You', online: true, host: true }];
|
||||
this.you = { slot: 0, host: true };
|
||||
this._fresh();
|
||||
this.versus = !!(opts && opts.mode === 'versus');
|
||||
this.mode = this.versus ? 'versus' : 'solo';
|
||||
this.vs = null; this.priv = null; this.bot = null;
|
||||
if (this.versus) { this._vsInit(opts); this._vsRound(); } else this._fresh();
|
||||
}
|
||||
get role() { return this.vs ? this.vs.roles[0] : null; }
|
||||
_fresh() {
|
||||
this.sim = new TD.Sim({ map: this.mapId, difficulty: this.difficulty });
|
||||
this.sim = this.versus ? new TD.Sim({ map: this.mapId, mode: 'versus', limit: this.limit }) : new TD.Sim({ map: this.mapId, difficulty: this.difficulty });
|
||||
this.board = this.sim.board;
|
||||
this.acc = 0; this.last = 0;
|
||||
this.view = {
|
||||
gold: 0, lives: 0, maxLives: 0, wave: 0, state: 'playing', auto: false, nextWaveIn: -1, kills: 0, winWave: 0, rev: 0, queued: 0,
|
||||
speed: this.speed, paused: this.paused, time: 0, difficulty: this.difficulty,
|
||||
speed: this.speed, paused: this.paused, time: 0, difficulty: this.difficulty, mode: this.mode, vs: null,
|
||||
towers: this.sim.towers, monsters: this.sim.monsters, bullets: this.sim.bullets, cursors: [],
|
||||
};
|
||||
this._sync();
|
||||
}
|
||||
// ---- versus against the computer (the human is seat 0, NWBot seat 1) ----
|
||||
_vsInit(opts) {
|
||||
if (!root.NWBot) throw new Error('A versus game against the computer needs js/bot.js (NWBot) to be loaded before it is created.');
|
||||
const V = TD.VS;
|
||||
this.limit = Number.isFinite(opts.limit) && opts.limit > 0 ? opts.limit : V.DEFAULT_LIMIT;
|
||||
this.skill = Number.isFinite(opts.skill) ? clamp(opts.skill, 0, 1) : 1;
|
||||
this.rounds = V.ROUNDS;
|
||||
this.countdownMs = Number.isFinite(opts.countdownMs) ? opts.countdownMs : VS_COUNTDOWN_MS;
|
||||
this.seed = Number.isInteger(opts.seed) ? opts.seed : Math.floor(Math.random() * 0x7fffffff);
|
||||
const mine = opts.role === 'attacker' || opts.role === 'defender' ? opts.role : (Math.random() < 0.5 ? 'attacker' : 'defender');
|
||||
this.firstRoles = { 0: mine, 1: vsOpposite(mine) };
|
||||
this.players = [{ slot: 0, name: 'You', online: true, host: true }];
|
||||
this._privAt = -Infinity;
|
||||
this._vsStartMatch(this.firstRoles);
|
||||
}
|
||||
_vsStartMatch(roles) {
|
||||
this.firstRoles = { 0: roles[0], 1: roles[1] };
|
||||
this.vsm = { phase: 'countdown', round: 1, roles: { 0: roles[0], 1: roles[1] }, history: [], result: null, scores: null, winner: null, cdMs: 0, cdSent: -1, botAcc: 0 };
|
||||
}
|
||||
// The sim and the computer of the current round (called when a round's countdown starts).
|
||||
_vsRound() {
|
||||
const m = this.vsm;
|
||||
this._fresh();
|
||||
m.phase = 'countdown'; m.cdMs = this.countdownMs; m.cdSent = Math.ceil(m.cdMs / 1000); m.botAcc = 0; m.result = null;
|
||||
this.bot = root.NWBot.create(this.sim, m.roles[1], { seed: this.seed + m.round * 101, skill: this.skill, by: 1 });
|
||||
this._vsEmit();
|
||||
this._vsPriv();
|
||||
}
|
||||
_vsNames() { return [this.players[0].name, 'Computer']; }
|
||||
_vsMsg() {
|
||||
const m = this.vsm;
|
||||
const out = {
|
||||
t: 'vs', phase: m.phase, round: m.round, rounds: this.rounds, roles: { 0: m.roles[0], 1: m.roles[1] }, random: false,
|
||||
bot: { slot: 1, role: m.roles[1], skill: this.skill, name: 'Computer' }, limit: this.limit, ready: { 0: true, 1: true }, frozen: false,
|
||||
};
|
||||
if (m.phase === 'countdown') out.countdown = Math.max(0, Math.ceil(m.cdMs / 1000));
|
||||
if (m.phase === 'roundover') out.next = { 0: false, 1: true };
|
||||
if (m.phase === 'roundover' || m.phase === 'matchover') out.result = m.result;
|
||||
if (m.history.length) out.history = m.history;
|
||||
if (m.phase === 'matchover') { out.scores = m.scores; out.winner = m.winner; out.forfeit = null; out.rematch = { 0: false, 1: true }; }
|
||||
return out;
|
||||
}
|
||||
_vsEmit() { this.vs = this._vsMsg(); this.emit('vs', this.vs); }
|
||||
_vsPriv() {
|
||||
const role = this.vsm.roles[0], ps = this.sim.privateState(role);
|
||||
if (!ps) return;
|
||||
this.priv = Object.assign({ t: 'priv', role }, ps);
|
||||
this.emit('priv', this.priv);
|
||||
}
|
||||
_vsRoundEnd() {
|
||||
const m = this.vsm, res = this.sim.vs.result;
|
||||
const attacker = m.roles[0] === 'attacker' ? 0 : 1;
|
||||
const entry = { round: m.round, roles: { 0: m.roles[0], 1: m.roles[1] }, result: { winner: res.winner, t: res.t, integrityLost: res.integrityLost, attacker, defender: 1 - attacker } };
|
||||
m.history.push(entry); m.result = entry.result;
|
||||
this.bot = null;
|
||||
if (m.round < this.rounds) m.phase = 'roundover';
|
||||
else { m.phase = 'matchover'; m.scores = matchScores(m.history, this._vsNames()); m.winner = matchWinner(m.scores); }
|
||||
this._vsEmit();
|
||||
this._vsPriv();
|
||||
}
|
||||
_vsUpdate(now) {
|
||||
const m = this.vsm;
|
||||
if (!this.last) this.last = now;
|
||||
const dt = Math.min(0.25, Math.max(0, (now - this.last) / 1000));
|
||||
this.last = now;
|
||||
if (m.phase === 'countdown') {
|
||||
m.cdMs -= dt * 1000;
|
||||
if (m.cdMs <= 0) { m.phase = 'playing'; this.acc = 0; this._vsEmit(); this._vsPriv(); }
|
||||
else if (Math.ceil(m.cdMs / 1000) !== m.cdSent) { m.cdSent = Math.ceil(m.cdMs / 1000); this._vsEmit(); }
|
||||
} else if (m.phase === 'playing') {
|
||||
this.acc = Math.min(0.6, this.acc + dt);
|
||||
let ticked = 0;
|
||||
while (this.acc >= TD.STEP && this.sim.state === 'playing') { this.sim.tick(TD.STEP); this.acc -= TD.STEP; ticked += TD.STEP; }
|
||||
m.botAcc += ticked;
|
||||
if (this.bot && m.botAcc >= VS_BOT_STEP) { const d = m.botAcc; m.botAcc = 0; try { this.bot.think(d); } catch (e) { console.error(e); this.bot = null; } }
|
||||
if (this.sim.state !== 'playing') this._vsRoundEnd();
|
||||
}
|
||||
const ev = this.sim.drainEvents();
|
||||
this._sync();
|
||||
if (ev.length) this.emit('events', ev);
|
||||
if ((m.phase === 'countdown' || m.phase === 'playing') && now - this._privAt >= VS_PRIV_MS) { this._privAt = now; this._vsPriv(); }
|
||||
}
|
||||
_vsSend(cmd) {
|
||||
const m = this.vsm;
|
||||
let res;
|
||||
if (m.phase !== 'playing') res = { ok: false, reason: m.phase === 'roundover' || m.phase === 'matchover' ? 'over' : 'phase' };
|
||||
else {
|
||||
const c = Object.assign({}, cmd, { by: 0, role: m.roles[0] });
|
||||
res = this.sim.cmd(stampCmd(c, this.view));
|
||||
}
|
||||
if (!res.ok) this.emit('reject', { c: cmd.c, reason: res.reason, x: cmd.x, y: cmd.y, id: cmd.id, tid: cmd.tid });
|
||||
else { this._vsPriv(); this._sync(); }
|
||||
return res;
|
||||
}
|
||||
// lobby / match control; a local game has no lobby, so only `next` and `rematch` do anything
|
||||
vsSend(c, v) {
|
||||
const m = this.vsm;
|
||||
if (!this.versus) return { ok: false, reason: 'versus' };
|
||||
if (c === 'next' && m.phase === 'roundover') { m.round++; m.roles = vsSwap(m.roles); this._vsRound(); return { ok: true }; }
|
||||
if (c === 'rematch' && m.phase === 'matchover') { this._vsStartMatch(vsSwap(this.firstRoles)); this._vsRound(); return { ok: true }; }
|
||||
this.emit('reject', { c, reason: 'phase' });
|
||||
return { ok: false, reason: 'phase' };
|
||||
}
|
||||
vsRole(v) { return this.vsSend('role', v); }
|
||||
vsReady(v) { return this.vsSend('ready', v); }
|
||||
vsBot(v, skill) { return this.vsSend('bot', v, { skill }); }
|
||||
vsStart() { return this.vsSend('start'); }
|
||||
vsNext() { return this.vsSend('next'); }
|
||||
vsRematch() { return this.vsSend('rematch'); }
|
||||
_sync() {
|
||||
const s = this.sim, v = this.view;
|
||||
v.gold = s.gold; v.lives = s.lives; v.maxLives = s.maxLives; v.wave = s.wave; v.state = s.state;
|
||||
|
|
@ -80,8 +237,10 @@
|
|||
v.winWave = isFinite(s.winWave) ? s.winWave : 0; v.rev = s.rev; v.queued = s.spawnQ.length;
|
||||
v.speed = this.speed; v.paused = this.paused; v.time = s.time; v.difficulty = s.difficulty;
|
||||
v.towers = s.towers; v.monsters = s.monsters; v.bullets = s.bullets;
|
||||
if (s.vs) v.vs = { t: TD.round2(s.time), lim: s.vs.limit, st: s.vsStaged() };
|
||||
}
|
||||
update(now) {
|
||||
if (this.versus) { this._vsUpdate(now); return; }
|
||||
if (!this.last) this.last = now;
|
||||
const dt = Math.min(0.25, Math.max(0, (now - this.last) / 1000));
|
||||
this.last = now;
|
||||
|
|
@ -94,11 +253,13 @@
|
|||
if (ev.length) this.emit('events', ev);
|
||||
}
|
||||
send(cmd) {
|
||||
if (this.versus) return this._vsSend(cmd);
|
||||
const res = this.sim.cmd(stampCmd(Object.assign({}, cmd, { by: 0 }), this.view));
|
||||
if (!res.ok) this.emit('reject', { c: cmd.c, reason: res.reason, x: cmd.x, y: cmd.y, id: cmd.id });
|
||||
return res;
|
||||
}
|
||||
ctl(c, v) {
|
||||
if (this.versus) return; // no pause, no speed in versus
|
||||
if (c === 'pause') this.paused = !!v;
|
||||
else if (c === 'speed') this.speed = clamp(v | 0, 1, 3);
|
||||
this._sync();
|
||||
|
|
@ -106,6 +267,11 @@
|
|||
preview(x, y, routes) { return this.sim.preview(x, y, routes); }
|
||||
// `difficulty` absent: the same one again ("Play again"); an unknown one is normal (the sim's rule)
|
||||
newGame(map, difficulty) {
|
||||
if (this.versus) { // a new match (the first roles swapped), at once, whatever the phase
|
||||
if (map) this.mapId = map;
|
||||
this._vsStartMatch(vsSwap(this.firstRoles)); this._vsRound(); this.emit('newgame');
|
||||
return;
|
||||
}
|
||||
if (map) this.mapId = map;
|
||||
if (difficulty !== undefined && difficulty !== null) this.difficulty = TD.difficultyId(difficulty);
|
||||
this._fresh(); this.emit('newgame');
|
||||
|
|
@ -125,6 +291,9 @@
|
|||
this.pid = opts.pid; this.name = opts.name || 'Player';
|
||||
this.room = opts.room || null; // null => create a room on first connect
|
||||
this.mapId = opts.map || 'open';
|
||||
this.mode = opts.mode === 'versus' ? 'versus' : 'coop'; // what a CREATING join asks for; afterwards whatever the server's state says
|
||||
this.limit = opts.limit; // versus round limit (seconds) of a creating join
|
||||
this.vs = null; this.priv = null; // the newest 'vs' / 'priv' messages (see the header)
|
||||
this.difficulty = TD.difficultyId(opts.difficulty); // what a CREATING join asks for; afterwards whatever the server's state says
|
||||
this.status = 'idle'; // idle | connecting | joined | reconnecting | closed
|
||||
this.ws = null; this.players = []; this.you = null; this.board = null;
|
||||
|
|
@ -140,13 +309,14 @@
|
|||
this.speed = 1; this.paused = false;
|
||||
this.view = {
|
||||
gold: 0, lives: 0, maxLives: 0, wave: 0, state: 'playing', auto: false, nextWaveIn: -1, kills: 0, winWave: 0, rev: 0, queued: 0,
|
||||
speed: 1, paused: false, time: 0, difficulty: this.difficulty, towers: [], monsters: [], bullets: [], cursors: [],
|
||||
speed: 1, paused: false, time: 0, difficulty: this.difficulty, mode: this.mode, vs: null, towers: [], monsters: [], bullets: [], cursors: [],
|
||||
};
|
||||
this._onVis = () => { if (!document.hidden) this._wake(); };
|
||||
document.addEventListener('visibilitychange', this._onVis);
|
||||
root.addEventListener('online', this._onVis);
|
||||
this._timers = [];
|
||||
}
|
||||
get role() { return this.vs && this.you ? this.vs.roles[this.you.slot] || null : null; }
|
||||
// The document / window listeners must not outlive the controller (code-19): called when it dies and when it is closed.
|
||||
_dispose() {
|
||||
document.removeEventListener('visibilitychange', this._onVis);
|
||||
|
|
@ -162,7 +332,11 @@
|
|||
this.ws = ws;
|
||||
ws.onopen = () => {
|
||||
const j = { t: 'join', pid: this.pid, name: this.name };
|
||||
if (this.room) j.room = this.room; else { j.map = this.mapId; j.difficulty = this.difficulty; } // only a creating join carries them: a joiner takes the room's
|
||||
if (this.room) j.room = this.room;
|
||||
else { // only a creating join carries these: a joiner takes the room's
|
||||
j.map = this.mapId; j.difficulty = this.difficulty;
|
||||
if (this.mode === 'versus') { j.mode = 'versus'; if (this.limit) j.limit = this.limit; }
|
||||
}
|
||||
this._send(j);
|
||||
};
|
||||
ws.onmessage = (e) => { this.lastMsg = performance.now(); this._onMsg(e.data); };
|
||||
|
|
@ -242,10 +416,12 @@
|
|||
this.everJoined = true;
|
||||
this.retry = 0; this.speed = m.speed || 1; this.paused = !!m.paused;
|
||||
this._applyState(m.state, m.snap);
|
||||
if (m.vs) this.vs = m.vs;
|
||||
this._setStatus('joined');
|
||||
this._startRtt();
|
||||
this.emit('joined', m);
|
||||
this.emit('players', this.players);
|
||||
if (m.vs) this.emit('vs', m.vs);
|
||||
break;
|
||||
case 'state':
|
||||
this.speed = m.speed || 1; this.paused = !!m.paused;
|
||||
|
|
@ -257,6 +433,8 @@
|
|||
{ const me = this.players.find((p) => this.you && p.pid === this.you.pid); if (me) this.you = Object.assign({}, this.you, { host: me.host, slot: me.slot }); }
|
||||
this.emit('players', this.players);
|
||||
break;
|
||||
case 'vs': this.vs = m; this.emit('vs', m); break;
|
||||
case 'priv': this.priv = m; this.emit('priv', m); break;
|
||||
case 'reject': this.emit('reject', m); break;
|
||||
case 'mark': this.emit('mark', m); break;
|
||||
case 'rtt':
|
||||
|
|
@ -268,7 +446,7 @@
|
|||
this._die('restarted', this.everJoined ? 'The server restarted, your game was lost.' : 'That co-op game is gone: the server restarted or the room timed out.');
|
||||
break;
|
||||
}
|
||||
const msgs = { no_room: 'That room does not exist (any more).', full: 'That room already has two players.', bad_request: 'The server did not understand the request.', too_many_rooms: 'The server is full right now. Try again in a minute.', bad_map: 'Unknown map.', bad_difficulty: 'Unknown difficulty.' };
|
||||
const msgs = { no_room: 'That room does not exist (any more).', full: 'That room already has two players.', bad_mode: 'Unknown game mode.', bad_limit: 'Unknown round length.', bad_rounds: 'Unsupported number of rounds.', bad_request: 'The server did not understand the request.', too_many_rooms: 'The server is full right now. Try again in a minute.', bad_map: 'Unknown map.', bad_difficulty: 'Unknown difficulty.' };
|
||||
this._die(m.code || 'error', msgs[m.code] || m.msg || 'Server error.');
|
||||
break;
|
||||
}
|
||||
|
|
@ -292,6 +470,8 @@
|
|||
this.maxLives = state.maxLives; this.winWave = state.winWave;
|
||||
this.difficulty = TD.isDifficulty(state.difficulty) ? state.difficulty : 'normal'; // (a server from before difficulties sends none)
|
||||
this.view.difficulty = this.difficulty;
|
||||
this.mode = state.mode === 'versus' ? 'versus' : 'coop'; this.limit = state.limit || this.limit;
|
||||
this.view.mode = this.mode;
|
||||
this.buf.length = 0; this._carryTw = null; this.monCache.clear(); this.bulCache.clear();
|
||||
if (snap) this._push(snap, true);
|
||||
}
|
||||
|
|
@ -345,6 +525,7 @@
|
|||
v.rev = s.rev; v.queued = s.q || 0; v.time = s.time;
|
||||
v.maxLives = this.maxLives; v.winWave = this.winWave;
|
||||
v.speed = s.sp || 1; v.paused = !!s.pa;
|
||||
v.vs = s.vs || null; // versus: the shared round block { t, lim, st }
|
||||
|
||||
const gen = ++this._gen;
|
||||
const kinds = TD.ENEMY_ORDER;
|
||||
|
|
@ -375,6 +556,7 @@
|
|||
for (const t of this.towers) {
|
||||
const r = a.tt.get(t.id);
|
||||
t.tgt = r ? r[1] : 0; t.kills = r ? r[2] : 0;
|
||||
if (this.mode === 'versus') t.empT = r && r[3] ? r[3] : 0; // versus rows carry a 4th field: seconds of EMP left
|
||||
}
|
||||
v.towers = this.towers;
|
||||
const mySlot = this.you ? this.you.slot : -1;
|
||||
|
|
@ -382,12 +564,19 @@
|
|||
}
|
||||
|
||||
send(cmd) {
|
||||
const c = Object.assign({}, cmd); delete c.by;
|
||||
const c = Object.assign({}, cmd); delete c.by; delete c.role; // the server knows the seat and its role
|
||||
stampCmd(c, this.view);
|
||||
this._send(Object.assign({ t: 'cmd' }, c));
|
||||
return { ok: true, pending: true };
|
||||
}
|
||||
ctl(c, v) { this._send({ t: 'ctl', c, v }); }
|
||||
ctl(c, v) { if (this.mode !== 'versus') this._send({ t: 'ctl', c, v }); } // versus has no pause and no speed (the server refuses them)
|
||||
vsSend(c, v, extra) { this._send(Object.assign({ t: 'vs', c, v }, extra)); return { ok: true, pending: true }; }
|
||||
vsRole(v) { return this.vsSend('role', v); }
|
||||
vsReady(v) { return this.vsSend('ready', v); }
|
||||
vsBot(v, skill) { return this.vsSend('bot', v, skill === undefined ? undefined : { skill }); }
|
||||
vsStart() { return this.vsSend('start'); }
|
||||
vsNext() { return this.vsSend('next'); }
|
||||
vsRematch() { return this.vsSend('rematch'); }
|
||||
preview(x, y, routes) {
|
||||
if (!this.board) return { ok: false, reason: 'bounds' };
|
||||
const reserved = new Set();
|
||||
|
|
@ -423,5 +612,7 @@
|
|||
|
||||
NW.LocalController = LocalController;
|
||||
NW.RemoteController = RemoteController;
|
||||
NW.matchScores = matchScores; // the match rules (shared with the server by a parity test): also handy for the end screen
|
||||
NW.matchWinner = matchWinner;
|
||||
NW.wsUrl = wsUrl;
|
||||
})(typeof self !== 'undefined' ? self : this);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue