Upstream/server/commands/index.js
jvanvik b18645dca1 Phase 2: client military/social screens, WebSocket push, trade + endgame, review findings pending
Built: units/movements/reports/send/expansion screens, alliance/messages/rankings screens, ws client + server /ws push
(docs/PROTOCOL.md), Internet exchange trade, backbone endgame + world end, shared reports, buildDone relative levels.
Tested: npm test 175/175. Two adversarial reviews found issues (trade-leg recall exploit, __proto__ via validate.js,
rankings DoS, static headers, ...): fixes follow in the next commit.
NOT done: bots/balance, e2e suite, real-device checks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
2026-10-11 19:05:03 +02:00

74 lines
3.3 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'),
};
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 };