Co-op robustness, audio/a11y, new towers and enemies, Uplink rebalance
Co-op (QA'd with two to three real browsers, TCP-proxy chaos scenarios): - partner's tower changes are never dropped after a render stall or snapshot burst - half-open connections show "reconnecting" in ~6 s instead of 45-65 s - upgrade and next-wave are idempotent (stale commands refused), restart message, promoted host gets Play again, failed joins never kill a live game, listener leaks Audio / keyboard / a11y: - one Audio element per track, fade/stinger supersede tokens, play() retry on the next gesture, 5 s fallback to the synth, mute works everywhere, dialogs with role/aria-modal/focus handling, accessible names for sound/speed/pause New content: - enemies: Daemon (healer), Proxy (regenerating shield), Botnet (flyer swarm); waves weave them in from wave 9 (waves 1-8 unchanged) - towers: Sandbox (stun pulse), Load Balancer (fire-rate aura); hotkeys 1-9 - glyphs/shapes, stun/heal effects, gallery e2e that pixel-checks every kind Orbital Uplink rebalanced after playtesting (was affordable by wave ~6 and buyable in bulk from boss rewards): 1500 credits, upgrades 1200/2000/3200, and every Uplink already owned makes the next one 25% dearer to buy AND to upgrade (each tower remembers its multiplier, carried in the tower list as a 9th field). Shared TD.priceFor/upgradePrice so the UI shows exactly what the sim charges. Also: tablet/foldable viewports in the layout matrix (tolerant colour check), docs (README tower/enemy tables, PROTOCOL shield/stun/pm encodings, NEXT roadmap). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tb8pW2KAN4oFkh1aK1BfD8
This commit is contained in:
parent
962189419a
commit
28502984ec
29 changed files with 3370 additions and 230 deletions
251
test/controllers.test.js
Normal file
251
test/controllers.test.js
Normal file
|
|
@ -0,0 +1,251 @@
|
|||
'use strict';
|
||||
/*
|
||||
* RemoteController / LocalController without a browser: public/js/controllers.js runs in a vm context with a fake WebSocket, a fake
|
||||
* document/window and a manual clock (timers and performance.now() advance only when the test says so).
|
||||
*/
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const vm = require('node:vm');
|
||||
const TD = require('../public/js/sim.js');
|
||||
|
||||
const SRC = fs.readFileSync(path.join(__dirname, '..', 'public', 'js', 'controllers.js'), 'utf8');
|
||||
|
||||
function harness() {
|
||||
const H = { now: 1000, timers: [], seq: 0, sockets: [], docAdded: 0, docRemoved: 0, winAdded: 0, winRemoved: 0 };
|
||||
const timer = (fn, ms, repeat) => { const t = { id: ++H.seq, at: H.now + ms, fn, ms, repeat }; H.timers.push(t); return t.id; };
|
||||
class FakeWS {
|
||||
constructor(url) { this.url = url; this.readyState = 0; this.sent = []; this.closeCalls = 0; H.sockets.push(this); }
|
||||
send(s) { this.sent.push(JSON.parse(s)); }
|
||||
close() { this.closeCalls++; this.readyState = 2; } // a half-open socket: no close event follows
|
||||
open() { this.readyState = 1; if (this.onopen) this.onopen(); }
|
||||
recv(o) { if (this.onmessage) this.onmessage({ data: JSON.stringify(o) }); }
|
||||
serverClose(code) { this.readyState = 3; if (this.onclose) this.onclose({ code }); }
|
||||
}
|
||||
const sandbox = {
|
||||
TD, console, URL, FakeWS,
|
||||
WebSocket: FakeWS,
|
||||
document: { hidden: false, addEventListener() { H.docAdded++; }, removeEventListener() { H.docRemoved++; } },
|
||||
performance: { now: () => H.now },
|
||||
location: { href: 'http://localhost/index.html' },
|
||||
addEventListener() { H.winAdded++; }, removeEventListener() { H.winRemoved++; },
|
||||
setTimeout: (fn, ms) => timer(fn, ms, false), clearTimeout: (id) => { H.timers = H.timers.filter((t) => t.id !== id); },
|
||||
setInterval: (fn, ms) => timer(fn, ms, true), clearInterval: (id) => { H.timers = H.timers.filter((t) => t.id !== id); },
|
||||
};
|
||||
sandbox.self = sandbox;
|
||||
vm.runInNewContext(SRC, sandbox, { filename: 'controllers.js' });
|
||||
H.NW = sandbox.NW;
|
||||
H.advance = (ms) => { // runs the timers that fall due, in order, moving the clock along
|
||||
const end = H.now + ms;
|
||||
for (;;) {
|
||||
const due = H.timers.filter((t) => t.at <= end).sort((a, b) => a.at - b.at || a.id - b.id)[0];
|
||||
if (!due) break;
|
||||
H.now = Math.max(H.now, due.at);
|
||||
if (due.repeat) due.at += due.ms; else H.timers = H.timers.filter((t) => t !== due);
|
||||
due.fn();
|
||||
}
|
||||
H.now = end;
|
||||
};
|
||||
return H;
|
||||
}
|
||||
|
||||
// What the server would send for a sim in its current state.
|
||||
function joinedMsg(sim, extra) {
|
||||
return Object.assign({
|
||||
t: 'joined', room: 'ABCD', resumed: false, you: { pid: 'pidpidpid1', slot: 1, host: false },
|
||||
players: [{ pid: 'pidpidpid1', slot: 1, name: 'Bob', host: false, online: true }, { pid: 'aliasaliasalias', slot: 0, name: 'Alice', host: true, online: true }],
|
||||
state: sim.fullState(), snap: sim.snapshot(), speed: 1, paused: false,
|
||||
}, extra);
|
||||
}
|
||||
let seq = 0;
|
||||
function snapMsg(sim, tw) {
|
||||
const m = Object.assign({ t: 'snap', seq: ++seq }, sim.snapshot(), { ev: [], cur: [], pa: 0, sp: 1 });
|
||||
if (tw) m.tw = sim.towerList();
|
||||
return m;
|
||||
}
|
||||
function connected(opts) {
|
||||
const H = harness();
|
||||
const sim = new TD.Sim({ map: 'open' });
|
||||
const rc = new H.NW.RemoteController(Object.assign({ pid: 'pidpidpid1', name: 'Bob', room: 'ABCD', url: 'ws://x/ws' }, opts));
|
||||
rc.connect();
|
||||
const ws = H.sockets[0];
|
||||
ws.open();
|
||||
ws.recv(joinedMsg(sim));
|
||||
return { H, sim, rc, ws };
|
||||
}
|
||||
|
||||
test('coop-1: a burst of snapshots larger than the buffer does not lose the partner\'s tower change', () => {
|
||||
const { H, sim, rc, ws } = connected();
|
||||
assert.equal(rc.status, 'joined');
|
||||
ws.recv(snapMsg(sim)); // quiet snapshots, then the partner builds...
|
||||
sim.cmd({ c: 'place', type: 'wall', x: 12, y: 5, by: 0 });
|
||||
ws.recv(snapMsg(sim, true)); // ...this one carries the tower list (sent exactly once)
|
||||
for (let i = 0; i < 14; i++) ws.recv(snapMsg(sim)); // the page was stalled: 14 more arrive before it renders again
|
||||
assert.ok(rc.buf.length <= 10, 'the buffer is still bounded');
|
||||
H.advance(500);
|
||||
rc.update(H.now);
|
||||
assert.equal(rc.towers.length, 1, 'the tower is there');
|
||||
assert.equal(rc.view.towers.length, 1);
|
||||
assert.equal(rc.rev, sim.rev, 'and so is the revision');
|
||||
assert.ok(rc.board.solid[5 * rc.board.cols + 12], 'the mirror board knows the new wall (route previews are right)');
|
||||
assert.equal(ws.sent.filter((m) => m.t === 'sync').length, 0, 'no resync was needed');
|
||||
});
|
||||
|
||||
test('coop-1: the NEWEST of several dropped tower lists wins; an older one is never applied over it', () => {
|
||||
const { H, sim, rc, ws } = connected();
|
||||
sim.cmd({ c: 'place', type: 'wall', x: 12, y: 5, by: 0 });
|
||||
ws.recv(snapMsg(sim, true));
|
||||
sim.cmd({ c: 'place', type: 'wall', x: 12, y: 6, by: 1 });
|
||||
ws.recv(snapMsg(sim, true));
|
||||
sim.cmd({ c: 'sell', id: 1 });
|
||||
ws.recv(snapMsg(sim, true)); // list 3: wall 2 only
|
||||
for (let i = 0; i < 20; i++) ws.recv(snapMsg(sim));
|
||||
H.advance(500);
|
||||
rc.update(H.now);
|
||||
assert.deepEqual([...rc.towers.map((t) => t.id)], [2]);
|
||||
assert.equal(rc.rev, sim.rev);
|
||||
});
|
||||
|
||||
test('coop-1: a tower list that never arrives is noticed (revision mismatch) and the full state is requested', () => {
|
||||
const { H, sim, rc, ws } = connected();
|
||||
sim.cmd({ c: 'place', type: 'wall', x: 12, y: 5, by: 0 });
|
||||
ws.recv(snapMsg(sim, true));
|
||||
rc.buf.length = 0; ws.recv(snapMsg(sim)); // (simulate the list being lost on the way)
|
||||
H.advance(500);
|
||||
rc.update(H.now);
|
||||
assert.equal(ws.sent.filter((m) => m.t === 'sync').length, 1);
|
||||
});
|
||||
|
||||
test('coop-2: a joined socket that goes silent is abandoned after 5 s: reconnecting at once and a fresh socket, without waiting for a close', () => {
|
||||
const { H, rc, ws } = connected();
|
||||
const states = [];
|
||||
rc.on('status', (s) => states.push(s));
|
||||
H.advance(4000);
|
||||
assert.equal(rc.status, 'joined', 'still fine after 4 s of silence');
|
||||
assert.equal(H.sockets.length, 1);
|
||||
H.advance(2500);
|
||||
assert.equal(rc.status, 'reconnecting', 'the watchdog shows it immediately');
|
||||
assert.equal(H.sockets.length, 2, 'a new socket was opened without waiting for the dead one to close');
|
||||
assert.equal(ws.closeCalls, 1, 'the dead one was closed');
|
||||
assert.equal(ws.onclose, null, 'its handlers are detached: a late close / the server\'s 4001 cannot be mistaken for the new connection');
|
||||
assert.deepEqual(states, ['reconnecting']);
|
||||
const ws2 = H.sockets[1];
|
||||
ws2.open();
|
||||
assert.equal(ws2.sent[0].t, 'join'); assert.equal(ws2.sent[0].room, 'ABCD'); assert.equal(ws2.sent[0].pid, 'pidpidpid1');
|
||||
ws2.recv(joinedMsg(new TD.Sim({ map: 'open' }), { resumed: true }));
|
||||
assert.equal(rc.status, 'joined');
|
||||
ws.serverClose(1006); // the old socket finally dies: nothing changes
|
||||
assert.equal(rc.status, 'joined');
|
||||
assert.equal(H.sockets.length, 2);
|
||||
});
|
||||
|
||||
test('coop-2: waking up after a stall abandons a socket that has been silent for 2.5 s', () => {
|
||||
const { H, rc } = connected();
|
||||
H.now += 3000; // the page was frozen; timers have not run
|
||||
rc._wake();
|
||||
assert.equal(rc.status, 'reconnecting');
|
||||
assert.equal(H.sockets.length, 2);
|
||||
});
|
||||
|
||||
test('coop-2: a connection attempt that hangs is abandoned after 10 s and retried', () => {
|
||||
const { H, rc } = connected();
|
||||
H.advance(6000); // watchdog: socket 2 is opened and then hangs (never opens)
|
||||
assert.equal(H.sockets.length, 2);
|
||||
H.advance(10100);
|
||||
assert.equal(rc.status, 'reconnecting');
|
||||
assert.equal(H.sockets[1].closeCalls, 1, 'the attempt that went nowhere was dropped');
|
||||
H.advance(500);
|
||||
assert.equal(H.sockets.length, 3, 'and another attempt was made');
|
||||
});
|
||||
|
||||
test('coop-6: "no such room" after a reconnect says the server restarted; on a resume it says the game is gone; a plain join says so', () => {
|
||||
const a = connected();
|
||||
const fatal = []; a.rc.on('fatal', (f) => fatal.push(f));
|
||||
a.ws.serverClose(1006); // the server went away...
|
||||
a.H.advance(500);
|
||||
const ws2 = a.H.sockets[1]; ws2.open();
|
||||
ws2.recv({ t: 'error', code: 'no_room', msg: 'no such room' }); // ...and came back without the room
|
||||
assert.equal(fatal.length, 1);
|
||||
assert.equal(fatal[0].code, 'restarted');
|
||||
assert.match(fatal[0].msg, /server restarted, your game was lost/);
|
||||
|
||||
const H = harness();
|
||||
const resume = new H.NW.RemoteController({ pid: 'pidpidpid1', name: 'Bob', room: 'ZZZZ', url: 'ws://x/ws', resume: true });
|
||||
const f2 = []; resume.on('fatal', (f) => f2.push(f));
|
||||
resume.connect(); H.sockets[0].open(); H.sockets[0].recv({ t: 'error', code: 'no_room', msg: 'no such room' });
|
||||
assert.equal(f2[0].code, 'restarted'); assert.match(f2[0].msg, /gone/);
|
||||
|
||||
const plain = new H.NW.RemoteController({ pid: 'pidpidpid1', name: 'Bob', room: 'ZZZZ', url: 'ws://x/ws' });
|
||||
const f3 = []; plain.on('fatal', (f) => f3.push(f));
|
||||
plain.connect(); H.sockets[1].open(); H.sockets[1].recv({ t: 'error', code: 'no_room', msg: 'no such room' });
|
||||
assert.equal(f3[0].code, 'no_room'); assert.match(f3[0].msg, /does not exist/);
|
||||
});
|
||||
|
||||
test('code-19: listeners are removed when the controller dies and when it is closed', () => {
|
||||
const { H, rc, ws } = connected();
|
||||
assert.equal(H.docAdded, 1); assert.equal(H.winAdded, 1);
|
||||
ws.recv({ t: 'error', code: 'full', msg: 'x' }); // fatal
|
||||
assert.equal(H.docRemoved, 1); assert.equal(H.winRemoved, 1);
|
||||
const c2 = new H.NW.RemoteController({ pid: 'pidpidpid2', name: 'x', room: 'ABCD', url: 'ws://x/ws' });
|
||||
c2.close();
|
||||
assert.equal(H.docRemoved, 2); assert.equal(H.winRemoved, 2);
|
||||
c2.close(); // idempotent
|
||||
assert.equal(H.docRemoved, 2);
|
||||
assert.equal(rc.status, 'closed');
|
||||
});
|
||||
|
||||
test('code-3: a controller that was closed while connecting never emits again (a late "joined" cannot take over a game)', () => {
|
||||
const H = harness();
|
||||
const rc = new H.NW.RemoteController({ pid: 'pidpidpid1', name: 'Bob', room: null, map: 'open', url: 'ws://x/ws' });
|
||||
const events = [];
|
||||
for (const e of ['joined', 'players', 'status', 'fatal', 'events']) rc.on(e, () => events.push(e));
|
||||
rc.connect();
|
||||
const ws = H.sockets[0];
|
||||
ws.open();
|
||||
assert.equal(ws.sent[0].t, 'join');
|
||||
rc.close(); // (the user pressed Back / Play solo)
|
||||
assert.deepEqual(ws.sent.map((m) => m.t), ['join', 'leave'], 'the room that is being created is left again at once');
|
||||
events.length = 0;
|
||||
ws.recv(joinedMsg(new TD.Sim({ map: 'open' }))); // the server's answer arrives anyway
|
||||
rc._onMsg(JSON.stringify(joinedMsg(new TD.Sim({ map: 'open' })))); // (even if the browser still delivered a frame already queued)
|
||||
ws.serverClose(1000);
|
||||
assert.deepEqual(events, []);
|
||||
assert.equal(rc.status, 'closed');
|
||||
});
|
||||
|
||||
test('coop-5: Upgrade carries the level that is on screen, Next wave the wave it expects to start; explicit values are kept', () => {
|
||||
const { sim, rc, ws, H } = connected();
|
||||
sim.gold = 5000;
|
||||
sim.cmd({ c: 'place', type: 'filter', x: 10, y: 8, by: 0 });
|
||||
sim.cmd({ c: 'upgrade', id: 1 });
|
||||
ws.recv(snapMsg(sim, true));
|
||||
H.advance(500); rc.update(H.now);
|
||||
assert.equal(rc.view.towers[0].level, 2);
|
||||
rc.send({ c: 'upgrade', id: 1 });
|
||||
rc.send({ c: 'wave' });
|
||||
rc.send({ c: 'upgrade', id: 1, lv: 7 });
|
||||
rc.send({ c: 'place', type: 'wall', x: 3, y: 3 });
|
||||
rc.send({ c: 'upgrade', id: 99 }); // a tower this client does not know: no level to carry
|
||||
const cmds = ws.sent.filter((m) => m.t === 'cmd');
|
||||
assert.deepEqual(cmds[0], { t: 'cmd', c: 'upgrade', id: 1, lv: 2 });
|
||||
assert.deepEqual(cmds[1], { t: 'cmd', c: 'wave', n: rc.view.wave + 1 });
|
||||
assert.equal(cmds[2].lv, 7);
|
||||
assert.equal(cmds[3].lv, undefined); assert.equal(cmds[3].n, undefined);
|
||||
assert.equal(cmds[4].lv, undefined);
|
||||
});
|
||||
|
||||
test('coop-5: solo too: a second "Next wave" in the same frame (a double tap) is stale, the next frame\'s is not', () => {
|
||||
const H = harness();
|
||||
const lc = new H.NW.LocalController({ map: 'open' });
|
||||
const rejects = [];
|
||||
lc.on('reject', (r) => rejects.push(r));
|
||||
lc.update(1000);
|
||||
assert.equal(lc.send({ c: 'wave' }).ok, true);
|
||||
assert.equal(lc.send({ c: 'wave' }).reason, 'stale');
|
||||
assert.equal(lc.sim.wave, 1);
|
||||
assert.deepEqual(rejects.map((r) => r.reason), ['stale']);
|
||||
lc.update(1016);
|
||||
assert.equal(lc.send({ c: 'wave' }).ok, true);
|
||||
assert.equal(lc.sim.wave, 2);
|
||||
});
|
||||
|
|
@ -460,6 +460,67 @@ test('upgrade / target / sell / auto / continue go through the same path', async
|
|||
});
|
||||
});
|
||||
|
||||
test('simultaneous Upgrade from both players: applied once, the second is rejected as stale (sender only), charged once (coop-5)', async () => {
|
||||
await withServer({}, async (S) => {
|
||||
const { a, b, room } = await S.pair();
|
||||
const sim = S.srv.rooms.get(room).sim;
|
||||
a.c.send({ t: 'cmd', c: 'place', type: 'filter', x: 4, y: 4 });
|
||||
await a.c.snap((m) => m.tw && m.tw.length === 1);
|
||||
await b.c.snap((m) => m.tw && m.tw.length === 1);
|
||||
const gold = sim.gold, cost = TD.TOWERS.filter.levels[1].cost;
|
||||
// both saw level 1 and pressed Upgrade at the same moment
|
||||
a.c.send({ t: 'cmd', c: 'upgrade', id: 1, lv: 1 });
|
||||
b.c.send({ t: 'cmd', c: 'upgrade', id: 1, lv: 1 });
|
||||
await a.c.barrier(); await b.c.barrier();
|
||||
assert.equal(sim.towers[0].level, 2, 'level 2, not 3');
|
||||
assert.equal(sim.gold, gold - cost, 'charged once');
|
||||
const rejects = [...a.c.of('reject'), ...b.c.of('reject')];
|
||||
assert.equal(rejects.length, 1, 'exactly one of them was refused');
|
||||
assert.deepEqual(rejects[0], { t: 'reject', c: 'upgrade', reason: 'stale', id: 1 });
|
||||
// a client that saw level 2 may continue; a command without lv is still applied (older clients)
|
||||
a.c.send({ t: 'cmd', c: 'upgrade', id: 1, lv: 2 });
|
||||
await a.c.snap((m) => m.tw && m.tw[0][4] === 3);
|
||||
sim.gold = 1000;
|
||||
a.c.send({ t: 'cmd', c: 'place', type: 'filter', x: 6, y: 4 });
|
||||
await a.c.snap((m) => m.tw && m.tw.length === 2);
|
||||
a.c.send({ t: 'cmd', c: 'upgrade', id: 2 });
|
||||
await a.c.snap((m) => m.tw && m.tw[1][4] === 2);
|
||||
});
|
||||
});
|
||||
|
||||
test('simultaneous Next wave from both players starts one wave; the other is stale (coop-5)', async () => {
|
||||
await withServer({}, async (S) => {
|
||||
const { a, b, room } = await S.pair();
|
||||
const sim = S.srv.rooms.get(room).sim;
|
||||
a.c.send({ t: 'cmd', c: 'wave', n: 1 });
|
||||
b.c.send({ t: 'cmd', c: 'wave', n: 1 });
|
||||
await a.c.barrier(); await b.c.barrier();
|
||||
assert.equal(sim.wave, 1, 'one wave, not two');
|
||||
const rejects = [...a.c.of('reject'), ...b.c.of('reject')];
|
||||
assert.equal(rejects.length, 1);
|
||||
assert.deepEqual(rejects[0], { t: 'reject', c: 'wave', reason: 'stale' });
|
||||
b.c.send({ t: 'cmd', c: 'wave', n: 2 });
|
||||
await b.c.snap((m) => m.w === 2);
|
||||
a.c.send({ t: 'cmd', c: 'wave' }); // no number: always the next wave
|
||||
await a.c.snap((m) => m.w === 3);
|
||||
});
|
||||
});
|
||||
|
||||
test('"lv" and "n" must be integers when present: anything else is a malformed command, never "stale"', async () => {
|
||||
await withServer({}, async (S) => {
|
||||
const { a, room } = await S.pair();
|
||||
const sim = S.srv.rooms.get(room).sim;
|
||||
a.c.send({ t: 'cmd', c: 'place', type: 'filter', x: 4, y: 4 });
|
||||
await a.c.snap((m) => m.tw && m.tw.length === 1);
|
||||
for (const bad of [{ c: 'upgrade', id: 1, lv: '1' }, { c: 'upgrade', id: 1, lv: 1.5 }, { c: 'wave', n: 'x' }, { c: 'wave', n: {} }]) a.c.send(Object.assign({ t: 'cmd' }, bad));
|
||||
await a.c.barrier();
|
||||
assert.equal(sim.towers[0].level, 1); assert.equal(sim.wave, 0);
|
||||
assert.equal(a.c.of('reject').length, 0, 'not rejected by the sim: dropped as malformed');
|
||||
for (let i = 0; i < 17; i++) a.c.send({ t: 'cmd', c: 'wave', n: 'x' }); // 4 + 17 = 21 malformed messages
|
||||
assert.equal((await a.c.closed()).code, 4400);
|
||||
});
|
||||
});
|
||||
|
||||
test('wave command spawns monsters that appear in snapshots; refused beyond the load cap', async () => {
|
||||
await withServer({}, async (S) => {
|
||||
const { a, room } = await S.pair();
|
||||
|
|
|
|||
87
test/sim-commands.test.js
Normal file
87
test/sim-commands.test.js
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
'use strict';
|
||||
/*
|
||||
* Idempotent commands (coop-5): two players who press Upgrade / Next wave at the same moment send the same
|
||||
* command twice. Each carries what its sender saw (`lv`: the tower level, `n`: the wave it expects to start);
|
||||
* the sim applies the first and rejects the second with reason 'stale' instead of charging or launching twice.
|
||||
* A command without the field is applied as before (auto waves, the balance bot, older clients).
|
||||
*/
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const TD = require('../public/js/sim.js');
|
||||
|
||||
const { Sim, TOWERS } = TD;
|
||||
|
||||
function withTower(type = 'filter') {
|
||||
const s = new Sim({ map: 'open', gold: 5000 });
|
||||
const r = s.cmd({ c: 'place', type, x: 10, y: 8, by: 0 });
|
||||
assert.ok(r.ok, 'place ' + type);
|
||||
return { s, id: r.id };
|
||||
}
|
||||
|
||||
test('upgrade carrying the level the sender saw is applied once; the same command again is stale and costs nothing', () => {
|
||||
const { s, id } = withTower();
|
||||
const g0 = s.gold, cost = TOWERS.filter.levels[1].cost;
|
||||
const a = s.cmd({ c: 'upgrade', id, lv: 1, by: 0 });
|
||||
assert.deepEqual([a.ok, a.level], [true, 2]);
|
||||
assert.equal(s.gold, g0 - cost);
|
||||
const rev = s.rev;
|
||||
const b = s.cmd({ c: 'upgrade', id, lv: 1, by: 1 });
|
||||
assert.deepEqual([b.ok, b.reason], [false, 'stale']);
|
||||
assert.equal(s.towerById(id).level, 2, 'still level 2, not 3');
|
||||
assert.equal(s.gold, g0 - cost, 'charged once');
|
||||
assert.equal(s.rev, rev, 'a rejected command changes nothing');
|
||||
assert.equal(s.cmd({ c: 'upgrade', id, lv: 2, by: 1 }).ok, true, 'a sender who saw level 2 may go on to 3');
|
||||
assert.equal(s.towerById(id).level, 3);
|
||||
});
|
||||
|
||||
test('upgrade without a level (older client, bot, console) keeps working; a stale level never matches a later level either', () => {
|
||||
const { s, id } = withTower();
|
||||
assert.equal(s.cmd({ c: 'upgrade', id }).ok, true);
|
||||
assert.equal(s.towerById(id).level, 2);
|
||||
assert.equal(s.cmd({ c: 'upgrade', id, lv: null }).ok, true);
|
||||
assert.equal(s.towerById(id).level, 3);
|
||||
assert.equal(s.cmd({ c: 'upgrade', id, lv: 2 }).reason, 'stale');
|
||||
assert.equal(s.cmd({ c: 'upgrade', id, lv: '3' }).reason, 'stale', 'a level of the wrong type is never "the level I saw"');
|
||||
assert.equal(s.towerById(id).level, 3);
|
||||
});
|
||||
|
||||
test('stale beats max / gold: a sender who saw an older level is told it is stale, not that the tower is maxed out', () => {
|
||||
const { s, id } = withTower();
|
||||
const top = TOWERS.filter.levels.length;
|
||||
for (let lv = 1; lv < top; lv++) assert.ok(s.cmd({ c: 'upgrade', id, lv }).ok);
|
||||
assert.equal(s.towerById(id).level, top);
|
||||
assert.equal(s.cmd({ c: 'upgrade', id, lv: top - 1 }).reason, 'stale');
|
||||
assert.equal(s.cmd({ c: 'upgrade', id, lv: top }).reason, 'max', 'a sender who saw the top level still gets the real reason');
|
||||
const poor = new Sim({ map: 'open', gold: TOWERS.filter.levels[0].cost });
|
||||
const p = poor.cmd({ c: 'place', type: 'filter', x: 10, y: 8 });
|
||||
assert.equal(poor.cmd({ c: 'upgrade', id: p.id, lv: 1 }).reason, 'gold');
|
||||
assert.equal(poor.cmd({ c: 'upgrade', id: 999, lv: 1 }).reason, 'no_tower');
|
||||
s.state = 'lost'; // and a finished game answers "over" before it says "stale"
|
||||
assert.equal(s.cmd({ c: 'upgrade', id, lv: 7 }).reason, 'over');
|
||||
assert.equal(s.cmd({ c: 'wave', n: 7 }).reason, 'over');
|
||||
});
|
||||
|
||||
test('wave carrying the number it expects to start: the first starts it, a second sender who saw the same wave is stale', () => {
|
||||
const s = new Sim({ map: 'open' });
|
||||
const a = s.cmd({ c: 'wave', n: 1, by: 0 });
|
||||
assert.deepEqual([a.ok, a.wave], [true, 1]);
|
||||
const queued = s.spawnQ.length;
|
||||
const b = s.cmd({ c: 'wave', n: 1, by: 1 });
|
||||
assert.deepEqual([b.ok, b.reason], [false, 'stale']);
|
||||
assert.equal(s.wave, 1, 'one wave started, not two');
|
||||
assert.equal(s.spawnQ.length, queued, 'nothing was queued by the rejected command');
|
||||
assert.equal(s.cmd({ c: 'wave', n: 2 }).ok, true, 'a sender who saw wave 1 may start wave 2');
|
||||
assert.equal(s.wave, 2);
|
||||
assert.equal(s.cmd({ c: 'wave', n: 9 }).reason, 'stale', 'a number from the future is stale too');
|
||||
});
|
||||
|
||||
test('wave without a number still starts the next wave; auto waves never need one', () => {
|
||||
const s = new Sim({ map: 'open' });
|
||||
assert.equal(s.cmd({ c: 'wave' }).wave, 1);
|
||||
assert.equal(s.cmd({ c: 'wave', n: null }).wave, 2);
|
||||
s.cmd({ c: 'auto', v: true });
|
||||
s.nextWaveIn = 0.01;
|
||||
s.tick(TD.STEP * 2);
|
||||
assert.equal(s.wave, 3, 'the timer starts a wave on its own');
|
||||
assert.equal(s.cmd({ c: 'wave', n: 3 }).reason, 'stale', 'and a click that saw the old wave number is now stale');
|
||||
});
|
||||
487
test/sim.test.js
487
test/sim.test.js
|
|
@ -313,19 +313,27 @@ test('fuzz: garbage commands never throw', () => {
|
|||
});
|
||||
|
||||
test('balance tables keep the shape the client and server rely on', () => {
|
||||
assert.deepEqual(TD.TOWER_ORDER, ['wall', 'filter', 'ids', 'mortar', 'throttle', 'arc', 'uplink']);
|
||||
assert.deepEqual(TD.ENEMY_ORDER, ['ping', 'worm', 'trojan', 'spam', 'ghost', 'rootkit']);
|
||||
assert.deepEqual(TD.TOWER_ORDER, ['wall', 'filter', 'ids', 'mortar', 'throttle', 'arc', 'sandbox', 'balancer', 'uplink']);
|
||||
assert.deepEqual(TD.ENEMY_ORDER, ['ping', 'worm', 'trojan', 'spam', 'ghost', 'rootkit', 'daemon', 'proxy', 'botnet']);
|
||||
assert.equal(Object.keys(TOWERS).length, TD.TOWER_ORDER.length, 'every tower is in the palette order');
|
||||
assert.equal(Object.keys(ENEMIES).length, TD.ENEMY_ORDER.length, 'every enemy is in the wire order');
|
||||
assert.equal(TOWERS.wall.levels.length, 1);
|
||||
assert.ok(TOWERS.wall.refund, 'walls refund in full');
|
||||
const ROLES = ['wall', 'attack', 'aura', 'control', 'support'];
|
||||
for (const k of TD.TOWER_ORDER) {
|
||||
const t = TOWERS[k];
|
||||
assert.ok(t.name && t.color && t.key && t.desc, k + ' has display fields');
|
||||
assert.ok(ROLES.includes(t.role), k + ' has a known role');
|
||||
if (k === 'wall') continue;
|
||||
assert.equal(t.levels.length, k === 'uplink' ? 4 : 3, k + ' has the expected number of levels');
|
||||
t.levels.forEach((lv, i) => {
|
||||
assert.ok(lv.cost > 0 && lv.range > 0, k + ' L' + (i + 1) + ' cost/range');
|
||||
if (k === 'throttle') assert.ok(lv.slow > 0 && lv.slow <= 0.6, 'slow is capped at 0.6 by the sim');
|
||||
else if (k === 'sandbox') assert.ok(lv.pulse > 0 && lv.stun > 0 && lv.stun < lv.pulse, 'sandbox pulses and stuns for less than the recharge');
|
||||
else if (k === 'balancer') assert.ok(lv.boost > 0 && lv.boost <= 1, 'balancer boosts the fire rate');
|
||||
else assert.ok(lv.dmg > 0 && lv.rate > 0, k + ' L' + (i + 1) + ' dmg/rate');
|
||||
if (t.role === 'attack') assert.ok(lv.dmg > 0 && lv.rate > 0, k + ' attack towers have dmg and rate');
|
||||
else assert.ok(!lv.dmg && !lv.rate, k + ' deals no damage of its own');
|
||||
if (k === 'mortar') assert.ok(lv.splash > 0);
|
||||
if (k === 'arc') assert.ok(lv.chains >= 1 && lv.chainRange > 0 && lv.falloff > 0 && lv.falloff < 1);
|
||||
if (k === 'uplink') assert.ok(lv.width > 0 && lv.splash > 0 && lv.slowOnHit > 0 && lv.slowOnHit <= 0.6 && lv.slowFor > 0);
|
||||
|
|
@ -335,6 +343,8 @@ test('balance tables keep the shape the client and server rely on', () => {
|
|||
for (const k of TD.ENEMY_ORDER) {
|
||||
const e = ENEMIES[k];
|
||||
assert.ok(e.name && e.color && e.hp > 0 && e.speed > 0 && e.reward > 0 && e.leak > 0 && e.armor >= 0 && e.r > 0, k);
|
||||
assert.equal(typeof e.air, 'boolean', k + ' says whether it flies');
|
||||
assert.ok(Number.isInteger(e.buyCost) && e.buyCost > 0, k + ' has a versus buyCost');
|
||||
}
|
||||
assert.ok(TD.START_GOLD >= TOWERS.filter.levels[0].cost * 2, 'the opening credits buy at least two turrets');
|
||||
assert.ok(TD.SELL_RATIO > 0 && TD.SELL_RATIO < 1, 'selling a turret always loses credits');
|
||||
|
|
@ -384,3 +394,476 @@ test('Orbital Uplink: expensive endgame beam that pierces, ignores armour, blast
|
|||
const g = s.gold; s.sell(t.id);
|
||||
assert.equal(s.gold, g + Math.floor(upTotal * TD.SELL_RATIO));
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------- new content
|
||||
// A real monster (spawned through the sim so it has every field) moved to (x, y) and, by default, held still.
|
||||
function put(s, kind, x, y, wave) {
|
||||
s.spawnMonster({ k: kind, wave: wave || 1 });
|
||||
const m = s.monsters[s.monsters.length - 1];
|
||||
m.x = x; m.y = y; m.cx = Math.floor(x); m.cy = Math.floor(y); m.tx = -1; m.ty = -1;
|
||||
m.speed = 0;
|
||||
return m;
|
||||
}
|
||||
const tick = (s, seconds) => { const n = Math.round(seconds / STEP); for (let i = 0; i < n; i++) s.tick(STEP); };
|
||||
|
||||
test('hotkeys: one unique key 1-9 per tower, in palette order; enemy kinds are only ever appended', () => {
|
||||
const keys = TD.TOWER_ORDER.map((k) => TOWERS[k].key);
|
||||
assert.deepEqual(keys, ['1', '2', '3', '4', '5', '6', '7', '8', '9']);
|
||||
assert.equal(new Set(keys).size, keys.length);
|
||||
assert.equal(TOWERS.wall.key, '1');
|
||||
assert.equal(TOWERS.sandbox.key, '7');
|
||||
assert.equal(TOWERS.balancer.key, '8');
|
||||
assert.equal(TOWERS.uplink.key, '9');
|
||||
// the first six indexes are on the wire already (snapshot kindIndex)
|
||||
assert.deepEqual(TD.ENEMY_ORDER.slice(0, 6), ['ping', 'worm', 'trojan', 'spam', 'ghost', 'rootkit']);
|
||||
assert.deepEqual(TD.ENEMY_ORDER.slice(6), ['daemon', 'proxy', 'botnet']);
|
||||
});
|
||||
|
||||
test('buyCost: every enemy has one, about three times its kill reward', () => {
|
||||
for (const k of TD.ENEMY_ORDER) {
|
||||
const e = ENEMIES[k];
|
||||
assert.ok(Number.isInteger(e.buyCost) && e.buyCost > 0, k);
|
||||
const ratio = e.buyCost / e.reward;
|
||||
assert.ok(ratio >= 2.5 && ratio <= 3.5, k + ' buyCost/reward = ' + ratio.toFixed(2));
|
||||
}
|
||||
assert.ok(ENEMIES.daemon.air === false && ENEMIES.proxy.air === false && ENEMIES.botnet.air === true);
|
||||
});
|
||||
|
||||
test('every tower can be built, upgraded through all its levels, and sold (solid blocks like the others)', () => {
|
||||
const s = new Sim({ map: 'open', gold: 100000 });
|
||||
let x = 3;
|
||||
for (const type of TD.TOWER_ORDER) {
|
||||
const def = TOWERS[type];
|
||||
const r = s.place(type, x, 10);
|
||||
assert.ok(r.ok, type + ' can be placed');
|
||||
assert.equal(s.board.solid[10 * s.board.cols + x], 1, type + ' is a solid block');
|
||||
assert.equal(s.place(type, x, 10).reason, 'occupied');
|
||||
const t = s.towerById(r.id);
|
||||
for (let lv = 1; lv < def.levels.length; lv++) assert.ok(s.upgrade(t.id).ok, type + ' upgrade to level ' + (lv + 1));
|
||||
assert.equal(s.upgrade(t.id).reason, 'max', type);
|
||||
assert.equal(t.level, def.levels.length);
|
||||
assert.equal(t.spent, def.levels.reduce((a, l) => a + l.cost, 0));
|
||||
assert.ok(s.setTargetMode(t.id, 'close').ok);
|
||||
const g = s.gold, sold = s.sell(t.id);
|
||||
assert.equal(sold.refund, def.refund ? t.spent : Math.floor(t.spent * TD.SELL_RATIO));
|
||||
assert.equal(s.gold, g + sold.refund);
|
||||
assert.equal(s.board.solid[10 * s.board.cols + x], 0, type + ' sold: the cell is free again');
|
||||
x++;
|
||||
}
|
||||
assert.equal(s.towers.length, 0);
|
||||
// a Sandbox or Load Balancer cannot seal the route either
|
||||
const tiny = new Sim({ map: mini(['S....', '.....', '.....', '.....', '....C']), gold: 1000 });
|
||||
assert.ok(tiny.place('sandbox', 3, 4).ok);
|
||||
assert.equal(tiny.place('balancer', 4, 3).reason, 'blocks');
|
||||
assert.equal(tiny.place('sandbox', 5, 5).reason, 'bounds');
|
||||
});
|
||||
|
||||
test('Daemon heals every other enemy within 2.2 cells for 6% of max hp, never itself, never above max', () => {
|
||||
const s = new Sim({ map: 'open', gold: 0 });
|
||||
const d = put(s, 'daemon', 10.5, 5.5);
|
||||
const near = put(s, 'trojan', 12.5, 5.5), far = put(s, 'trojan', 13.0, 5.5); // 2.0 and 2.5 cells away
|
||||
const air = put(s, 'botnet', 10.5, 7.5); // 2.0 cells away, a flyer
|
||||
const full = put(s, 'ping', 10.5, 4.0);
|
||||
const dead = put(s, 'trojan', 10.5, 6.0);
|
||||
for (const m of [near, far, d, air]) m.hp = m.maxhp / 2;
|
||||
air.maxhp = air.hp = 100; air.hp = 50;
|
||||
dead.hp = 1; dead.dead = true;
|
||||
const per = (m) => 0.06 * m.maxhp;
|
||||
const n0 = near.hp, f0 = far.hp, d0 = d.hp, a0 = air.hp;
|
||||
s.healPulse();
|
||||
assert.ok(Math.abs(near.hp - (n0 + per(near))) < 1e-9, 'a trojan 2.0 cells away is healed by 6% of its max');
|
||||
assert.equal(far.hp, f0, 'one 2.5 cells away is not');
|
||||
assert.equal(d.hp, d0, 'a daemon never heals itself');
|
||||
assert.ok(Math.abs(air.hp - (a0 + per(air))) < 1e-9, 'flyers are healed too');
|
||||
assert.equal(full.hp, full.maxhp);
|
||||
assert.equal(dead.hp, 1, 'dead enemies are not healed');
|
||||
near.hp = near.maxhp - 1;
|
||||
s.healPulse();
|
||||
assert.equal(near.hp, near.maxhp, 'never above max');
|
||||
// two daemons heal each other, a lone one does not
|
||||
const d2 = put(s, 'daemon', 10.5, 6.5);
|
||||
d.hp = d.maxhp / 2; d2.hp = d2.maxhp / 2;
|
||||
s.healPulse();
|
||||
assert.ok(d.hp > d.maxhp / 2 && d2.hp > d2.maxhp / 2);
|
||||
assert.equal(d.hp, d.maxhp / 2 + per(d), 'the healing came from the other daemon only (6%, not 12%)');
|
||||
});
|
||||
|
||||
test('several daemons never heal one enemy for more than 12% of its max hp per second', () => {
|
||||
const s = new Sim({ map: 'open', gold: 0 });
|
||||
const t = put(s, 'trojan', 10.5, 5.5);
|
||||
for (let i = 0; i < 5; i++) put(s, 'daemon', 10.5 + 0.3 * i, 6.0);
|
||||
t.hp = t.maxhp * 0.1;
|
||||
s.healPulse();
|
||||
assert.ok(Math.abs(t.hp - t.maxhp * 0.22) < 1e-9, 'five daemons heal 12%, not 30%: ' + (t.hp / t.maxhp));
|
||||
// two daemons already give the full 12%; one gives 6%
|
||||
const s1 = new Sim({ map: 'open', gold: 0 });
|
||||
const t1 = put(s1, 'trojan', 10.5, 5.5); put(s1, 'daemon', 11.0, 5.5); t1.hp = 0;
|
||||
s1.healPulse();
|
||||
assert.ok(Math.abs(t1.hp - 0.06 * t1.maxhp) < 1e-9);
|
||||
});
|
||||
|
||||
test('daemons pulse once a second on one clock; heals scale with wave hp (max hp, not base hp)', () => {
|
||||
const s = new Sim({ map: 'open', gold: 0 });
|
||||
const d = put(s, 'daemon', 10.5, 5.5, 10), t = put(s, 'trojan', 11.5, 5.5, 10);
|
||||
t.hp = 100;
|
||||
assert.equal(t.maxhp, Math.round(ENEMIES.trojan.hp * TD.hpScale(10)));
|
||||
tick(s, 0.9);
|
||||
assert.equal(t.hp, 100, 'no heal before the first second is up');
|
||||
tick(s, 0.2);
|
||||
assert.ok(Math.abs(t.hp - (100 + 0.06 * t.maxhp)) < 1e-6, 'one pulse after a second');
|
||||
tick(s, 1.0);
|
||||
assert.ok(Math.abs(t.hp - (100 + 2 * 0.06 * t.maxhp)) < 1e-6, 'one more pulse a second later');
|
||||
const ev = s.drainEvents().filter((e) => e.e === 'heal');
|
||||
assert.equal(ev.length, 2, 'each pulse tells the client where the daemon healed');
|
||||
assert.ok(ev[0].r === ENEMIES.daemon.healRange && Number.isFinite(ev[0].x));
|
||||
s.monsters.splice(s.monsters.indexOf(d), 1);
|
||||
const before = t.hp;
|
||||
tick(s, 2.5);
|
||||
assert.equal(t.hp, before, 'no daemon, no healing');
|
||||
});
|
||||
|
||||
test('Proxy: the shield soaks damage first, armour applies once, overflow hurts hp, the snapshot carries hp + shield', () => {
|
||||
const s = new Sim({ map: 'open', gold: 0 });
|
||||
const m = put(s, 'proxy', 10.5, 5.5);
|
||||
const E = ENEMIES.proxy;
|
||||
assert.equal(m.hp, E.hp); assert.equal(m.shield, E.shield); assert.equal(m.maxShield, E.shield);
|
||||
s.damage(m, 10); // armour 1: 9 get through to the shield
|
||||
assert.equal(m.shield, E.shield - 9);
|
||||
assert.equal(m.hp, E.hp, 'hp untouched while the shield holds');
|
||||
const snap1 = s.snapshot().mo[0];
|
||||
assert.equal(snap1[4], E.hp + E.shield - 9, 'hp field = hp + shield');
|
||||
assert.equal(snap1[5], E.hp + E.shield, 'maxhp field = maxhp + maxShield');
|
||||
assert.equal(snap1.length, 7);
|
||||
s.damage(m, 150); // 149 after armour: 111 left in the shield, 38 into hp (armour NOT applied a second time)
|
||||
assert.equal(m.shield, 0);
|
||||
assert.equal(m.hp, E.hp - (149 - (E.shield - 9)));
|
||||
assert.equal(s.snapshot().mo[0][4], m.hp);
|
||||
assert.equal(m.dead, false);
|
||||
s.damage(m, 1e6);
|
||||
assert.equal(m.dead, true);
|
||||
assert.equal(s.kills, 1);
|
||||
// the whole pool (shield + hp) must be dealt before it dies; armour-ignoring damage also hits the shield first
|
||||
const m2 = put(s, 'proxy', 10.5, 5.5);
|
||||
s.damage(m2, E.shield + E.hp - 1, null, true);
|
||||
assert.equal(m2.dead, false);
|
||||
s.damage(m2, 1, null, true);
|
||||
assert.equal(m2.dead, true);
|
||||
});
|
||||
|
||||
test('Proxy shield is scaled with the wave like hp, regrows 10%/s after 3 s without damage, and the timer restarts on every hit', () => {
|
||||
const s = new Sim({ map: 'open', gold: 0 });
|
||||
const m = put(s, 'proxy', 10.5, 5.5, 12);
|
||||
const sh = Math.round(ENEMIES.proxy.shield * TD.hpScale(12));
|
||||
assert.equal(m.maxShield, sh);
|
||||
assert.equal(m.maxhp, Math.round(ENEMIES.proxy.hp * TD.hpScale(12)));
|
||||
s.damage(m, 400, null, true);
|
||||
const left = sh - 400;
|
||||
assert.equal(m.shield, left);
|
||||
tick(s, 2.8);
|
||||
assert.equal(m.shield, left, 'nothing regrows during the 3 s delay');
|
||||
tick(s, 1.2); // 4.0 s since the hit: one second of regrow
|
||||
assert.ok(Math.abs(m.shield - (left + 0.1 * sh)) < 0.5, 'regrew 10% of max in the first second after the delay: ' + (m.shield - left) + ' vs ' + 0.1 * sh);
|
||||
s.damage(m, 5, null, true);
|
||||
const after = m.shield;
|
||||
tick(s, 2.9);
|
||||
assert.equal(m.shield, after, 'a hit restarts the delay');
|
||||
tick(s, 60);
|
||||
assert.equal(m.shield, sh, 'regrows to the maximum and not beyond');
|
||||
assert.equal(m.hp, m.maxhp, 'the shield regrows, hp is not touched');
|
||||
// a daemon never tops up a shield, only hp
|
||||
s.damage(m, sh + 40, null, true);
|
||||
const d = put(s, 'daemon', 11.0, 5.5, 12);
|
||||
s.healPulse();
|
||||
assert.equal(m.shield, 0);
|
||||
assert.ok(Math.abs(m.hp - (m.maxhp - 40 + 0.06 * m.maxhp)) < 1e-6 || m.hp === m.maxhp);
|
||||
assert.ok(d.hp > 0);
|
||||
});
|
||||
|
||||
test('Botnet: a fast, tiny flyer that goes straight for the core and is hit by anti-air only', () => {
|
||||
const s = new Sim({ map: 'open', gold: 1000 });
|
||||
const E = ENEMIES.botnet;
|
||||
assert.ok(E.air && E.hp <= 15 && E.speed > ENEMIES.ghost.speed && E.reward === 1);
|
||||
const m = put(s, 'botnet', 12.5, 7.5);
|
||||
s.place('mortar', 12, 6);
|
||||
tick(s, 1);
|
||||
assert.equal(m.dead, false, 'a mortar cannot hit flyers');
|
||||
s.place('filter', 11, 6);
|
||||
tick(s, 1);
|
||||
assert.ok(m.dead || m.hp < m.maxhp, 'a filter can');
|
||||
// and it flies straight at the core at its own speed
|
||||
const s2 = new Sim({ map: 'open', gold: 0 });
|
||||
const f = put(s2, 'botnet', 0.5, 7.5);
|
||||
f.speed = E.speed;
|
||||
tick(s2, 2);
|
||||
assert.ok(Math.abs(f.x - (0.5 + 2 * E.speed * Math.cos(Math.atan2(8 - 7.5, 25.5 - 0.5)))) < 0.1, 'x=' + f.x);
|
||||
});
|
||||
|
||||
test('Sandbox stuns everything in range (ground and air), not what is outside, and stunned enemies do not move', () => {
|
||||
const s = new Sim({ map: 'open', gold: 1000 });
|
||||
s.place('sandbox', 2, 6); // centre (2.5, 6.5), range 2.6
|
||||
const L = TOWERS.sandbox.levels[0];
|
||||
const ground = put(s, 'ping', 0.5, 7.5), air = put(s, 'ghost', 3.5, 7.5), out = put(s, 'ping', 8.5, 7.5);
|
||||
for (const m of [ground, air, out]) m.speed = ENEMIES[m.kind].speed;
|
||||
const gx = ground.x, ax = air.x, ox = out.x;
|
||||
tick(s, L.stun * 0.9);
|
||||
assert.ok(ground.stunT > 0 && air.stunT > 0, 'ground and air are stunned');
|
||||
assert.equal(out.stunT, 0, 'out of range: untouched');
|
||||
assert.equal(ground.x, gx, 'stunned ground unit stands still');
|
||||
assert.equal(air.x, ax, 'stunned flyer stands still');
|
||||
assert.ok(out.x > ox, 'the unit outside keeps moving');
|
||||
assert.equal(s.snapshot().mo.find((e) => e[0] === ground.id)[6], 1, 'stunned units are flagged in the snapshot (the 0/1 slow flag)');
|
||||
tick(s, L.stun * 0.3);
|
||||
assert.ok(ground.x > gx, 'moves again once the stun is over');
|
||||
const ev = s.drainEvents().filter((e) => e.e === 'stun');
|
||||
assert.equal(ev.length, 1);
|
||||
assert.equal(ev[0].n, 2);
|
||||
});
|
||||
|
||||
test('Sandbox: pulse cadence by level, only pulses when something is in range, stun does not stack, bosses get half', () => {
|
||||
const s = new Sim({ map: 'open', gold: 10000 });
|
||||
const r = s.place('sandbox', 5, 5), t = s.towerById(r.id);
|
||||
tick(s, 12);
|
||||
assert.equal(s.drainEvents().filter((e) => e.e === 'stun').length, 0, 'nothing in range: no pulse and the charge is kept');
|
||||
assert.equal(t.cd, 0);
|
||||
const boss = put(s, 'rootkit', 6.5, 6.5, 10), ping = put(s, 'ping', 5.5, 7.0);
|
||||
const L1 = TOWERS.sandbox.levels[0];
|
||||
tick(s, 0.1);
|
||||
assert.ok(Math.abs(ping.stunT - (L1.stun - 0.1 + STEP)) < 0.05, 'a ping is stunned for the full time');
|
||||
assert.ok(Math.abs(boss.stunT - (L1.stun * (1 - ENEMIES.rootkit.slowRes) - 0.1 + STEP)) < 0.05, 'a boss for half');
|
||||
assert.ok(boss.stunT < ping.stunT);
|
||||
tick(s, 8.8); // 8.9 s: pulses at 0 and 5
|
||||
assert.equal(s.drainEvents().filter((e) => e.e === 'stun').length, 2);
|
||||
// level 3 pulses faster and stuns longer; a second pulse never adds to a running stun, it only refreshes it
|
||||
s.upgrade(t.id); s.upgrade(t.id);
|
||||
const L3 = TOWERS.sandbox.levels[2];
|
||||
assert.ok(L3.pulse < L1.pulse && L3.stun > L1.stun && L3.range > L1.range);
|
||||
s.drainEvents();
|
||||
t.cd = 0; ping.stunT = 0.5;
|
||||
tick(s, 0.05);
|
||||
assert.ok(Math.abs(ping.stunT - (L3.stun - 0.05 + STEP)) < 0.05 && ping.stunT <= L3.stun, 'stun is set to the pulse stun, not added');
|
||||
t.cd = 0; ping.stunT = L3.stun * 2; // an already longer stun stays
|
||||
s.tick(STEP);
|
||||
assert.ok(ping.stunT > L3.stun, 'a shorter stun never cuts a longer one');
|
||||
s.drainEvents();
|
||||
tick(s, 12.1);
|
||||
assert.equal(s.drainEvents().filter((e) => e.e === 'stun').length, 3, 'level 3 recharges every ' + L3.pulse + ' s');
|
||||
});
|
||||
|
||||
test('Load Balancer: boosts attack towers in range, best boost only, not itself, not control/aura towers, ends when sold', () => {
|
||||
const s = new Sim({ map: 'open', gold: 100000 });
|
||||
const f1 = s.towerById(s.place('filter', 10, 5).id); // 1 cell from the balancer at (11,5)
|
||||
const fFar = s.towerById(s.place('filter', 14, 5).id); // 3 cells: outside the level 1 and 2 range, exactly inside level 3
|
||||
const sb = s.towerById(s.place('sandbox', 10, 6).id);
|
||||
const th = s.towerById(s.place('throttle', 9, 5).id);
|
||||
const wl = s.towerById(s.place('wall', 11, 6).id);
|
||||
const b1 = s.towerById(s.place('balancer', 11, 5).id);
|
||||
const B = TOWERS.balancer.levels;
|
||||
s.updateAuras();
|
||||
assert.equal(f1.buff, B[0].boost);
|
||||
assert.equal(fFar.buff, 0, 'out of range');
|
||||
for (const t of [sb, th, wl, b1]) assert.equal(t.buff, 0, t.type + ' is not an attack tower');
|
||||
// upgrading makes it stronger; a second, weaker balancer never adds to it
|
||||
s.upgrade(b1.id);
|
||||
s.updateAuras();
|
||||
assert.equal(f1.buff, B[1].boost);
|
||||
assert.equal(fFar.buff, 0);
|
||||
const b2 = s.towerById(s.place('balancer', 10, 4).id);
|
||||
s.updateAuras();
|
||||
assert.equal(f1.buff, B[1].boost, 'the best boost applies, they do not stack (not 0.15 + 0.25)');
|
||||
assert.equal(b1.buff, 0, 'a balancer does not boost another balancer'); assert.equal(b2.buff, 0);
|
||||
s.upgrade(b1.id);
|
||||
s.updateAuras();
|
||||
assert.equal(f1.buff, B[2].boost);
|
||||
assert.equal(fFar.buff, B[2].boost, 'level 3 reaches 3 cells');
|
||||
// selling the strong one drops back to the weak one, selling the last ends the boost
|
||||
s.sell(b1.id);
|
||||
s.updateAuras();
|
||||
assert.equal(f1.buff, B[0].boost);
|
||||
assert.equal(fFar.buff, 0);
|
||||
s.sell(b2.id);
|
||||
s.tick(STEP);
|
||||
assert.equal(f1.buff, 0, 'selling the last balancer ends the boost');
|
||||
});
|
||||
|
||||
test('Load Balancer: cooldowns really elapse faster, so a boosted tower fires more often', () => {
|
||||
const mk = (withBalancer) => {
|
||||
const s = new Sim({ map: 'open', gold: 10000 });
|
||||
const t = s.towerById(s.place('ids', 10, 5).id);
|
||||
if (withBalancer) s.place('balancer', 11, 5);
|
||||
const target = put(s, 'trojan', 10.5, 7.5, 1);
|
||||
target.hp = target.maxhp = 1e12; target.armor = 0;
|
||||
return { s, t, target };
|
||||
};
|
||||
const a = mk(false), b = mk(true);
|
||||
a.t.cd = b.t.cd = 1;
|
||||
a.s.tick(0.1); b.s.tick(0.1);
|
||||
assert.ok(Math.abs(a.t.cd - 0.9) < 1e-9);
|
||||
assert.ok(Math.abs(b.t.cd - (1 - 0.1 * (1 + TOWERS.balancer.levels[0].boost))) < 1e-9, 'cooldown ran 15% faster');
|
||||
const shots = (x) => { tick(x.s, 40); return x.s.drainEvents().filter((e) => e.e === 'shot' && e.k === 'ids').length; };
|
||||
const na = shots(a), nb = shots(b);
|
||||
assert.ok(na >= 25, 'baseline fires ~28 shots in 40 s: ' + na);
|
||||
assert.ok(nb > na * 1.1 && nb < na * 1.2, 'boosted ' + nb + ' vs ' + na);
|
||||
});
|
||||
|
||||
test('waves 1-8 are exactly the original introduction', () => {
|
||||
const W = [
|
||||
[{ k: 'ping', c: 8, gap: 0.7 }],
|
||||
[{ k: 'worm', c: 11, gap: 0.4 }],
|
||||
[{ k: 'ping', c: 9, gap: 0.6 }, { k: 'trojan', c: 1, gap: 1.4 }],
|
||||
[{ k: 'spam', c: 30, gap: 0.18 }],
|
||||
[{ k: 'ghost', c: 7, gap: 0.9 }, { k: 'ping', c: 8, gap: 0.5 }],
|
||||
[{ k: 'trojan', c: 4, gap: 1.2 }, { k: 'worm', c: 11, gap: 0.35 }],
|
||||
[{ k: 'ghost', c: 12, gap: 0.6 }, { k: 'spam', c: 24, gap: 0.2 }],
|
||||
[{ k: 'ping', c: 16, gap: 0.4 }, { k: 'trojan', c: 5, gap: 1 }, { k: 'ghost', c: 6, gap: 0.8 }],
|
||||
];
|
||||
for (let n = 1; n <= 8; n++) assert.deepEqual(TD.waveGroups(n), W[n - 1], 'wave ' + n);
|
||||
for (let n = 1; n <= 8; n++) assert.ok(!TD.waveGroups(n).some((g) => ['daemon', 'proxy', 'botnet'].includes(g.k)));
|
||||
});
|
||||
|
||||
test('new kinds are woven into the waves from 9 on: daemons with trojans, proxies from 12, botnets from 14', () => {
|
||||
const first = {};
|
||||
for (let n = 1; n <= 60; n++) {
|
||||
const g = TD.waveGroups(n);
|
||||
for (const e of g) {
|
||||
assert.ok(ENEMIES[e.k], 'wave ' + n + ' kind ' + e.k);
|
||||
assert.ok(Number.isInteger(e.c) && e.c >= 1 && e.gap > 0, 'wave ' + n);
|
||||
if (first[e.k] === undefined) first[e.k] = n;
|
||||
}
|
||||
assert.ok(g.length <= 8, 'wave ' + n + ' has a sane number of groups');
|
||||
const t = g.reduce((a, e) => a + e.c * e.gap + 1.5, 0);
|
||||
assert.ok(t < 120, 'wave ' + n + ' finishes spawning within two minutes (' + t.toFixed(0) + ' s)');
|
||||
if (n >= 9 && g.some((e) => e.k === 'trojan')) {
|
||||
const tr = g.find((e) => e.k === 'trojan'), dm = g.find((e) => e.k === 'daemon');
|
||||
assert.ok(dm && dm.c >= 1, 'wave ' + n + ' has a daemon escort');
|
||||
assert.ok(g.indexOf(dm) > g.indexOf(tr), 'the escort spawns behind the trojans');
|
||||
assert.ok(dm.c <= tr.c, 'the escort is not bigger than the trojan group');
|
||||
}
|
||||
}
|
||||
assert.equal(first.daemon, 9);
|
||||
assert.equal(first.proxy, 12);
|
||||
assert.equal(first.botnet, 14);
|
||||
for (const k of TD.ENEMY_ORDER) assert.ok(first[k] !== undefined, k + ' appears somewhere in waves 1-60');
|
||||
for (let n = 9; n <= 60; n++) {
|
||||
const kinds = TD.waveGroups(n).map((e) => e.k);
|
||||
if (n < 12) assert.ok(!kinds.includes('proxy'), 'no proxy before 12');
|
||||
if (n < 14) assert.ok(!kinds.includes('botnet'), 'no botnet before 14');
|
||||
}
|
||||
// the late waves combine them
|
||||
assert.ok(['daemon', 'proxy', 'botnet'].every((k) => TD.waveGroups(30).some((e) => e.k === k)), 'wave 30 has all three');
|
||||
assert.ok(TD.waveGroups(20).some((e) => e.k === 'rootkit') && TD.waveGroups(20).some((e) => e.k === 'proxy'));
|
||||
// the loop keeps growing: the same slot gets more units later
|
||||
const cnt = (n, k) => TD.waveGroups(n).filter((e) => e.k === k).reduce((a, e) => a + e.c, 0);
|
||||
assert.ok(cnt(22, 'botnet') > cnt(14, 'botnet') && cnt(36, 'proxy') > cnt(12, 'proxy'));
|
||||
});
|
||||
|
||||
test('waveSummary lists one entry per kind with counts and flags flyers', () => {
|
||||
for (let n = 1; n <= 60; n++) {
|
||||
const sum = TD.waveSummary(n), groups = TD.waveGroups(n);
|
||||
assert.equal(new Set(sum.map((e) => e.k)).size, sum.length, 'one entry per kind in wave ' + n);
|
||||
for (const e of sum) {
|
||||
assert.equal(e.air, !!ENEMIES[e.k].air, 'wave ' + n + ' ' + e.k + ' air flag');
|
||||
assert.equal(e.c, groups.filter((g) => g.k === e.k).reduce((a, g) => a + g.c, 0));
|
||||
}
|
||||
assert.equal(sum.reduce((a, e) => a + e.c, 0), groups.reduce((a, g) => a + g.c, 0));
|
||||
}
|
||||
assert.deepEqual(TD.waveSummary(5), [{ k: 'ghost', c: 7, air: true }, { k: 'ping', c: 8, air: false }]);
|
||||
assert.ok(TD.waveSummary(14).some((e) => e.k === 'botnet' && e.air === true));
|
||||
assert.ok(TD.waveSummary(7).some((e) => e.k === 'ghost' && e.air));
|
||||
});
|
||||
|
||||
test('the new waves run end to end and the snapshot (incl. daemon, proxy, botnet) survives JSON', () => {
|
||||
const s = new Sim({ map: 'twin', gold: 0, lives: 1000 });
|
||||
s.wave = 14; // every new kind in the queue at once
|
||||
s.spawnQ.push({ t: 0, k: 'daemon', wave: 14 }, { t: 0, k: 'proxy', wave: 14 }, { t: 0, k: 'botnet', wave: 14 }, { t: 0, k: 'trojan', wave: 14 });
|
||||
s.spawnQ.push(...TD.waveGroups(14).flatMap((g, gi) => Array.from({ length: g.c }, (_, i) => ({ t: gi * 5 + i * g.gap, k: g.k, wave: 14 }))));
|
||||
s.spawnQ.sort((a, b) => a.t - b.t);
|
||||
let sawNew = false, sawShield = false;
|
||||
for (let i = 0; i < 60 * 25 && s.state === 'playing'; i++) {
|
||||
s.tick(STEP);
|
||||
if (i % 30 === 0) {
|
||||
const snap = JSON.parse(JSON.stringify(s.snapshot()));
|
||||
assert.deepEqual(snap, JSON.parse(JSON.stringify(snap)));
|
||||
for (const e of snap.mo) {
|
||||
assert.equal(e.length, 7);
|
||||
assert.ok(Number.isInteger(e[1]) && e[1] >= 0 && e[1] < TD.ENEMY_ORDER.length, 'kindIndex indexes ENEMY_ORDER');
|
||||
assert.ok(e[4] > 0 && e[4] <= e[5], 'hp (incl. shield) within maxhp (incl. maxShield)');
|
||||
if (e[1] >= 6) sawNew = true;
|
||||
if (TD.ENEMY_ORDER[e[1]] === 'proxy') { sawShield = true; assert.ok(e[5] > ENEMIES.proxy.hp * TD.hpScale(14), 'proxy maxhp includes the shield'); }
|
||||
}
|
||||
}
|
||||
}
|
||||
assert.ok(sawNew && sawShield);
|
||||
assert.ok(s.leaks > 0, 'undefended waves leak');
|
||||
assert.equal(JSON.stringify(s.fullState()).includes('null'), false);
|
||||
});
|
||||
|
||||
test('Orbital Uplink pricing: very expensive, and every one you own makes the next dearer', () => { // (25% per owned one, for buying and for upgrading)
|
||||
const base = TOWERS.uplink.levels[0].cost;
|
||||
assert.ok(base >= 8 * TD.START_GOLD, 'the first Uplink costs many times the opening credits');
|
||||
assert.ok(base >= 20 * TOWERS.ids.levels[0].cost, 'and a lot more than an IDS Sniper');
|
||||
// the shared helper: arrays of towers and plain counts agree; other towers have a flat price
|
||||
assert.equal(TD.priceFor('uplink', 0), base);
|
||||
assert.equal(TD.priceFor('uplink', [{ type: 'uplink' }, { type: 'filter' }, { type: 'uplink' }]), TD.priceFor('uplink', 2));
|
||||
assert.equal(TD.priceFor('filter', 50), TOWERS.filter.levels[0].cost, 'a Packet Filter never gets dearer');
|
||||
assert.equal(TD.priceFor('wall', [{ type: 'wall' }, { type: 'wall' }]), TOWERS.wall.levels[0].cost);
|
||||
for (let n = 1; n < 6; n++) assert.ok(TD.priceFor('uplink', n) >= 1.2 * TD.priceFor('uplink', n - 1), 'each Uplink is about 25% dearer than the one before');
|
||||
|
||||
const s = new Sim({ map: 'open', gold: 1e6 });
|
||||
const ids = [];
|
||||
let prev = 0;
|
||||
for (let i = 0; i < 4; i++) {
|
||||
const want = TD.priceFor('uplink', s.towers);
|
||||
assert.ok(want > prev, 'price keeps rising');
|
||||
prev = want;
|
||||
const g = s.gold;
|
||||
const r = s.place('uplink', 4 + 2 * i, 6);
|
||||
assert.ok(r.ok);
|
||||
assert.equal(g - s.gold, want, 'the sim charges exactly the price the UI shows');
|
||||
assert.equal(s.towerById(r.id).spent, want, 'spent records what was actually paid');
|
||||
ids.push(r.id);
|
||||
}
|
||||
// refunds follow what each one cost, and selling brings the price back down
|
||||
const before = TD.priceFor('uplink', s.towers);
|
||||
const g0 = s.gold, t0 = s.towerById(ids[0]);
|
||||
const refund = s.sell(ids[0]).refund;
|
||||
assert.equal(refund, Math.floor(t0.spent * TD.SELL_RATIO));
|
||||
assert.equal(s.gold, g0 + refund);
|
||||
assert.ok(TD.priceFor('uplink', s.towers) < before, 'owning fewer makes the next one cheaper again');
|
||||
// not enough credits for the dearer one: refused without charging
|
||||
const poor = new Sim({ map: 'open', gold: base });
|
||||
assert.ok(poor.place('uplink', 5, 5).ok);
|
||||
poor.gold = TD.priceFor('uplink', poor.towers) - 10;
|
||||
const gp = poor.gold;
|
||||
assert.equal(poor.place('uplink', 8, 5).reason, 'gold');
|
||||
assert.equal(poor.gold, gp);
|
||||
});
|
||||
|
||||
test('Orbital Uplink upgrades get dearer by the same factor as the tower they belong to', () => {
|
||||
const s = new Sim({ map: 'open', gold: 1e7 });
|
||||
const a = s.towerById(s.place('uplink', 4, 6).id), b = s.towerById(s.place('uplink', 8, 6).id), c = s.towerById(s.place('uplink', 12, 6).id);
|
||||
assert.equal(a.pm, 1);
|
||||
assert.ok(Math.abs(b.pm - TOWERS.uplink.priceGrowth) < 1e-9 && Math.abs(c.pm - TOWERS.uplink.priceGrowth ** 2) < 1e-9, 'each remembers the multiplier it was bought at');
|
||||
for (const t of [a, b, c]) {
|
||||
const m = t.pm;
|
||||
const g0 = s.gold, up1 = TD.upgradePrice('uplink', 1, m);
|
||||
assert.equal(s.upgradeCost(t), up1, 'the sim and the shared helper agree');
|
||||
assert.ok(Math.abs(up1 / TOWERS.uplink.levels[1].cost - m) < 0.01 * m + 0.01, 'level 2 costs the base upgrade times the multiplier');
|
||||
assert.ok(s.upgrade(t.id).ok);
|
||||
assert.equal(g0 - s.gold, up1);
|
||||
assert.equal(t.spent, TD.priceFor('uplink', t.pm === 1 ? 0 : Math.round(Math.log(t.pm) / Math.log(TOWERS.uplink.priceGrowth))) + up1);
|
||||
}
|
||||
assert.ok(TD.upgradePrice('uplink', 3, c.pm) > TD.upgradePrice('uplink', 3, a.pm) * 1.5, 'the third Uplink is much dearer to upgrade than the first');
|
||||
// flat-price towers are untouched
|
||||
const f = s.towerById(s.place('filter', 6, 10).id);
|
||||
assert.equal(f.pm, 1);
|
||||
assert.equal(TD.upgradePrice('filter', 1, f.pm), TOWERS.filter.levels[1].cost);
|
||||
// a tower bought after another was sold gets the lower multiplier back
|
||||
s.sell(a.id);
|
||||
const d = s.towerById(s.place('uplink', 16, 6).id);
|
||||
assert.ok(d.pm < c.pm * 1.0001 && d.pm >= b.pm - 1e-9, 'owning fewer when you buy lowers the multiplier again');
|
||||
// the multiplier travels in the tower list so a co-op client can show the same upgrade price
|
||||
const row = s.towerList().find((r) => r[0] === c.id);
|
||||
assert.equal(row.length, 9);
|
||||
assert.ok(Math.abs(row[8] - c.pm) < 0.002);
|
||||
assert.equal(s.towerList().find((r) => r[0] === f.id)[8], 1);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue