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

@ -14,6 +14,7 @@ const R = require('../../public/js/rules.js');
const D = require('../../public/js/data.js');
const { loadTown, settle } = require('../state');
const U = require('./units');
const garrison = () => require('./garrison'); // lazy: garrison.js needs this module
const EPS = D.MILITARY.batteryEpsilon;
@ -23,10 +24,12 @@ function online(town, now) {
return !(c.amounts.battery <= EPS && c.rates.powerNet < 0);
}
// Every unit drawing power in the POP: home + reinforcements hosted here, as { unit: count }.
// Every unit drawing power in the POP: home + reinforcements hosted here + its own units standing in backbone landing sites (they draw
// their owner's power, E6), as { unit: count }.
function drawingUnits(db, townId) {
const out = U.homeUnits(db, townId);
for (const g of U.hostedGroups(db, townId)) U.sumInto(out, g.units);
U.sumInto(out, garrison().ofTown(db, townId));
return out;
}
@ -52,7 +55,7 @@ function reschedule(ctx, townId, now) {
ctx.enqueue({ worldId: town.worldId, dueAt: Math.max(now, row.offline_since + R.decommissionMs(town.speed)), type: 'powerCheck', townId, payload: { townId } });
}
// Decommission the most expensive units (home first, then reinforcements) until the draw fits. The town is settled.
// Decommission the most expensive units (home first, then reinforcements, then its own garrison units) until the draw fits. The town is settled.
function decommission(ctx, town, now) {
const { db } = ctx;
const units = drawingUnits(db, town.id);
@ -77,6 +80,14 @@ function decommission(ctx, town, now) {
note(ot ? U.userOfOperator(db, ot.oid) : null, unit, n);
}
}
if (left > 0) { // its own units standing in landing sites
const G = garrison();
for (const r of db.all('SELECT site_id, count FROM site_stationed WHERE owner_town_id = ? AND unit = ? ORDER BY site_id', town.id, unit)) {
if (left <= 0) break;
const n = Math.min(r.count, left);
G.setUnit(db, r.site_id, town.id, unit, r.count - n); left -= n; note(ownerUser, unit, n);
}
}
removed[unit] = plan[unit] - left;
}
for (const [userId, lost] of byOwner) if (userId) ctx.notify(userId, town.worldId, 'units_decommissioned', { townId: town.id, units: lost });