Phase 4: close loot/refund leaks, landing-site garrisons, speed-scaled shipping, update/offsite tooling, broad e2e

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
This commit is contained in:
jvanvik 2026-10-12 00:58:12 +02:00
parent 9104480463
commit 3c23c51902
92 changed files with 4887 additions and 300 deletions

View file

@ -24,12 +24,12 @@ class Collector {
this.unitsLost = {}; // unit -> count, every way of losing units that the ledger knows about
this.unitsConsumed = {}; // crews/teams used up (not battle losses)
this.byType = {}; // report type -> count
this.battles = { raid: { n: 0, att: 0, loot: 0, lootRes: { fiber: 0, hardware: 0, capital: 0 }, attLost: 0, defLost: 0 }, attack: { n: 0, att: 0, loot: 0, attLost: 0, defLost: 0 }, takeover: { n: 0, att: 0, loot: 0, attLost: 0, defLost: 0 } };
this.battles = { raid: { n: 0, att: 0, loot: 0, lootRes: { fiber: 0, hardware: 0, capital: 0 }, lootLeft: { fiber: 0, hardware: 0, capital: 0 }, emptyWins: 0, attLost: 0, defLost: 0 }, attack: { n: 0, att: 0, loot: 0, attLost: 0, defLost: 0 }, takeover: { n: 0, att: 0, loot: 0, attLost: 0, defLost: 0 } };
this.raidSamples = []; // { loot, attLostValue, defLostValue, A, D, win, targetAgeDays, undefended }
this.takeovers = { attempted: 0, dropped: 0, blocked: {}, captured: 0, loyaltyDropped: 0 };
this.scans = { n: 0, ok: 0, failed: 0, detected: 0, revealed: 0 };
this.incumbent = { n: 0, wins: 0, hardware: 0, attLostValue: 0 };
this.landing = { n: 0, taken: 0, attLostValue: 0 };
this.landing = { n: 0, taken: 0, attLostValue: 0, vsHeld: 0, vsHeldTaken: 0, vsGarrison: 0, garrisonLostValue: 0, garrisonLost: {}, attackPower: 0, defencePower: 0 };
this.movementFailed = {};
this.settles = { ok: 0 };
this.siege = { n: 0, levels: 0 };
@ -37,6 +37,7 @@ class Collector {
this.notes = {}; // notification type -> count
this.cargoLostTotal = { fiber: 0, hardware: 0, capital: 0 };
this.cargoLostEvents = 0;
this.cargoLostBy = {}; // where the overflow happened: transport_arrived | raid_return | link_return -> { fiber, hardware, capital, n }
this.decommissioned = {};
this.unitsOfflineNotes = 0;
this.opCreated = new Map();
@ -68,17 +69,25 @@ class Collector {
default: break;
}
}
const rows = db.all("SELECT id, type, payload FROM notifications WHERE id > ? AND type IN ('transport_arrived','movement_returned','units_decommissioned','units_offline') ORDER BY id", this.noteCursor);
const rows = db.all("SELECT id, type, payload FROM notifications WHERE id > ? AND type IN ('transport_arrived','movement_returned','units_decommissioned','units_offline','trade_refund_clipped') ORDER BY id", this.noteCursor);
for (const n of rows) {
this.noteCursor = n.id;
const p = JSON.parse(n.payload);
bump(this.notes, n.type);
if (n.type === 'units_decommissioned') { addInto(this.decommissioned, p.units); addInto(this.unitsLost, p.units); }
else if (n.type === 'units_offline') this.unitsOfflineNotes += 1;
else if (n.type === 'trade_refund_clipped') { ledger.sinkClip[p.resource] += p.lost; ledger.clipEvents += 1; }
else if (p.cargoLost) {
let any = false;
for (const r of RES) if (p.cargoLost[r]) { this.cargoLostTotal[r] += p.cargoLost[r]; ledger.sinkLost[r] += p.cargoLost[r]; any = true; }
if (any) { ledger.lostEvents += 1; this.cargoLostEvents += 1; }
if (any) {
ledger.lostEvents += 1; this.cargoLostEvents += 1;
let where = n.type;
if (n.type === 'movement_returned') { const m = db.get('SELECT m.extra, pm.type AS ptype, pm.to_town_id AS ptown FROM movements m LEFT JOIN movements pm ON pm.id = m.parent_id WHERE m.id = ?', p.movementId); where = m && JSON.parse(m.extra || '{}').link ? 'link_return' : (m && m.ptype && !m.ptown ? 'site_return' : 'raid_return'); }
const by = this.cargoLostBy[where] || (this.cargoLostBy[where] = { fiber: 0, hardware: 0, capital: 0, n: 0 });
for (const r of RES) by[r] += p.cargoLost[r] || 0;
by.n += 1;
}
}
}
// notifications are numbered independently of the filter above: move the cursor past rows we skipped
@ -100,7 +109,7 @@ class Collector {
const loot = lootValue(d.loot);
if (win) b.att += 1;
b.loot += loot; b.attLost += attLostVal; b.defLost += defLostVal;
if (kind === 'raid') addInto(b.lootRes, d.loot);
if (kind === 'raid') { addInto(b.lootRes, d.loot); addInto(b.lootLeft, d.lootLeft || {}); if (win && !loot) b.emptyWins += 1; }
const aId = d.attacker.operator.id, dId = d.defender.operator.id;
const a = this.op(aId), df = this.op(dId);
if (kind === 'raid') { a.raids += 1; if (win) a.raidWins += 1; a.raidLoot += loot; a.raidLostValue += attLostVal; }
@ -144,6 +153,12 @@ class Collector {
const lost = (d.attacker.units && d.attacker.units.lost) || {};
this.landing.attLostValue += unitsValue(lost);
addInto(this.unitsLost, lost);
// contests (E6): attacks on a HELD site, those that met a garrison, and what the garrison lost (units stationed at a site die in these reports)
if (d.site && d.site.holder) { this.landing.vsHeld += 1; if (d.taken) this.landing.vsHeldTaken += 1; this.landing.firstVsHeldAt = this.landing.firstVsHeldAt || d.arrivedAt; this.landing.lastVsHeldAt = d.arrivedAt; }
const gar = (d.defender && d.defender.garrison) || [];
if (gar.some((g) => Object.keys(g.sent || {}).length)) this.landing.vsGarrison += 1;
for (const g of gar) { addInto(this.unitsLost, g.lost || {}); addInto(this.landing.garrisonLost, g.lost || {}); this.landing.garrisonLostValue += unitsValue(g.lost || {}); }
if (d.power) { this.landing.attackPower += d.power.attack || 0; this.landing.defencePower += d.power.defence || 0; }
}
}