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
85 lines
4.6 KiB
JavaScript
85 lines
4.6 KiB
JavaScript
'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',
|
|
garrison_arrived: 'ownerTownId', garrison_released: 'ownerTownId', // units of that POP joined / left a landing site garrison: its power draw changed
|
|
};
|
|
|
|
// 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 };
|