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

49
server/finalRanking.js Normal file
View file

@ -0,0 +1,49 @@
'use strict';
/*
* The final ranking snapshot written when a world ends (events/worldEnd.js) and served by GET /worlds/:id/results:
* operators by footprint, alliances by the sum over their members, top attackers and defenders by battle points, the winner.
* Same footprint numbers as /rankings (socialQueries.footprints), computed at the end time. Top D.ENDGAME.rankingTop per list.
*/
const D = require('../public/js/data.js');
const { createSocialQueries } = require('./socialQueries');
const E = require('./endgame');
const round2 = (v) => Math.round(v * 100) / 100;
function finalRanking(db, world, at) {
const social = createSocialQueries({ db, clock: { now: () => at }, scheduler: { runDue() {} } });
const fp = social.footprints(world.id, at);
const top = D.ENDGAME.rankingTop;
const ranked = (rows) => rows.slice(0, top).map((r, i) => Object.assign({ rank: i + 1 }, r));
const ops = db.all('SELECT o.id AS id, o.name AS name, o.as_number AS asn, a.id AS aid, a.tag AS tag FROM operators o LEFT JOIN alliances a ON a.id = o.alliance_id WHERE o.world_id = ?', world.id)
.map((o) => { const f = fp.get(o.id) || { footprint: 0, towns: 0 }; return { operatorId: o.id, name: o.name, asNumber: o.asn, allianceId: o.aid, allianceTag: o.tag, towns: f.towns, footprint: round2(f.footprint) }; })
.sort((x, y) => y.footprint - x.footprint || x.operatorId - y.operatorId);
const byAlliance = new Map();
for (const o of ops) {
if (!o.allianceId) continue;
const c = byAlliance.get(o.allianceId) || { footprint: 0, members: 0, towns: 0 };
c.footprint += o.footprint; c.members += 1; c.towns += o.towns;
byAlliance.set(o.allianceId, c);
}
const alliances = db.all('SELECT id, name, tag FROM alliances WHERE world_id = ?', world.id)
.map((a) => { const c = byAlliance.get(a.id) || { footprint: 0, members: 0, towns: 0 }; return { allianceId: a.id, name: a.name, tag: a.tag, members: c.members, towns: c.towns, footprint: round2(c.footprint) }; })
.sort((x, y) => y.footprint - x.footprint || x.allianceId - y.allianceId);
const stats = db.all(`SELECT o.id AS id, o.name AS name, a.tag AS tag, s.attack_points AS att, s.defense_points AS def FROM operator_stats s
JOIN operators o ON o.id = s.operator_id LEFT JOIN alliances a ON a.id = o.alliance_id WHERE o.world_id = ?`, world.id)
.map((r) => ({ operatorId: r.id, name: r.name, allianceTag: r.tag, attackPoints: round2(r.att || 0), defensePoints: round2(r.def || 0) }));
const attackers = stats.filter((r) => r.attackPoints > 0).sort((x, y) => y.attackPoints - x.attackPoints || x.operatorId - y.operatorId);
const defenders = stats.filter((r) => r.defensePoints > 0).sort((x, y) => y.defensePoints - x.defensePoints || x.operatorId - y.operatorId);
const winner = E.winnerView(db.get('SELECT * FROM worlds WHERE id = ?', world.id));
const sites = E.siteStates(db, world, at).map(({ site, row }) => E.siteView(db, world, site, row));
return {
worldId: world.id, at, winner, sites,
totals: { operators: ops.length, alliances: alliances.length },
operators: ranked(ops), alliances: ranked(alliances), attackers: ranked(attackers), defenders: ranked(defenders),
};
}
module.exports = { finalRanking };