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
This commit is contained in:
jvanvik 2026-10-11 19:05:03 +02:00
parent 249818a7aa
commit b18645dca1
87 changed files with 10201 additions and 71 deletions

84
server/pushHooks.js Normal file
View file

@ -0,0 +1,84 @@
'use strict';
/*
* The game-aware half of push: which hint goes to whom. push.js knows sockets; this file knows notifications, events and
* commands. Hooks fire only AFTER the transaction that caused them committed (scheduler outbox, commands.run).
*
* notification row (any kind) -> user: {type:'notification', id, kind}
* kind incoming -> user: {type:'incoming', townId, arriveAt}
* kind report -> user: {type:'report', id}
* kind message | alliance_message -> user: {type:'message', id}
* town_lost, town_gained, ... -> user: {type:'town', townId} (ownership changed: the old owner is not
* reachable through toTown any more)
* event done (build, train, research, arrive, return ...) -> owner of every POP it touched: {type:'town', townId}
* command done (build, train, send, ...) -> owner of every POP it touched: {type:'town', townId}
* (also the device that issued it: the client may ignore its own echo; other devices of the user learn from it)
*
* Not a hint source: reading (GET) never pushes.
*/
const isId = (v) => Number.isInteger(v) && v > 0;
// notification kind -> payload key holding a POP id whose state changed for THAT user (kinds whose event does not already
// carry the town in events.town_id, or whose owner changed)
const TOWN_KINDS = {
town_lost: 'townId', town_gained: 'townId',
transport_arrived: 'townId', transport_delivered: 'toTownId',
movement_returned: 'townId', reinforcements_sent_home: 'ownerTownId',
};
// Command args that name a POP the command changed (the receiving POP of a movement is NOT listed: the defender gets
// an 'incoming' hint, never a plain 'town' one that would give the launch away without its ETA).
const COMMAND_TOWN_KEYS = ['townId', 'fromTownId', 'hostTownId', 'ownerTownId'];
// Deps for createPush that need the database.
function pushDeps(db) {
const owner = 'SELECT o.user_id AS uid FROM towns t JOIN operators o ON o.id = t.owner_id WHERE t.id = ?';
const valid = 'SELECT 1 AS ok FROM tokens t JOIN users u ON u.id = t.user_id WHERE t.id = ? AND t.revoked_at IS NULL AND u.banned = 0';
return {
townOwner: (townId) => { const r = db.get(owner, townId); return r ? r.uid : null; },
tokenValid: (tokenId) => !!db.get(valid, tokenId),
};
}
function bindGame({ push, db, scheduler, commands, log }) {
const toTowns = (ids) => { for (const id of ids) push.toTown(id, { type: 'town', townId: id }); };
function movementTowns(movementId, ids) {
if (!isId(movementId)) return;
const mv = db.get('SELECT from_town_id AS f, to_town_id AS t FROM movements WHERE id = ?', movementId);
if (mv) { if (isId(mv.f)) ids.add(mv.f); if (isId(mv.t)) ids.add(mv.t); }
}
// ---- notifications: written by events and commands via scheduler.notify
scheduler.onNotify = (n) => {
const p = n.payload || {};
push.toUser(n.userId, { type: 'notification', id: n.id, kind: n.type });
if (n.type === 'incoming' && isId(p.toTownId)) push.toUser(n.userId, { type: 'incoming', townId: p.toTownId, arriveAt: p.arriveAt });
else if (n.type === 'report' && isId(p.reportId)) push.toUser(n.userId, { type: 'report', id: p.reportId });
else if ((n.type === 'message' || n.type === 'alliance_message') && isId(p.messageId)) push.toUser(n.userId, { type: 'message', id: p.messageId });
const key = TOWN_KINDS[n.type];
if (key && isId(p[key])) push.toUser(n.userId, { type: 'town', townId: p[key] });
};
// ---- events: the POPs an event touched (events.town_id, or both ends of a movement)
const prevOnEvent = scheduler.onEvent;
scheduler.onEvent = (ev) => {
if (prevOnEvent) prevOnEvent(ev);
const ids = new Set();
if (isId(ev.townId)) ids.add(ev.townId);
if (ev.type === 'arrive' || ev.type === 'return') movementTowns(ev.payload && ev.payload.movementId, ids);
toTowns(ids);
};
// ---- commands
commands.hooks.onCommand = (name, args, out) => {
const ids = new Set();
for (const k of COMMAND_TOWN_KEYS) if (args && isId(args[k])) ids.add(args[k]);
if (out && isId(out.townId)) ids.add(out.townId); // joinWorld: the new POP
if (name === 'recallMovement') movementTowns(args.movementId, ids); // also tells the target's owner the warning is gone
toTowns(ids);
};
if (log) log('push hooks bound');
}
module.exports = { bindGame, pushDeps, TOWN_KINDS, COMMAND_TOWN_KEYS };