Built: - Economy: raid loot capped to the room the raiders have at home (rest stays with the victim, "lootLeft" in the report); trade escrow reserves its room so refunds always fit; trade accept refuses with no_room_for_goods when the goods would not fit. Loot destroyed per 30-day run 31-61k -> ~1k, refunds clipped ~27k -> 0 (docs/BALANCE.md, 3 seeds). - End game: members of the holding alliance garrison landing sites (migration 005_site_garrison.sql), garrison counts in site defence, 72 h capture shield, withdraw route, POP view lists garrisoned units. Shipping capacity scales with world speed (3x world: winner 76-98 -> ~30-36 days after sites open). Bot runs show real contests (84-194 attacks on held sites at 1x). - Ops: deploy/update.sh (backup, stop, ff-merge, npm ci --ignore-scripts, health check, rollback), deploy/upstream-offsite (rsync only), admin end-world snapshots results, EPIPE fix, docs/workflows/ filled in. - e2e 62 -> 163 checks: units, raids/reports, alliance+messages, trade, end game, nb, PWA, reconnect, slow polling fallback (opt-in). Two app bugs found: message push ignored, "New" button stayed English. Tested: npm test 274/274, openapi check, npm run e2e 163/163 (137 s), bot runs 50x30d seeds 1-3 and 140-day/60-day speed-3 end-game runs INVARIANTS OK, adversarial review pass (findings fixed or documented). NOT tested: update.sh on a real box (systemd, real npm, runuser; stubs only, shellcheck not installed), garrison card/loot text on a phone, Node 22.13, humans. Known leaks left open and documented (BALANCE S6): loot/shipments can still overflow when stock arrives in flight, bounced backbone shipments lose overflow (reported to the sender). Garrison numbers (heldDefenceMult 2, shield 72 h) are first guesses from 2 seeds. Not claimed: balanced. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
125 lines
7 KiB
JavaScript
125 lines
7 KiB
JavaScript
'use strict';
|
|
/* tools/admin.js end-world: it must end a world exactly like the natural end (worldEnd event handler): final ranking stored and
|
|
* served by /worlds/:id/results, trade escrow returned, pending events cancelled, notifications written, refusal without --yes.
|
|
* Also: a closed stdout pipe (`admin.js help | head`) is not an error. A file DB shared by the fixture and the CLI, fake clock. */
|
|
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('fs');
|
|
const os = require('os');
|
|
const path = require('path');
|
|
const { spawn } = require('child_process');
|
|
const admin = require('../tools/admin.js');
|
|
const T = require('./military-helpers');
|
|
const { createEndgameQueries } = require('../server/endgameQueries');
|
|
const { H } = T;
|
|
|
|
function cli(file, args, now) {
|
|
const out = [], err = [];
|
|
const code = admin.main(args.concat(['--db', file]), { out: (s) => out.push(s), err: (s) => err.push(s), env: {}, now: () => now });
|
|
const text = out.join('\n');
|
|
return { code, out: text, err: err.join('\n'), json: args.includes('--json') ? JSON.parse(text) : null };
|
|
}
|
|
|
|
// A world with two operators, an open trade offer (escrow), a pending build and one build that is already overdue.
|
|
function fixture() {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'upstream-endworld-'));
|
|
const file = path.join(dir, 'game.db');
|
|
const h = T.setup({ file });
|
|
const a = T.addOperator(h, 'alice', { x: 5, y: 0 });
|
|
const b = T.addOperator(h, 'bob', { x: 9, y: 0 });
|
|
h.run('build', { userId: a.userId, townId: a.townId, key: 'f1' });
|
|
const offer = h.run('tradeOffer', { userId: a.userId, townId: a.townId, giveRes: 'fiber', giveAmount: 400, wantRes: 'hardware', wantAmount: 300, maxDistance: 30 }).offer;
|
|
return { h, file, a, b, offer, eg: createEndgameQueries(h) };
|
|
}
|
|
|
|
test('end-world refuses without --yes, says what it would do, and changes nothing', () => {
|
|
const { h, file, a, eg } = fixture();
|
|
const r = cli(file, ['end-world', String(h.worldId)], h.clock.now());
|
|
assert.equal(r.code, 2);
|
|
assert.match(r.err, /refusing without --yes/);
|
|
assert.match(r.err, /1 open trade offer/);
|
|
assert.equal(h.db.get('SELECT status FROM worlds').status, 'running');
|
|
assert.equal(eg.results(a.userId, h.worldId).available, false);
|
|
});
|
|
|
|
test('end-world --yes runs the real worldEnd: ranking snapshot, results available, escrow back, events cancelled, players notified', () => {
|
|
const { h, file, a, b, offer, eg } = fixture();
|
|
const fiberBefore = T.town(h, a.townId).fiber;
|
|
assert.ok(h.db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'pending'").n >= 2);
|
|
const now = h.clock.now() + 60 * 1000; // the offer's expiry and the build are still in the future
|
|
|
|
const r = cli(file, ['end-world', String(h.worldId), '--yes', '--json'], now);
|
|
assert.equal(r.code, 0, r.err);
|
|
assert.equal(r.json.changed, true);
|
|
assert.equal(r.json.ranking, true);
|
|
assert.equal(r.json.operators, 2);
|
|
assert.equal(r.json.closedOffers, 1);
|
|
|
|
const w = h.db.get('SELECT * FROM worlds WHERE id = ?', h.worldId);
|
|
assert.equal(w.status, 'ended');
|
|
assert.equal(w.end_at, now);
|
|
// exactly what GET /worlds/:id/results serves after a natural end
|
|
const res = eg.results(b.userId, h.worldId);
|
|
assert.equal(res.available, true);
|
|
assert.equal(res.status, 'ended');
|
|
assert.equal(res.results.at, now);
|
|
assert.equal(res.results.operators.length, 2);
|
|
assert.deepEqual(res.results.operators.map((o) => o.rank), [1, 2]);
|
|
assert.equal(res.results.totals.operators, 2);
|
|
assert.equal(res.results.winner, null, 'nobody won the race: the admin ended it');
|
|
|
|
// the event row is the same shape the natural end leaves, and nothing is left pending
|
|
const ev = h.db.get("SELECT * FROM events WHERE type = 'worldEnd'");
|
|
assert.equal(ev.status, 'done');
|
|
assert.deepEqual(JSON.parse(ev.payload), { reason: 'admin' });
|
|
assert.equal(h.db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'pending'").n, 0);
|
|
assert.ok(h.db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'cancelled'").n >= 2);
|
|
// escrow went back to the POP, the offer is closed
|
|
assert.equal(h.db.get('SELECT status FROM trade_offers WHERE id = ?', offer.id).status, 'expired');
|
|
assert.ok(T.town(h, a.townId).fiber >= fiberBefore + 400, 'the 400 are back in the POP (plus what it produced meanwhile)');
|
|
// every operator is told
|
|
for (const u of [a, b]) assert.equal(T.notes(h, u.userId, 'world_ended').length, 1);
|
|
// the world is frozen for commands
|
|
assert.throws(() => h.run('build', { userId: b.userId, townId: b.townId, key: 'f1' }), (e) => e.code === 'world_ended');
|
|
|
|
// second run: already ended, no change, exit 0, and no second ranking or notification
|
|
const again = cli(file, ['end-world', String(h.worldId), '--json'], now + 1000);
|
|
assert.equal(again.code, 0);
|
|
assert.equal(again.json.changed, false);
|
|
assert.equal(again.json.ranking, true);
|
|
assert.equal(T.notes(h, a.userId, 'world_ended').length, 1);
|
|
});
|
|
|
|
test('end-world processes events that were already due first, in order (a finished build is not lost)', () => {
|
|
const { h, file, a, eg } = fixture();
|
|
const q = h.db.get("SELECT due_at FROM events WHERE type = 'buildDone'");
|
|
assert.ok(q);
|
|
const now = q.due_at + 5000; // the server was stopped: the build fell due before the CLI ran
|
|
const r = cli(file, ['end-world', String(h.worldId), '--yes', '--json'], now);
|
|
assert.equal(r.code, 0, r.err);
|
|
assert.ok(r.json.caughtUpFirst >= 1);
|
|
assert.equal(h.db.get("SELECT status FROM events WHERE type = 'buildDone'").status, 'done');
|
|
assert.equal(h.db.get("SELECT level FROM buildings WHERE town_id = ? AND key = 'f1'", a.townId).level >= 1, true);
|
|
assert.equal(eg.results(a.userId, h.worldId).available, true);
|
|
});
|
|
|
|
test('end-world only touches the named world', () => {
|
|
const { h, file } = fixture();
|
|
const { createWorld } = require('../server/world');
|
|
const second = createWorld(h.db, h.clock.now(), { seed: 7, mapRadius: 20 });
|
|
h.db.run("INSERT INTO events (world_id, due_at, type, payload, status, created_at) VALUES (?, ?, 'reportCleanup', '{}', 'pending', ?)", second, h.clock.now() + 99 * H, h.clock.now());
|
|
assert.equal(cli(file, ['end-world', String(h.worldId), '--yes'], h.clock.now() + 1000).code, 0);
|
|
assert.equal(h.db.get('SELECT status FROM worlds WHERE id = ?', second).status, 'running');
|
|
assert.equal(h.db.get("SELECT COUNT(*) AS n FROM events WHERE world_id = ? AND status = 'pending'", second).n, 1);
|
|
assert.equal(h.db.get('SELECT COUNT(*) AS n FROM world_rankings WHERE world_id = ?', second).n, 0);
|
|
});
|
|
|
|
test('a closed stdout pipe is quiet: `admin.js help | head` prints no EPIPE stack trace and keeps the exit code', async () => {
|
|
const child = spawn(process.execPath, [path.join(__dirname, '..', 'tools', 'admin.js'), 'help'], { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
child.stdout.destroy(); // the reader goes away before the CLI has written anything
|
|
let err = '';
|
|
child.stderr.on('data', (d) => { err += d; });
|
|
const code = await new Promise((resolve) => child.on('close', resolve));
|
|
assert.equal(err, '');
|
|
assert.equal(code, 0);
|
|
});
|