'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'); });