'use strict'; /* Garrisons at backbone landing sites (docs/DECISIONS.md E6): members of the holding alliance station units in a site, the units defend it on top * of its static defence, attacks resolve in arrival order (a lost wave kills a share of the garrison, the next wave faces less), a won attack * wipes it and takes the site. Plus the edges: nobody but the holder can reinforce, units are never in two places, withdraw/recall while under * attack, leaving the alliance, power. Fake clock, in-memory db; the world is moved to day 100 by advancing the clock. */ const test = require('node:test'); const assert = require('node:assert'); const T = require('./military-helpers'); const A = require('../server/alliances'); const E = require('../server/endgame'); const G = require('../server/military/garrison'); const { createEndgameQueries } = require('../server/endgameQueries'); const { loadTown } = require('../server/state'); const { R, D, H } = T; const DAY = 24 * H; function alliance(h, name, leader, members) { const id = Number(h.db.run('INSERT INTO alliances (world_id, name, tag, size_cap, created_by, created_at) VALUES (?, ?, ?, 10, ?, ?)', h.worldId, name, name.slice(0, 3).toUpperCase(), leader.opId, h.clock.now()).lastInsertRowid); A.addMember(h.db, id, leader.opId, 'leader', h.clock.now()); for (const m of members || []) A.addMember(h.db, id, m.opId, 'member', h.clock.now()); return id; } const err = (fn) => { try { fn(); } catch (e) { return e; } assert.fail('expected an error'); }; function world(day, o) { const h = T.setup(Object.assign({ seed: 4242, radius: 50 }, o)); h.advanceTo(h.world.start_at + Math.round(day * DAY / (h.world.speed || 1))); h.eg = createEndgameQueries(h); h.sites = R.backboneSites(h.world.seed, h.world.map_radius); return h; } // An operator next to site `i` with plenty of power so that its units stay online; no production unless asked. function near(h, name, i, o) { const s = h.sites[i]; const op = T.addOperator(h, name, Object.assign({ x: s.x - 4, y: s.y, power: 25 }, o || {})); if (!(o && o.produce)) h.db.run("UPDATE buildings SET level = 0 WHERE town_id = ? AND key LIKE 'f%' AND key NOT IN ('f13','f14','f15','f16')", op.townId); return op; } const siteOf = (h, i) => h.db.get('SELECT * FROM landing_sites WHERE world_id = ? AND idx = ?', h.worldId, i); function hold(h, i, allianceId, opId) { E.ensureSites(h.db, h.world, h.clock.now()); h.db.run('UPDATE landing_sites SET holder_alliance_id = ?, holder_operator_id = ? WHERE idx = ?', allianceId, opId, i); } const sendTo = (h, who, i, type, units) => h.run('sendMovement', { userId: who.userId, fromTownId: who.townId, type, x: h.sites[i].x, y: h.sites[i].y, units }); const staticHeld = (h, i, level, held) => R.backboneDefence(h.world.seed, h.sites[i].x, h.sites[i].y, R.worldAgeDays(h.world.start_at, h.clock.now(), h.world.speed), level || 0, held !== false); const garrisonAt = (h, i) => { const s = siteOf(h, i); const out = {}; for (const g of G.groups(h.db, s.id)) for (const k of Object.keys(g.units)) out[k] = (out[k] || 0) + g.units[k]; return out; }; // Every unit in the world, wherever it is: home, stationed in a POP, in a garrison, in a movement that has not arrived. function totals(h) { const out = {}; const add = (u, n) => { out[u] = (out[u] || 0) + n; }; for (const r of h.db.all('SELECT unit, SUM(count) AS n FROM town_units GROUP BY unit')) add(r.unit, r.n); for (const r of h.db.all('SELECT unit, SUM(count) AS n FROM stationed GROUP BY unit')) add(r.unit, r.n); for (const r of h.db.all('SELECT unit, SUM(count) AS n FROM site_stationed GROUP BY unit')) add(r.unit, r.n); for (const m of h.db.all("SELECT units FROM movements WHERE status = 'out'")) { const u = JSON.parse(m.units); for (const k of Object.keys(u)) add(k, u[k]); } return out; } const draw = (h, who) => loadTown(h.db, who.townId).unitDraw; const lastReport = (h, who, type) => T.reportsOf(h, who.userId).filter((r) => r.type === type).pop(); // alice (leader) + alice2 (member) hold site 0 for ALP; bob + bob2 are BRA; 'loner' has no alliance function scene(o) { const h = world(100, o); const s0 = h.sites[0]; const a = near(h, 'alice', 0, { units: { soc_analyst: 600 } }); const a2 = near(h, 'alice2', 0, { x: s0.x - 4, y: s0.y + 3, units: { blue_team: 100 } }); const b = near(h, 'bob', 0, { x: s0.x + 4, y: s0.y }); const b2 = near(h, 'bob2', 0, { x: s0.x + 4, y: s0.y + 3 }); const loner = near(h, 'loner', 0, { x: s0.x, y: s0.y - 5, units: { soc_analyst: 10 } }); const A1 = alliance(h, 'Alpha', a, [a2]), B1 = alliance(h, 'Bravo', b, [b2]); hold(h, 0, A1, a.opId); return Object.assign(h, { a, a2, b, b2, loner, A1, B1 }); } // ---------------------------------------------------------------- pure rules test('rules: a held site is much weaker than an unheld one; battle = static + garrison; the static part never loses anything; transport capacity scales with speed', () => { const seed = 4242, x = 10, y = 10; const open = R.backboneDefence(seed, x, y, 100, 0, false), heldD = R.backboneDefence(seed, x, y, 100, 0, true); assert.equal(R.backboneDefence(seed, x, y, 100, 0), open, 'unheld is the default (the opening scramble)'); assert.ok(open > 5 * heldD && heldD > 0, 'held = ' + heldD + ', unheld = ' + open); assert.ok(R.backboneDefence(seed, x, y, 100, 50, true) > heldD, 'node levels still raise it'); const grp = (n) => ({ id: 'g', units: { soc_analyst: n }, research: {} }); const none = R.resolveBattle({ kind: 'attack', attackers: { units: { script_crew: 100 }, research: {} }, defenders: [], firewall: 0, staticDefence: 3000 }); assert.equal(none.D, 3000); assert.equal(none.defenderLossFraction, 0, 'no garrison, nothing to lose'); assert.equal(none.attackerWins, true); const b = R.resolveBattle({ kind: 'attack', attackers: { units: { script_crew: 100 }, research: {} }, defenders: [grp(100)], firewall: 0, staticDefence: 3000 }); assert.equal(b.D, 3000 + 100 * 25, 'static + raw defence, no firewall, no base defence'); assert.equal(b.attackerWins, false); assert.ok(b.defenderLossFraction > 0 && b.defenderLossFraction < 1); assert.equal(b.defenders[0].lost.soc_analyst, Math.round(100 * Math.pow(4000 / 5500, 1.5))); const win = R.resolveBattle({ kind: 'attack', attackers: { units: { script_crew: 200 }, research: {} }, defenders: [grp(100)], firewall: 0, staticDefence: 3000 }); assert.equal(win.attackerWins, true); assert.equal(win.defenders[0].lost.soc_analyst, 100, 'a won attack wipes the garrison'); // a POP battle is untouched by the new mode const pop = R.resolveBattle({ kind: 'attack', attackers: { units: { script_crew: 100 }, research: {} }, defenders: [grp(100)], firewall: 0 }); assert.equal(pop.D, (100 * 25 + D.MILITARY.defenceBase) * R.firewallMult(0)); assert.equal(R.transportCapacity(1), D.TRANSPORT.capacityPerLink); assert.equal(R.transportCapacity(3), 3 * D.TRANSPORT.capacityPerLink); assert.equal(R.transportCapacity(undefined), D.TRANSPORT.capacityPerLink); }); // ---------------------------------------------------------------- stationing test('reinforce a site: only members of the holding alliance; the units leave home, join the garrison on arrival and draw their owner\'s power', () => { const h = scene(); const { a, a2, b, loner } = h; T.giveUnits(h, b.townId, { soc_analyst: 20 }); assert.equal(err(() => sendTo(h, b, 0, 'reinforce', { soc_analyst: 10 })).details.reason, 'not_holder', 'a rival cannot'); assert.equal(err(() => sendTo(h, loner, 0, 'reinforce', { soc_analyst: 10 })).details.reason, 'not_holder', 'no alliance cannot'); assert.equal(err(() => sendTo(h, a, 1, 'reinforce', { soc_analyst: 10 })).details.reason, 'not_holder', 'not a site your alliance holds'); assert.equal(err(() => h.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'reinforce', x: h.sites[0].x + 1, y: h.sites[0].y, units: { soc_analyst: 1 } })).code, 'invalid_target'); const before = totals(h); const d0 = draw(h, a); assert.equal(d0, 600); const mv = sendTo(h, a, 0, 'reinforce', { soc_analyst: 400 }); assert.equal(draw(h, a), 200, 'units on the road draw nothing'); assert.equal(T.units(h, a.townId).soc_analyst, 200); assert.equal(h.db.get('SELECT COUNT(*) AS n FROM site_stationed').n, 0); assert.deepEqual(totals(h), before, 'in a movement, counted once'); assert.equal(T.notes(h, a2.userId, 'backbone_incoming').length, 0, 'own members are not warned about the own reinforcement'); h.advanceTo(mv.arriveAt); assert.deepEqual(garrisonAt(h, 0), { soc_analyst: 400 }); assert.equal(draw(h, a), 600, 'the owner pays for them again, nobody else'); assert.equal(draw(h, a2), 100 * D.UNITS.blue_team.draw); assert.equal(h.db.get('SELECT COUNT(*) AS n FROM stationed').n, 0, 'not a POP reinforcement'); assert.deepEqual(totals(h), Object.assign({}, before), 'counted once'); assert.equal(T.notes(h, a.userId, 'garrison_arrived').length, 1); assert.equal(h.db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'failed'").n, 0); // the POP view lists them (they draw this POP's power, so they must not just vanish from it) const mu = h.queries.town(a.userId, a.townId).town.military.units; assert.deepEqual(mu.garrisoned, [{ siteId: siteOf(h, 0).id, x: h.sites[0].x, y: h.sites[0].y, units: { soc_analyst: 400 } }]); assert.equal(T.units(h, a.townId).soc_analyst, 200); assert.deepEqual(h.queries.town(b.userId, b.townId).town.military.units.garrisoned, []); // a second member adds to the same site; groups are per owner POP const mv2 = sendTo(h, a2, 0, 'reinforce', { blue_team: 100 }); h.advanceTo(mv2.arriveAt); assert.equal(G.groups(h.db, siteOf(h, 0).id).length, 2); const view = h.eg.site(a.userId, siteOf(h, 0).id); assert.equal(view.you.canReinforce, true); assert.equal(view.garrison.length, 2); assert.deepEqual(view.garrison.map((g) => g.mine), [true, false]); assert.equal(view.defence.static, staticHeld(h, 0, 0)); assert.equal(view.defence.garrison, 400 * 25 + 100 * 80); assert.equal(view.defence.total, view.defence.static + view.defence.garrison); assert.equal(h.eg.site(b.userId, siteOf(h, 0).id).garrison, undefined, 'the garrison is the holders\' business (a scan shows it)'); assert.equal(h.eg.site(b.userId, siteOf(h, 0).id).you.canReinforce, false); }); test('a scan of a held site shows the garrison once: defence = static + garrison, units not doubled', () => { const h = scene(); const { a, b } = h; const mv = sendTo(h, a, 0, 'reinforce', { soc_analyst: 300 }); h.advanceTo(mv.arriveAt); T.giveUnits(h, b.townId, { port_scanner: 200 }); const sc = sendTo(h, b, 0, 'scan', { port_scanner: 200 }); assert.equal(T.notes(h, a.userId, 'backbone_incoming').length, 1, 'the holders are told something is coming (no type, no sender)'); assert.deepEqual(Object.keys(T.notes(h, a.userId, 'backbone_incoming')[0]).sort(), ['arriveAt', 'index', 'siteId', 'x', 'y']); h.advanceTo(sc.arriveAt); const rep = lastReport(h, b, 'scan').data; assert.equal(rep.success, true); assert.deepEqual(rep.intel.garrison.units, { soc_analyst: 300 }); assert.equal(rep.intel.garrison.power, 300 * 25); assert.equal(rep.intel.static, staticHeld(h, 0, 0)); assert.equal(rep.intel.defence, staticHeld(h, 0, 0) + 300 * 25); assert.equal(rep.intel.holder.allianceId, h.A1); }); // ---------------------------------------------------------------- the fight test('waves: a lost wave kills part of the garrison and nothing else, the next wave faces less and takes the site; the garrison is wiped, the site changes hands', () => { const h = scene(); const { a, b, b2 } = h; const mv = sendTo(h, a, 0, 'reinforce', { soc_analyst: 400 }); h.advanceTo(mv.arriveAt); const S = staticHeld(h, 0, 0), G0 = 400 * 25; // wave 1: more than the static part, less than static + garrison const n1 = Math.ceil((S + 0.8 * G0) / 40); assert.ok(n1 * 40 > S && n1 * 40 < S + G0); T.giveUnits(h, b.townId, { script_crew: n1 }); // wave 2 is sized for what is LEFT after wave 1 (computed from the rules), and must be smaller than the whole defence const f1 = Math.pow((n1 * 40) / (S + G0), 1.5); const left = 400 - Math.round(400 * f1); const n2 = Math.ceil((S + left * 25) * 1.02 / 40); assert.ok(n2 * 40 < S + G0, 'the second wave alone could not have taken the site'); T.giveUnits(h, b2.townId, { script_crew: n2 }); const w1 = sendTo(h, b, 0, 'attack', { script_crew: n1 }); const w2 = sendTo(h, b2, 0, 'attack', { script_crew: n2 }); assert.ok(w1.arriveAt <= w2.arriveAt, 'bob first (the same distance, lower id first)'); assert.equal(T.notes(h, a.userId, 'backbone_incoming').length, 2); const unitsBefore = totals(h); h.advanceTo(w1.arriveAt); assert.equal(siteOf(h, 0).holder_alliance_id, h.A1, 'wave 1 lost'); assert.deepEqual(garrisonAt(h, 0), { soc_analyst: left }, 'the garrison lost ratio^1.5 of its units'); const r1 = lastReport(h, b, 'landing').data; assert.equal(r1.outcome, 'defender_won'); assert.equal(r1.power.static, S); assert.equal(r1.power.garrison, G0); assert.equal(r1.defender.garrison[0].lost.soc_analyst, 400 - left); assert.equal(T.units(h, b.townId).script_crew, undefined, 'a lost attack loses everything'); assert.equal(lastReport(h, a, 'landing').role, 'defender', 'the garrison owner gets the report'); assert.equal(h.db.get('SELECT defense_points FROM operator_stats WHERE operator_id = ?', a.opId).defense_points > 0, true); assert.equal(h.db.get('SELECT attack_points FROM operator_stats WHERE operator_id = ?', b.opId).attack_points > 0, true); assert.equal(draw(h, a), 200 + left, 'the dead no longer draw power'); h.advanceTo(w2.arriveAt); const site = siteOf(h, 0); assert.equal(site.holder_alliance_id, h.B1, 'wave 2 took it'); assert.equal(site.holder_operator_id, b2.opId); assert.deepEqual(garrisonAt(h, 0), {}, 'a won attack wipes the garrison'); const r2 = lastReport(h, b2, 'landing').data; assert.equal(r2.outcome, 'attacker_won'); assert.equal(r2.power.defence, S + left * 25); assert.equal(T.notes(h, a.userId, 'backbone_lost').length, 1); assert.equal(draw(h, a), 200, 'and the wiped ones neither'); // nothing was created: units before = units after + the dead (units of the survivors are in movements) const after = totals(h); const dead = { soc_analyst: 400 - 0, script_crew: n1 + n2 }; assert.equal(unitsBefore.soc_analyst - after.soc_analyst, 400, 'all 400 analysts are gone (lost in the two battles), none duplicated'); assert.ok(unitsBefore.script_crew - after.script_crew <= dead.script_crew && unitsBefore.script_crew - after.script_crew >= n1, 'wave 1 died, wave 2 lost part of its crews'); // the new holder can now garrison, the old one cannot T.giveUnits(h, b.townId, { soc_analyst: 5 }); assert.equal(err(() => sendTo(h, a, 0, 'reinforce', { soc_analyst: 1 })).details.reason, 'not_holder'); assert.ok(sendTo(h, b, 0, 'reinforce', { soc_analyst: 5 }).movementId); assert.equal(h.db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'failed'").n, 0); }); test('an attack that beats the static part but not the garrison loses; without the garrison the same attack wins (withdraw at T-1)', () => { const h = scene(); const { a, b } = h; const mv = sendTo(h, a, 0, 'reinforce', { soc_analyst: 400 }); h.advanceTo(mv.arriveAt); const S = staticHeld(h, 0, 0); const n = Math.ceil(1.3 * S / 40); assert.ok(n * 40 > S && n * 40 < S + 400 * 25); T.giveUnits(h, b.townId, { script_crew: 2 * n }); const atk = sendTo(h, b, 0, 'attack', { script_crew: n }); const before = totals(h); // alice calls the garrison home one millisecond before the attack arrives: the attack meets the static part only h.advanceTo(atk.arriveAt - 1); const wd = h.run('withdrawGarrison', { userId: a.userId, siteId: siteOf(h, 0).id, fromTownId: a.townId }); assert.deepEqual(wd.units, { soc_analyst: 400 }); assert.deepEqual(garrisonAt(h, 0), {}); assert.equal(draw(h, a), 200, 'they stopped drawing at once'); assert.deepEqual(totals(h), before, 'in the return movement, counted once'); h.advanceTo(atk.arriveAt); assert.equal(siteOf(h, 0).holder_alliance_id, h.B1, 'the same attack that would have lost took the site'); assert.equal(lastReport(h, b, 'landing').data.defender.garrison.length, 0); // the return leg survives the change of hands and brings every unit home assert.equal(h.db.get("SELECT status FROM movements WHERE id = ?", wd.movementId).status, 'out'); h.advanceTo(wd.arriveAt); assert.equal(T.units(h, a.townId).soc_analyst, 600, 'nothing lost on the way home'); assert.equal(draw(h, a), 600); assert.equal(T.notes(h, a.userId, 'movement_returned').length, 1); }); test('the other order: the attack arrives first, then a withdraw only gets the survivors, and after a wipe there is nothing to withdraw', () => { const h = scene(); const { a, a2, b, b2 } = h; const mv = sendTo(h, a, 0, 'reinforce', { soc_analyst: 400 }); h.advanceTo(mv.arriveAt); const S = staticHeld(h, 0, 0); const n = Math.ceil((S + 0.5 * 400 * 25) / 40); T.giveUnits(h, b.townId, { script_crew: n }); const atk = sendTo(h, b, 0, 'attack', { script_crew: n }); h.advanceTo(atk.arriveAt); const left = garrisonAt(h, 0).soc_analyst; assert.ok(left > 0 && left < 400); const siteId = siteOf(h, 0).id; assert.equal(err(() => h.run('withdrawGarrison', { userId: a.userId, siteId, fromTownId: a.townId, units: { soc_analyst: 400 } })).code, 'not_enough_units', 'cannot call home more than there is'); assert.equal(err(() => h.run('withdrawGarrison', { userId: a2.userId, siteId, fromTownId: a.townId })).code, 'not_found', 'only the owner can'); assert.equal(err(() => h.run('withdrawGarrison', { userId: b.userId, siteId, fromTownId: b.townId })).code, 'not_enough_units', 'a POP with nothing there'); const part = h.run('withdrawGarrison', { userId: a.userId, siteId, fromTownId: a.townId, units: { soc_analyst: 5 } }); assert.deepEqual(part.units, { soc_analyst: 5 }); assert.equal(garrisonAt(h, 0).soc_analyst, left - 5); // the site is wiped by the next wave: a late withdraw finds nothing const S1 = staticHeld(h, 0, 0); const n2 = Math.ceil((S1 + (left - 5) * 25) * 1.05 / 40); T.giveUnits(h, b2.townId, { script_crew: n2 }); const atk2 = sendTo(h, b2, 0, 'attack', { script_crew: n2 }); h.advanceTo(atk2.arriveAt); assert.equal(siteOf(h, 0).holder_alliance_id, h.B1); assert.equal(err(() => h.run('withdrawGarrison', { userId: a.userId, siteId, fromTownId: a.townId })).details.reason, 'nothing stationed'); h.advance(DAY); assert.equal(T.units(h, a.townId).soc_analyst, 200 + 5, 'only the withdrawn 5 came back; the rest died in the two battles'); }); test('recall of a reinforcement within the window: nothing ever arrives in the garrison; a reinforcement that arrives after the site changed hands is turned back', () => { const h = scene(); const { a, a2, b } = h; const before = totals(h); const mv = sendTo(h, a, 0, 'reinforce', { soc_analyst: 100 }); const rc = h.run('recallMovement', { userId: a.userId, movementId: mv.movementId }); h.advanceTo(rc.arriveAt + 1); assert.deepEqual(garrisonAt(h, 0), {}); assert.deepEqual(totals(h), before); assert.equal(T.units(h, a.townId).soc_analyst, 600); // alice2 lives further away: bob takes the site while her units are on the road h.db.run("UPDATE operators SET alliance_id = alliance_id WHERE id = ?", a2.opId); const late = sendTo(h, a2, 0, 'reinforce', { blue_team: 50 }); T.giveUnits(h, b.townId, { script_crew: Math.ceil(staticHeld(h, 0, 0) * 1.2 / 40) }); const atk = sendTo(h, b, 0, 'attack', { script_crew: Math.ceil(staticHeld(h, 0, 0) * 1.2 / 40) }); assert.ok(atk.arriveAt < late.arriveAt, 'the attack gets there first'); h.advanceTo(late.arriveAt); assert.equal(siteOf(h, 0).holder_alliance_id, h.B1); assert.deepEqual(garrisonAt(h, 0), {}, 'an ex-holder\'s units do not join the new holder\'s garrison'); assert.equal(lastReport(h, a2, 'movement_failed').data.reason, 'not_holder'); h.advance(DAY); assert.equal(T.units(h, a2.townId).blue_team, 100, 'they came home'); }); // ---------------------------------------------------------------- membership edges test('leaving or being kicked from the alliance, or the alliance disbanding, sends the garrison home; so does losing the POP it belongs to', () => { const h = scene(); const { a, a2, b } = h; const m1 = sendTo(h, a, 0, 'reinforce', { soc_analyst: 300 }); const m2 = sendTo(h, a2, 0, 'reinforce', { blue_team: 60 }); h.advanceTo(Math.max(m1.arriveAt, m2.arriveAt)); const before = totals(h); assert.deepEqual(garrisonAt(h, 0), { soc_analyst: 300, blue_team: 60 }); // a2 leaves: only her units go home h.run('allianceLeave', { userId: a2.userId, allianceId: h.A1 }); assert.deepEqual(garrisonAt(h, 0), { soc_analyst: 300 }); assert.deepEqual(totals(h), before, 'in a return movement'); const rel = T.notes(h, a2.userId, 'garrison_released'); assert.equal(rel.length, 1); assert.equal(rel[0].reason, 'left_alliance'); assert.deepEqual(rel[0].units, { blue_team: 60 }); h.advance(DAY); assert.equal(T.units(h, a2.townId).blue_team, 100); // a POP that changes hands: what it had in a site goes home to its OLD owner's other POP const a3 = T.addTown(h, a, { n: 2 }); const ctx = h.ctx(); const m3 = h.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'reinforce', x: h.sites[0].x, y: h.sites[0].y, units: { soc_analyst: 50 } }); h.advanceTo(m3.arriveAt); assert.equal(G.ownedAt(h.db, siteOf(h, 0).id, a.townId).soc_analyst, 350); const combat = require('../server/military/combat'); const tb = loadTown(h.db, a.townId); assert.ok(a3.townId); combat.transferTown(h.ctx(), tb, { id: b.opId }, { id: a.opId }, h.clock.now()); assert.deepEqual(garrisonAt(h, 0), {}, 'nothing is left standing for a POP that no longer belongs to its owner'); const rel2 = T.notes(h, a.userId, 'garrison_released').pop(); assert.equal(rel2.reason, 'town_lost'); h.advance(DAY); assert.equal(T.units(h, a3.townId).soc_analyst, 350, 'home to the old owner\'s remaining POP'); }); test('disbanding the holding alliance sends every garrison home and leaves the level with the site', () => { const h = scene(); const { a, a2 } = h; const m1 = sendTo(h, a, 0, 'reinforce', { soc_analyst: 300 }); const m2 = sendTo(h, a2, 0, 'reinforce', { blue_team: 60 }); h.advanceTo(Math.max(m1.arriveAt, m2.arriveAt)); h.db.run('UPDATE landing_sites SET level = 7 WHERE idx = 0'); const before = totals(h); h.run('allianceDisband', { userId: a.userId, allianceId: h.A1 }); assert.deepEqual(garrisonAt(h, 0), {}); assert.equal(siteOf(h, 0).holder_alliance_id, null); assert.equal(siteOf(h, 0).level, 7); assert.deepEqual(totals(h), before); assert.equal(T.notes(h, a.userId, 'garrison_released')[0].reason, 'alliance_dissolved'); h.advance(DAY); assert.equal(T.units(h, a.townId).soc_analyst, 600); assert.equal(T.units(h, a2.townId).blue_team, 100); assert.equal(h.db.get('SELECT COUNT(*) AS n FROM site_stationed').n, 0); }); // ---------------------------------------------------------------- power test('power: garrison units draw their owner\'s power; with the owner POP out of power the group stops defending, and a won attack still wipes it', () => { const h = scene(); const { b } = h; const s0 = h.sites[0]; const weak = near(h, 'weak', 0, { x: s0.x - 6, y: s0.y - 3, power: 5, battery: 40, infra: { battery_bank: 1 }, units: { soc_analyst: 60 } }); A.addMember(h.db, h.A1, weak.opId, 'member', h.clock.now()); const mv = sendTo(h, weak, 0, 'reinforce', { soc_analyst: 60 }); h.advanceTo(mv.arriveAt); const town = loadTown(h.db, weak.townId); assert.equal(town.unitDraw, 60, 'the garrison is the owner\'s draw'); assert.ok(R.ratesPerHour(town).powerNet < 0, 'net power is negative: ' + JSON.stringify(R.ratesPerHour(town))); assert.equal(h.db.get("SELECT COUNT(*) AS n FROM events WHERE type = 'powerCheck' AND status = 'pending' AND town_id = ?", weak.townId).n, 1, 'a power check is scheduled for the owner'); const emptyAt = R.batteryEmptyAt(town, h.clock.now()); assert.ok(emptyAt > h.clock.now()); const S = staticHeld(h, 0, 0); // online: the group defends assert.equal(G.defenders(h.db, siteOf(h, 0).id, emptyAt - 1000).groups[0].online, true); assert.equal(G.defenders(h.db, siteOf(h, 0).id, emptyAt - 1000).power, 60 * 25); // dry: offline, no defence value, and an attack that beats the static part takes the site although the group is there h.advanceTo(emptyAt + 5000); assert.equal(T.notes(h, weak.userId, 'units_offline').length, 1); const d = G.defenders(h.db, siteOf(h, 0).id, h.clock.now()); assert.equal(d.groups[0].online, false); assert.equal(d.power, 0); const n = Math.ceil(1.1 * S / 40); T.giveUnits(h, b.townId, { script_crew: n }); const atk = sendTo(h, b, 0, 'attack', { script_crew: n }); h.advanceTo(atk.arriveAt); assert.equal(siteOf(h, 0).holder_alliance_id, h.B1, 'an attack that beats the static part only takes a site whose garrison is offline'); const rep = lastReport(h, b, 'landing').data; assert.equal(rep.power.garrison, 0); assert.equal(rep.defender.garrison[0].offline, true); assert.equal(G.ownedAt(h.db, siteOf(h, 0).id, weak.townId).soc_analyst || 0, 0, 'a won attack wipes offline units too'); }); // ---------------------------------------------------------------- the opening scramble is unchanged test('an unheld site keeps its heavy static defence (the opening scramble), a held one drops to heldDefenceMult', () => { const h = scene(); const open = staticHeld(h, 1, 0, false), held = staticHeld(h, 1, 0, true); assert.equal(open, R.backboneDefence(h.world.seed, h.sites[1].x, h.sites[1].y, 100, 0)); assert.ok(open / held >= D.ENDGAME.defenceMult / D.ENDGAME.heldDefenceMult - 0.01); // a scan of the unheld site 1 shows the heavy number, a scan of the held site 0 the light one const c = near(h, 'scout', 1, { units: { port_scanner: 300 } }); const mv = sendTo(h, c, 1, 'scan', { port_scanner: 300 }); h.advanceTo(mv.arriveAt); assert.equal(lastReport(h, c, 'scan').data.intel.defence, open); }); test('power: decommission takes units out of the garrison, partly, until the draw fits the output', () => { const h = scene(); const s0 = h.sites[0]; const weak = near(h, 'weak', 0, { x: s0.x - 6, y: s0.y - 3, power: 5, battery: 40, infra: { battery_bank: 1 }, units: { soc_analyst: 60 } }); A.addMember(h.db, h.A1, weak.opId, 'member', h.clock.now()); const mv = sendTo(h, weak, 0, 'reinforce', { soc_analyst: 60 }); h.advanceTo(mv.arriveAt); const emptyAt = R.batteryEmptyAt(loadTown(h.db, weak.townId), h.clock.now()); h.advanceTo(emptyAt + 5000); const since = h.db.get('SELECT offline_since FROM town_power WHERE town_id = ?', weak.townId).offline_since; h.advanceTo(since + R.decommissionMs(1) + 1000); const left = G.ownedAt(h.db, siteOf(h, 0).id, weak.townId).soc_analyst || 0; assert.ok(left > 0 && left < 60, 'partly decommissioned: ' + left); const t1 = loadTown(h.db, weak.townId); const base = R.ratesPerHour(Object.assign({}, t1, { unitDraw: 0 })); assert.ok(t1.unitDraw <= base.powerOut - base.powerDraw + 1e-9, 'the draw fits the output again'); assert.equal(T.units(h, weak.townId).soc_analyst, undefined, 'it was the garrison that was decommissioned, nothing at home to take'); assert.equal(T.notes(h, weak.userId, 'units_decommissioned').length, 1); }); test('kicking a member sends her units home from the site', () => { const h = scene(); const { a, a2 } = h; const m = sendTo(h, a2, 0, 'reinforce', { blue_team: 40 }); h.advanceTo(m.arriveAt); assert.deepEqual(garrisonAt(h, 0), { blue_team: 40 }); h.run('allianceKick', { userId: a.userId, allianceId: h.A1, operatorId: a2.opId }); assert.deepEqual(garrisonAt(h, 0), {}); assert.equal(T.notes(h, a2.userId, 'garrison_released')[0].reason, 'left_alliance'); h.advance(DAY); assert.equal(T.units(h, a2.townId).blue_team, 100); }); // ---------------------------------------------------------------- the capture shield test('a site that changed hands is shielded for captureShieldHours (world hours / speed): attacks are refused or turned back, scans and garrisons are not', () => { const h = scene(); const { a, b } = h; const s0 = h.sites[0]; const S = staticHeld(h, 0, 0); const n = Math.ceil(1.3 * S / 40); const c = near(h, 'charlie', 0, { x: s0.x, y: s0.y + 6, units: { script_crew: 4 * n, port_scanner: 5 } }); alliance(h, 'Charlie', c); T.giveUnits(h, b.townId, { script_crew: n }); // charlie's strike is on the road when bob takes the site (she lives further away) const early = sendTo(h, c, 0, 'attack', { script_crew: 2 * n }); const atk = sendTo(h, b, 0, 'attack', { script_crew: n }); assert.ok(atk.arriveAt < early.arriveAt); h.advanceTo(atk.arriveAt); const site = siteOf(h, 0); assert.equal(site.holder_alliance_id, h.B1); const until = R.siteShieldUntil(site.taken_at, 1); assert.equal(until, site.taken_at + D.ENDGAME.captureShieldHours * H); assert.equal(h.eg.status(a.userId, h.worldId).sites[0].shieldUntil, until); // refused at the door ... const e = err(() => sendTo(h, c, 0, 'attack', { script_crew: n })); assert.equal(e.code, 'invalid_target'); assert.equal(e.details.reason, 'site_shielded'); assert.equal(e.details.until, until); // ... and one already on the road is turned back (its units come home) h.advanceTo(early.arriveAt); assert.equal(siteOf(h, 0).holder_alliance_id, h.B1, 'the strike bounced'); assert.equal(lastReport(h, c, 'movement_failed').data.reason, 'site_shielded'); // scanning and garrisoning are fine during the shield assert.ok(sendTo(h, c, 0, 'scan', { port_scanner: 5 }).movementId); T.giveUnits(h, b.townId, { soc_analyst: 5 }); assert.ok(sendTo(h, b, 0, 'reinforce', { soc_analyst: 5 }).movementId); // the shield ends on time, to the millisecond h.advanceTo(until - 1); assert.equal(err(() => sendTo(h, c, 0, 'attack', { script_crew: n })).details.reason, 'site_shielded'); h.advanceTo(until); assert.ok(sendTo(h, c, 0, 'attack', { script_crew: n }).movementId); // unheld sites never have one: the opening scramble is first come, first served assert.ok(sendTo(h, c, 1, 'attack', { script_crew: n }).movementId); }); test('the shield is world time: at speed 2 it lasts half as long', () => { assert.equal(R.siteShieldUntil(1000, 1) - 1000, D.ENDGAME.captureShieldHours * H); assert.equal(R.siteShieldUntil(1000, 2) - 1000, D.ENDGAME.captureShieldHours * H / 2); assert.equal(R.siteShieldUntil(null, 1), 0); });