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

View file

@ -0,0 +1,22 @@
'use strict';
/* A Backbone node level finished. Payload { siteId, toLevel, by }. Idempotent: only acts while the site's build points at THIS
* event. The level lands on the SITE (a capture during the build does not stop it); level maxLevel makes the holder alliance the
* winner (once per world) and schedules the end of the world. */
const D = require('../../public/js/data.js');
const { loadWorld } = require('../state');
const E = require('../endgame');
module.exports = function backboneDone(ctx, ev) {
const { db } = ctx;
const site = E.siteRow(db, ev.payload.siteId);
if (!site || site.build_event_id !== ev.id) return;
const world = loadWorld(db, site.world_id);
if (!world || world.status === 'ended') return;
const level = site.build_to;
db.run('UPDATE landing_sites SET level = ?, build_to = NULL, build_event_id = NULL, build_started_at = NULL, build_due_at = NULL, build_cost = NULL WHERE id = ?', level, site.id);
const fresh = E.siteRow(db, site.id);
if (fresh.holder_alliance_id && (level % D.ENDGAME.notifyEvery === 0 || level >= D.ENDGAME.maxLevel)) {
E.notifyAlliance(ctx, fresh.holder_alliance_id, 'backbone_level', { siteId: site.id, index: site.idx, level });
}
E.maybeWin(ctx, fresh, ev.dueAt);
};

View file

@ -1,5 +1,8 @@
'use strict';
/* A building level finished. Payload: { key, type, toLevel, userId }. Idempotent: a level never goes down. */
/* A building level finished. Payload: { key, type, toLevel, userId }. Each build event adds exactly ONE level to what the
* building is at the moment it finishes (relative, not absolute): exploit kits may have knocked the building down while the
* upgrade was in flight, and the finished level must not undo that. Never lowers a level; `toLevel` is the level the
* upgrade was queued for and is only an upper bound. Events run once (status), so +1 is safe. */
const { loadTown, settle } = require('../state');
module.exports = function buildDone(ctx, ev) {
@ -9,8 +12,12 @@ module.exports = function buildDone(ctx, ev) {
if (!town) return;
settle(db, town, ev.dueAt); // values up to the moment of completion, at the OLD rates
const cur = town.buildings[key];
if (!cur) db.run('INSERT INTO buildings (town_id, key, type, level) VALUES (?, ?, ?, ?)', town.id, key, type, toLevel);
else if (cur.level < toLevel) db.run('UPDATE buildings SET level = ? WHERE town_id = ? AND key = ?', toLevel, town.id, key);
let level;
if (!cur) { level = 1; db.run('INSERT INTO buildings (town_id, key, type, level) VALUES (?, ?, ?, ?)', town.id, key, type, level); }
else {
level = cur.level < toLevel ? cur.level + 1 : cur.level;
if (level !== cur.level) db.run('UPDATE buildings SET level = ? WHERE town_id = ? AND key = ?', level, town.id, key);
}
require('../military/power').reschedule(ctx, town.id, ev.dueAt); // building draw / battery cap changed: units' power check
if (userId) ctx.notify(userId, ev.worldId, 'build_done', { townId: town.id, key, type, level: toLevel });
if (userId) ctx.notify(userId, ev.worldId, 'build_done', { townId: town.id, key, type, level });
};

View file

@ -10,4 +10,8 @@ module.exports = {
reportCleanup: require('./reportCleanup'),
buildDone: require('./buildDone'),
diploChange: require('./diploChange'),
// trade and end game
tradeExpire: require('./tradeExpire'),
backboneDone: require('./backboneDone'),
worldEnd: require('./worldEnd'),
};

View file

@ -0,0 +1,12 @@
'use strict';
/* An open Internet exchange offer ran out of time: the escrow goes back to the POP. Payload { offerId }. Idempotent: acts only
* while the offer is still open (accepted/cancelled offers cancel their own expiry event, this is the second line of defence). */
const T = require('../trade');
module.exports = function tradeExpire(ctx, ev) {
const o = T.getOffer(ctx.db, ev.payload.offerId);
if (!o || o.status !== 'open') return;
T.closeOffer(ctx, o, 'expired', ev.dueAt);
const uid = T.userOfOperator(ctx.db, o.operator_id);
if (uid) ctx.notify(uid, o.world_id, 'trade_expired', { offerId: o.id, townId: o.town_id, returned: { resource: o.give_res, amount: o.give_amount } });
};

20
server/events/worldEnd.js Normal file
View file

@ -0,0 +1,20 @@
'use strict';
/* The world ends (two world days after a node reached the last level; payload { reason, siteId }). Stores the final ranking
* snapshot, sets status 'ended', gives the escrow of open trade offers back to its POPs, cancels everything still pending in this world (the world is frozen: no arrival, training or
* trade changes anything any more; reads keep working) and tells every operator. Idempotent: an ended world is left alone. */
const { loadWorld } = require('../state');
const E = require('../endgame');
const T = require('../trade');
const { finalRanking } = require('../finalRanking');
module.exports = function worldEnd(ctx, ev) {
const { db } = ctx;
const world = loadWorld(db, ev.worldId);
if (!world || world.status === 'ended') return;
const snapshot = finalRanking(db, world, ev.dueAt);
db.run('INSERT OR REPLACE INTO world_rankings (world_id, created_at, data) VALUES (?, ?, ?)', world.id, ev.dueAt, JSON.stringify(snapshot));
db.run("UPDATE worlds SET status = 'ended', end_at = ? WHERE id = ?", ev.dueAt, world.id);
for (const o of db.all("SELECT * FROM trade_offers WHERE world_id = ? AND status = 'open'", world.id)) T.closeOffer(ctx, o, 'expired', ev.dueAt); // escrow back in its POP: every unit stays accounted for
db.run("UPDATE events SET status = 'cancelled', done_at = ? WHERE world_id = ? AND status = 'pending' AND id != ?", ev.dueAt, world.id, ev.id);
E.notifyWorld(ctx, world.id, 'world_ended', { winner: snapshot.winner, endedAt: ev.dueAt });
};