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

@ -32,7 +32,7 @@ function ownerView(m, names) {
to: { townId: m.to_town_id, x: m.to_x, y: m.to_y, name: t ? t.name : null },
units: j(m.units), cargo: j(m.cargo), departAt: m.depart_at, arriveAt: m.arrive_at,
recallableUntil: m.type === 'return' ? null : m.depart_at + D.MILITARY.recallWindowMs,
target: extra.target || null, parentId: m.parent_id,
target: extra.target || null, parentId: m.parent_id, siteId: extra.siteId || null, // siteId: a shipment to a backbone landing site
};
}
@ -55,8 +55,27 @@ function movementLists(db, operatorId, townId) {
const inc = db.all(`SELECT m.* FROM movements m JOIN towns t ON t.id = m.to_town_id WHERE t.owner_id = ? AND m.owner_id != ? AND m.status = 'out' AND m.type != 'return'
${townId ? 'AND m.to_town_id = ?' : ''} ORDER BY m.arrive_at, m.id LIMIT ?`,
...(townId ? [operatorId, operatorId, townId, D.MILITARY.maxLists] : [operatorId, operatorId, D.MILITARY.maxLists]));
const names = townNames(db, own.concat(inc).flatMap((m) => [m.from_town_id, m.to_town_id]));
return { outgoing: own.map((m) => ownerView(m, names)), incoming: inc.map((m) => incomingView(m, names)) };
// Confederation members (own alliance included) see what is heading for each other's POPs: origin and ETA only, the type
// stays hidden. Movements of operators who are themselves partners of the target are friendly traffic and left out.
// Only in the whole-operator list, not in the per-POP lists.
let allied = [];
if (!townId) {
const partners = require('../alliances').confederatedOperators(db, operatorId);
if (partners.length) {
allied = db.all(`SELECT m.*, t.owner_id AS target_owner FROM movements m JOIN towns t ON t.id = m.to_town_id WHERE t.owner_id IN (${partners.map(() => '?').join(',')})
AND m.owner_id != t.owner_id AND m.owner_id != ? AND m.status = 'out' AND m.type != 'return' ORDER BY m.arrive_at, m.id LIMIT ?`, ...partners, operatorId, D.MILITARY.maxLists)
.filter((m) => !U.pactBlocks(db, m.owner_id, m.target_owner));
}
}
const names = townNames(db, own.concat(inc, allied).flatMap((m) => [m.from_town_id, m.to_town_id]));
const out = { outgoing: own.map((m) => ownerView(m, names)), incoming: inc.map((m) => incomingView(m, names)) };
if (!townId) {
out.alliedIncoming = allied.map((m) => {
const o = db.get('SELECT id, name FROM operators WHERE id = ?', m.target_owner);
return Object.assign(incomingView(m, names), { ally: true, toOperator: { id: o.id, name: o.name } });
});
}
return out;
}
// The military part of GET /towns/:id (the town object is already loaded and belongs to the caller).
@ -127,7 +146,7 @@ function createMilitaryQueries({ db, clock, scheduler }) {
const now = fresh();
const op = operatorFor(userId, worldId);
const world = loadWorld(db, op.world_id);
const tile = R.tileAt(world.seed, world.map_radius, x, y);
const tile = R.tileAt(world.seed, world.map_radius, x, y, R.worldAgeDays(world.start_at, now, world.speed));
if (tile.type === 'void') throw new GameError('not_found', { what: 'tile' });
const row = db.get('SELECT * FROM towns WHERE world_id = ? AND x = ? AND y = ?', world.id, x, y);
const mine = U.operatorTowns(db, op.id);
@ -140,11 +159,18 @@ function createMilitaryQueries({ db, clock, scheduler }) {
const taken = !!db.get('SELECT 1 AS x FROM incumbent_takes WHERE world_id = ? AND x = ? AND y = ? AND operator_id = ?', world.id, x, y, op.id);
out.incumbent = { takenByYou: taken, hardwareBonus: taken ? 0 : D.INCUMBENT.hardwareBonus };
}
if (tile.type === 'backbone') { // end game: who holds the site, its node level and stockpile, whether you could attack it
const E = require('../endgame');
const st = E.siteStates(db, world, now).find((s) => s.site.index === tile.site);
const v = E.siteView(db, world, st.site, st.row);
const holds = !!(op.alliance_id && v.holder && v.holder.allianceId === op.alliance_id);
out.backbone = { siteId: v.id, index: v.index, holder: v.holder, level: v.level, maxLevel: v.maxLevel, pool: v.pool, build: v.build, mine: holds, canAttack: !!op.alliance_id && !holds };
}
const fp = U.operatorFootprint(db, op.id, now);
const gate = R.footprintGate(mine.length, fp);
let nearest = Infinity;
for (const t of mine) nearest = Math.min(nearest, R.distance(t.x, t.y, x, y));
const sc = R.settleCheck({ tileType: tile.type, occupied: !!row, nearestOwnDist: nearest, ownedCount: mine.length, footprint: fp });
const sc = R.settleCheck({ tileType: tile.type, backbone: R.isBackboneSite(world.seed, world.map_radius, x, y), occupied: !!row, nearestOwnDist: nearest, ownedCount: mine.length, footprint: fp });
out.settle = { possible: sc.ok, code: sc.ok ? null : sc.code, footprint: fp, footprintNeeded: gate.need, maxDistance: D.EXPANSION.maxDistance };
return out;
}