Built: units/research/training, movements (scan, raid, attack, takeover, reinforce, settle, transport), deterministic combat resolved in the event queue, reports, power/offline units, founding + conquest + incumbent sites; alliances, roles, diplomacy (24h pact cancel via events), messages, rankings; vanilla-JS phone-first client (auth, POP, map, alerts, settings, en/nb); systemd units, backup timer, DEPLOY.md, tools/admin.js. Tested: npm test 96/96 (fake clock, in-memory db, a few real-HTTP tests); client checked with headless Chromium screenshots only. NOT done: marketplace/transport trade, client screens for military/social, WebSocket push, bots/balance, e2e suite. All unit stats and costs are unbalanced first guesses. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
151 lines
9.5 KiB
JavaScript
151 lines
9.5 KiB
JavaScript
'use strict';
|
|
const test = require('node:test');
|
|
const assert = require('node:assert');
|
|
const fs = require('fs');
|
|
const os = require('os');
|
|
const path = require('path');
|
|
const T = require('./military-helpers');
|
|
const P = require('../server/military/power');
|
|
const { loadTown } = require('../server/state');
|
|
const { R, D, H } = T;
|
|
|
|
// A POP whose units draw more than it produces, with a small battery: units must go offline when the battery is empty.
|
|
function strainedTown(h, name, o) {
|
|
const op = T.addOperator(h, name, Object.assign({ power: 5, battery: 40, infra: { battery_bank: 1 } }, o));
|
|
T.giveUnits(h, op.townId, { script_crew: 40, soc_analyst: 10 });
|
|
P.reschedule(h.ctx(), op.townId, h.clock.now());
|
|
return op;
|
|
}
|
|
|
|
test('power: the battery empties, units go offline (cannot move, do not defend), then the expensive ones are decommissioned', () => {
|
|
const h = T.setup();
|
|
const a = strainedTown(h, 'strained', { x: 5, y: 0 });
|
|
const t0 = loadTown(h.db, a.townId);
|
|
const net = R.ratesPerHour(t0).powerNet;
|
|
assert.ok(net < 0, 'net power is negative with 50 units');
|
|
const emptyAt = R.batteryEmptyAt(t0, h.clock.now());
|
|
assert.ok(emptyAt > h.clock.now());
|
|
assert.equal(h.db.get("SELECT COUNT(*) AS n FROM events WHERE type = 'powerCheck' AND status = 'pending'").n, 1, 'exactly one pending check');
|
|
|
|
// before the battery is empty: units online and can move
|
|
h.advanceTo(emptyAt - 60000);
|
|
assert.ok(P.online(loadTown(h.db, a.townId), h.clock.now()));
|
|
assert.equal(h.queries.town(a.userId, a.townId).town.power.online, true);
|
|
|
|
// after: offline
|
|
h.advanceTo(emptyAt + 5000);
|
|
const offline = h.db.get('SELECT offline_since FROM town_power WHERE town_id = ?', a.townId);
|
|
assert.ok(offline && offline.offline_since >= emptyAt, 'offline since the battery ran dry');
|
|
assert.equal(T.notes(h, a.userId, 'units_offline').length, 1);
|
|
assert.equal(h.queries.town(a.userId, a.townId).town.power.online, false);
|
|
const target = T.addOperator(h, 'target', { x: 9, y: 0 });
|
|
assert.throws(() => h.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'raid', toTownId: target.townId, units: { script_crew: 10 } }), (e) => e.code === 'units_offline');
|
|
|
|
// offline units do not defend: a small raid against the strained POP loots it although it "has" 50 units
|
|
const raider = T.addOperator(h, 'raider', { x: 3, y: 2, units: { script_crew: 15 } });
|
|
const s = h.run('sendMovement', { userId: raider.userId, fromTownId: raider.townId, type: 'raid', toTownId: a.townId, units: { script_crew: 15 } });
|
|
h.advanceTo(s.arriveAt);
|
|
const rep = T.reportsOf(h, raider.userId).find((r) => r.type === 'raid').data;
|
|
assert.equal(rep.defender.online, false);
|
|
assert.equal(rep.outcome, 'attacker_won');
|
|
assert.equal(rep.power.defenceRaw, 0);
|
|
assert.deepEqual(T.units(h, a.townId), { script_crew: 40, soc_analyst: 10 }, 'offline units are not hurt by a raid');
|
|
|
|
// 24 h offline: decommission, most expensive first, until the draw fits again
|
|
const since = h.db.get('SELECT offline_since FROM town_power WHERE town_id = ?', a.townId).offline_since;
|
|
h.advanceTo(since + R.decommissionMs(1) - 1000);
|
|
assert.deepEqual(T.units(h, a.townId), { script_crew: 40, soc_analyst: 10 }, 'nothing before the 24 hours are up');
|
|
h.advanceTo(since + R.decommissionMs(1) + 1000);
|
|
const left = T.units(h, a.townId);
|
|
assert.ok(left.script_crew < 40 || left.soc_analyst < 10, 'units were decommissioned');
|
|
const t1 = loadTown(h.db, a.townId);
|
|
assert.ok(R.ratesPerHour(t1).powerNet >= 0, 'the draw fits the output now');
|
|
assert.ok(P.online(t1, h.clock.now()), 'online again');
|
|
assert.equal(h.db.get('SELECT COUNT(*) AS n FROM town_power WHERE town_id = ?', a.townId).n, 0);
|
|
assert.equal(T.notes(h, a.userId, 'units_decommissioned').length, 1);
|
|
const unit = (id) => D.UNITS[id];
|
|
// script_crew (cost 150) is more expensive than soc_analyst (90): the crews go first
|
|
assert.ok(unit('script_crew').cost.fiber + unit('script_crew').cost.hardware + unit('script_crew').cost.capital > unit('soc_analyst').cost.fiber + unit('soc_analyst').cost.hardware + unit('soc_analyst').cost.capital);
|
|
assert.equal(left.soc_analyst, 10, 'the cheaper defenders survive');
|
|
assert.equal(h.db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'failed'").n, 0);
|
|
});
|
|
|
|
test('power: building more substations before the battery is empty re-arms the check (no offline)', () => {
|
|
const h = T.setup();
|
|
const a = strainedTown(h, 'saver', { x: 5, y: 0 });
|
|
const emptyAt = R.batteryEmptyAt(loadTown(h.db, a.townId), h.clock.now());
|
|
// more output: the reschedule moves the check, units never go offline
|
|
h.db.run("UPDATE buildings SET level = 12 WHERE town_id = ? AND key IN ('f13','f14','f15','f16')", a.townId);
|
|
P.reschedule(h.ctx(), a.townId, h.clock.now());
|
|
assert.equal(h.db.get("SELECT COUNT(*) AS n FROM events WHERE type = 'powerCheck' AND status = 'pending'").n, 0, 'net power is positive: no check needed');
|
|
h.advanceTo(emptyAt + 10 * H);
|
|
assert.equal(T.notes(h, a.userId, 'units_offline').length, 0);
|
|
assert.ok(P.online(loadTown(h.db, a.townId), h.clock.now()));
|
|
});
|
|
|
|
test('power: reinforcements draw from the host, and leaving units stops the draw at the origin', () => {
|
|
const h = T.setup();
|
|
const a = T.addOperator(h, 'ally-a', { x: 5, y: 0, units: { soc_analyst: 20 } });
|
|
const b = T.addOperator(h, 'ally-b', { x: 8, y: 0 });
|
|
const t = (id) => loadTown(h.db, id).unitDraw;
|
|
assert.equal(t(a.townId), 20);
|
|
// not allied: refused
|
|
assert.throws(() => h.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'reinforce', toTownId: b.townId, units: { soc_analyst: 5 } }), (e) => e.code === 'invalid_target');
|
|
// allies via the social tables
|
|
h.db.run("INSERT INTO alliances (world_id, name, tag, size_cap, created_by, created_at) VALUES (?, 'N', 'NN', 10, ?, ?)", h.worldId, a.opId, h.clock.now());
|
|
const aid = h.db.get('SELECT id FROM alliances').id;
|
|
for (const op of [a, b]) h.db.run('UPDATE operators SET alliance_id = ? WHERE id = ?', aid, op.opId);
|
|
const s = h.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'reinforce', toTownId: b.townId, units: { soc_analyst: 12 } });
|
|
assert.equal(t(a.townId), 8, 'units in transit draw nothing');
|
|
h.advanceTo(s.arriveAt);
|
|
assert.equal(t(b.townId), 12, 'the host pays for the reinforcements');
|
|
assert.equal(t(a.townId), 8);
|
|
const view = h.queries.town(b.userId, b.townId).town.military.units;
|
|
assert.equal(view.stationedHere.length, 1);
|
|
assert.deepEqual(view.stationedHere[0].units, { soc_analyst: 12 });
|
|
assert.equal(h.queries.town(a.userId, a.townId).town.military.units.stationedAway[0].hostTownId, b.townId);
|
|
|
|
// the reinforcement defends the host
|
|
const raider = T.addOperator(h, 'raider', { x: 12, y: 0, units: { script_crew: 20 } });
|
|
const rs = h.run('sendMovement', { userId: raider.userId, fromTownId: raider.townId, type: 'raid', toTownId: b.townId, units: { script_crew: 20 } });
|
|
h.advanceTo(rs.arriveAt);
|
|
const rep = T.reportsOf(h, raider.userId).find((r) => r.type === 'raid');
|
|
assert.equal(rep.data.defender.groups[0].sent.soc_analyst, 12);
|
|
assert.ok(T.reportsOf(h, a.userId).some((r) => r.role === 'reinforcer'), 'the reinforcing owner gets the report');
|
|
|
|
// the host sends them home: draw moves back after they arrive
|
|
const w = h.run('withdrawStationed', { userId: b.userId, hostTownId: b.townId, ownerTownId: a.townId });
|
|
assert.equal(t(b.townId), 0);
|
|
h.advanceTo(w.arriveAt);
|
|
assert.equal(T.units(h, a.townId).soc_analyst > 8, true);
|
|
});
|
|
|
|
test('restart: overdue events are caught up in order after reopening the same database file', () => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'upstream-mil-'));
|
|
const file = path.join(dir, 'game.db');
|
|
const h1 = T.setup({ file });
|
|
const clock = h1.clock;
|
|
const a = T.addOperator(h1, 'raider', { x: 5, y: 0, units: { script_crew: 60, botnet: 10 } });
|
|
const d = T.addOperator(h1, 'victim', { x: 20, y: 0, units: { soc_analyst: 5 } });
|
|
const slow = h1.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'raid', toTownId: d.townId, units: { script_crew: 60 } });
|
|
const fast = h1.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'raid', toTownId: d.townId, units: { botnet: 10 } });
|
|
h1.run('train', { userId: a.userId, townId: a.townId, unit: 'soc_analyst', count: 3 });
|
|
assert.ok(fast.arriveAt < slow.arriveAt);
|
|
h1.db.close(); // "the server is down"
|
|
clock.advance(100 * H); // ... for a long time
|
|
const h2 = T.setup({ file, clock });
|
|
const n = h2.scheduler.catchUp();
|
|
assert.ok(n >= 5, 'arrivals, returns and the training batch were all overdue: ' + n);
|
|
const reps = h2.db.all("SELECT id, type, data FROM reports WHERE type = 'raid' ORDER BY id").map((r) => JSON.parse(r.data));
|
|
assert.equal(reps.length, 2);
|
|
assert.ok(reps[0].arrivedAt === fast.arriveAt && reps[1].arrivedAt === slow.arriveAt, 'reports follow event order, not creation order');
|
|
assert.equal(h2.db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'pending' AND due_at <= ?", clock.now()).n, 0);
|
|
assert.equal(h2.db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'failed'").n, 0);
|
|
const home = T.units(h2, a.townId);
|
|
assert.ok(home.script_crew > 0 && home.botnet > 0, 'everyone came home');
|
|
assert.equal(home.soc_analyst, 3, 'the training batch finished during the downtime');
|
|
// resources: the first (fast) raid saw the victim's stock at ITS time, not at catch-up time
|
|
assert.equal(reps[0].defender.groups[0].sent.soc_analyst, 5);
|
|
h2.db.close();
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
});
|