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
121 lines
6 KiB
JavaScript
121 lines
6 KiB
JavaScript
'use strict';
|
|
/*
|
|
* Pure helpers of the military / expansion screens (no DOM, no storage, no clock): what can be sent where, which units go
|
|
* on which movement, report perspective, reconnect backoff. Loads as a browser script (window.UpstreamMil) and as a Node
|
|
* module (test/client-mil.test.js). The SERVER decides everything; this only hides choices that can never work, so the
|
|
* sheet is short, and says why the rest is greyed out.
|
|
*/
|
|
(function (root, factory) {
|
|
if (typeof module === 'object' && module.exports) module.exports = factory(require('./data.js'), require('./rules.js'));
|
|
else root.UpstreamMil = factory(root.UpstreamData, root.UpstreamRules);
|
|
}(typeof self !== 'undefined' ? self : this, function (D, R) {
|
|
const TYPES = ['scan', 'raid', 'attack', 'takeover', 'reinforce', 'settle', 'transport'];
|
|
const HOSTILE = ['raid', 'attack', 'takeover'];
|
|
const ROLE_ICON = { scout: 'eye', defender: 'shield', attacker: 'target', siege: 'alert', settler: 'flag', conquest: 'handshake' };
|
|
const TYPE_ICON = { scan: 'eye', raid: 'target', attack: 'target', takeover: 'handshake', reinforce: 'shield', settle: 'flag', transport: 'box', return: 'return', unknown: 'alert' };
|
|
|
|
// Units of `home` ({id: n}) that may go on a movement of `type`: [{ unit, have }] in display order.
|
|
function unitsForType(type, home) {
|
|
const roles = D.MOVE_ROLES[type];
|
|
if (!roles) return [];
|
|
const out = [];
|
|
for (const unit of D.UNIT_ORDER) {
|
|
const def = D.UNITS[unit];
|
|
const have = (home && home[unit]) || 0;
|
|
if (have > 0 && !def.fixed && roles.indexOf(def.role) >= 0) out.push({ unit, have });
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/*
|
|
* What can be sent to a tile. c = {
|
|
* tile: GET /map/tile response,
|
|
* home: units at the sending POP, transport: { links, active } of the sending POP,
|
|
* allianceId: my alliance id or null, crews: number of rollout crews at home
|
|
* } -> [{ type, ok, reason }] (reason is a code the sheet turns into text; null when ok)
|
|
* Hidden (not even greyed): types that make no sense for this kind of tile. Pact rules (NAP, confederation) are not
|
|
* known to the client: the server refuses those and the sheet shows its message.
|
|
*/
|
|
function moveOptions(c) {
|
|
const t = c.tile, home = c.home || {};
|
|
const out = [];
|
|
if (!t) return out;
|
|
const has = (type) => unitsForType(type, home).length > 0;
|
|
const add = (type, ok, reason) => out.push({ type, ok: !!ok, reason: ok ? null : reason });
|
|
if (t.town) {
|
|
if (t.town.mine) {
|
|
add('reinforce', has('reinforce'), 'no_units');
|
|
const links = (c.transport && c.transport.links) || 0, active = (c.transport && c.transport.active) || 0;
|
|
add('transport', links > active, links === 0 ? 'no_link' : 'links_busy');
|
|
} else {
|
|
add('scan', has('scan'), 'no_units');
|
|
const prot = !!(t.town.owner && t.town.owner.protected);
|
|
for (const type of HOSTILE) {
|
|
let ok = !prot && has(type), reason = prot ? 'target_protected' : 'no_units';
|
|
if (ok && type === 'takeover' && !(home[D.EXPANSION.conquestUnit] > 0)) { ok = false; reason = 'needs_ma_team'; }
|
|
add(type, ok, reason);
|
|
}
|
|
if (c.allianceId && t.town.owner && t.town.owner.allianceId === c.allianceId) add('reinforce', has('reinforce'), 'no_units');
|
|
}
|
|
} else if (t.type === 'incumbent') {
|
|
add('scan', has('scan'), 'no_units');
|
|
add('attack', has('attack'), 'no_units');
|
|
} else if (t.type === 'land') {
|
|
const s = t.settle || {};
|
|
if (!s.possible) add('settle', false, 'settle:' + (s.code || 'invalid_target'));
|
|
else add('settle', (c.crews || 0) >= D.EXPANSION.crews, 'needs_crews');
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// Default selection when a type is picked: settle takes exactly the crews, everything else starts empty.
|
|
function defaultUnits(type, home) {
|
|
if (type === 'settle') { const o = {}; o[D.EXPANSION.settleUnit] = D.EXPANSION.crews; return o; }
|
|
return {};
|
|
}
|
|
|
|
// Drop zero entries and clamp to what is at home.
|
|
function cleanSelection(sel, home) {
|
|
const out = {};
|
|
for (const k of Object.keys(sel || {})) {
|
|
const n = Math.max(0, Math.min(Math.floor(sel[k] || 0), (home && home[k]) || 0));
|
|
if (n > 0) out[k] = n;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// Preview of a movement: distance, latency (ms), carry capacity, slowest unit.
|
|
function preview(type, from, to, units, cargo, worldSpeed) {
|
|
const dist = R.distance(from.x, from.y, to.x, to.y);
|
|
const ms = type === 'transport' ? R.transportMs(dist, worldSpeed) : R.movementMs(dist, units, worldSpeed);
|
|
const s = R.slowestSpeed(units);
|
|
return { dist, ms, carry: R.carryOf(units), slowest: isFinite(s) ? s : null };
|
|
}
|
|
|
|
const isHostile = (type) => HOSTILE.indexOf(type) >= 0;
|
|
|
|
// Report perspective: did *I* win? role is the recipient role of the report.
|
|
function perspective(role, outcome) {
|
|
const attackerSide = role === 'attacker';
|
|
const attackerWon = outcome === 'attacker_won';
|
|
return attackerSide === attackerWon ? 'victory' : 'defeat';
|
|
}
|
|
|
|
// When units that went offline are decommissioned (ms, server clock).
|
|
const decommissionAt = (offlineSince, speed) => offlineSince + R.decommissionMs(speed);
|
|
|
|
// Reconnect delay for the WebSocket: exponential from 1 s, capped at 60 s, +-25% jitter (rnd in [0,1)).
|
|
function backoffMs(attempt, rnd) {
|
|
const base = Math.min(60000, 1000 * Math.pow(2, Math.min(attempt, 10)));
|
|
return Math.round(base * (0.75 + 0.5 * (rnd === undefined ? Math.random() : rnd)));
|
|
}
|
|
|
|
const sumUnits = (u) => Object.keys(u || {}).reduce((s, k) => s + (u[k] || 0), 0);
|
|
|
|
// Exploit-kit target choices: infrastructure types (the server also takes f1..f18 slots; the client does not know them).
|
|
function siegeTargets() {
|
|
return Object.keys(D.BUILDINGS).filter((k) => D.BUILDINGS[k].kind === 'infra' && !D.BUILDINGS[k].special);
|
|
}
|
|
|
|
return { TYPES, HOSTILE, ROLE_ICON, TYPE_ICON, unitsForType, moveOptions, defaultUnits, cleanSelection, preview, isHostile, perspective, decommissionAt, backoffMs, sumUnits, siegeTargets };
|
|
}));
|