Upstream/server/commands/index.js
jvanvik 3c23c51902 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
2026-10-12 00:58:12 +02:00

75 lines
3.4 KiB
JavaScript

'use strict';
/*
* Player commands: one file each, validate + apply in one transaction. `run` first catches the event queue up to now
* (so a town is never touched with an overdue event pending), then runs the command in a transaction, then re-arms the
* scheduler timer because the command may have enqueued events.
*/
const handlers = {
// stage 3/4: military and expansion
train: require('./train'),
research: require('./research'),
sendMovement: require('./sendMovement'),
sendTransport: require('./sendTransport'),
recallMovement: require('./recallMovement'),
withdrawStationed: require('./withdrawStationed'),
markReportRead: require('./markReportRead'),
signup: require('./signup'),
joinWorld: require('./joinWorld'),
build: require('./build'),
cancelBuild: require('./cancelBuild'),
renameTown: require('./renameTown'),
// stage 5: alliances, diplomacy, messages
allianceCreate: require('./allianceCreate'),
allianceInvite: require('./allianceInvite'),
allianceInviteCancel: require('./allianceInviteCancel'),
allianceInviteAnswer: require('./allianceInviteAnswer'),
allianceLeave: require('./allianceLeave'),
allianceKick: require('./allianceKick'),
allianceRole: require('./allianceRole'),
allianceTransfer: require('./allianceTransfer'),
allianceDisband: require('./allianceDisband'),
allianceNoteAdd: require('./allianceNote').allianceNoteAdd,
allianceNoteEdit: require('./allianceNote').allianceNoteEdit,
allianceNoteDelete: require('./allianceNote').allianceNoteDelete,
diploPropose: require('./diploPropose'),
diploAnswer: require('./diploAnswer'),
diploWithdraw: require('./diploWithdraw'),
diploRevoke: require('./diploRevoke'),
messageSend: require('./messageSend'),
messageRead: require('./messageRead'),
messageDelete: require('./messageDelete'),
messageBlock: require('./messageBlock').messageBlock,
messageUnblock: require('./messageBlock').messageUnblock,
// trade, end game, shared reports
reportShare: require('./reportShare').reportShare,
reportUnshare: require('./reportShare').reportUnshare,
tradeOffer: require('./tradeOffer'),
tradeCancel: require('./tradeCancel'),
tradeAccept: require('./tradeAccept'),
backboneBuild: require('./backboneBuild'),
backboneShip: require('./backboneShip'),
withdrawGarrison: require('./withdrawGarrison'),
};
function createCommands({ db, clock, scheduler }) {
// hooks.onCommand(name, args, out): optional, called after a command committed (stage 6 push). Never throws into the caller.
const hooks = { onCommand: null };
function run(name, args) {
const fn = handlers[name];
if (!fn) throw new Error('unknown command ' + name);
scheduler.runDue();
require('../endgame').guard(db, name, args); // an ended world refuses every command except reading state and chatting
const now = clock.now();
const mark = scheduler.outboxMark();
let out;
try { out = db.tx(() => fn({ db, clock, now, scheduler }, args)); }
catch (err) { scheduler.outboxRollback(mark); throw err; }
scheduler.flushOutbox(); // notifications written by the command go out now that it is committed
if (hooks.onCommand) { try { hooks.onCommand(name, args, out); } catch (e) { if (scheduler.log) scheduler.log('onCommand hook failed: ' + e.message); } }
scheduler.arm();
return out;
}
return { run, handlers, hooks };
}
module.exports = { createCommands };