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
98 lines
5.2 KiB
JavaScript
98 lines
5.2 KiB
JavaScript
'use strict';
|
|
/*
|
|
* Pure helpers of the end game screen (no DOM, no storage, no clock): who leads the race, how far a node build is, what a build
|
|
* still lacks, what a holder may do. Loads as a browser script (window.UpstreamEnd) and as a Node module (test/client-world.test.js).
|
|
* The SERVER decides everything (GET /worlds/:id/status, GET /backbone/:id); this only avoids doomed taps and explains the greyed-out ones.
|
|
*
|
|
* A site is the `LandingSite` of docs/openapi.json: { id | null, index, x, y, holder: { allianceId, tag, name } | null, takenBy, takenAt,
|
|
* level, maxLevel, progress, pool: { fiber, hardware, capital }, build: { toLevel, startedAt, dueAt } | null, next: { toLevel, cost, durationMs } | null }.
|
|
*/
|
|
(function (root, factory) {
|
|
if (typeof module === 'object' && module.exports) module.exports = factory(require('./data.js'));
|
|
else root.UpstreamEnd = factory(root.UpstreamData);
|
|
}(typeof self !== 'undefined' ? self : this, function (D) {
|
|
const RES = D.RESOURCES;
|
|
|
|
// The alliance furthest in the race: highest node level on any of its sites; ties by more sites held, then the earlier takeover.
|
|
// -> { allianceId, tag, name, level, sites, siteIndex } | null (nobody holds a site)
|
|
function leading(sites) {
|
|
const by = new Map();
|
|
for (const s of sites || []) {
|
|
if (!s.holder) continue;
|
|
const k = s.holder.allianceId;
|
|
const c = by.get(k) || { allianceId: k, tag: s.holder.tag, name: s.holder.name, level: -1, sites: 0, siteIndex: null, at: Infinity };
|
|
c.sites++;
|
|
if (s.level > c.level) { c.level = s.level; c.siteIndex = s.index; }
|
|
c.at = Math.min(c.at, s.takenAt || Infinity);
|
|
by.set(k, c);
|
|
}
|
|
const all = Array.from(by.values());
|
|
if (!all.length) return null;
|
|
all.sort((a, b) => b.level - a.level || b.sites - a.sites || a.at - b.at || a.allianceId - b.allianceId);
|
|
const w = all[0];
|
|
return { allianceId: w.allianceId, tag: w.tag, name: w.name, level: w.level, sites: w.sites, siteIndex: w.siteIndex };
|
|
}
|
|
|
|
// Progress 0..1 of the node level being built, or null when nothing is being built.
|
|
function buildProgress(site, now) {
|
|
const b = site && site.build;
|
|
if (!b) return null;
|
|
return Math.max(0, Math.min(1, (now - b.startedAt) / Math.max(1, b.dueAt - b.startedAt)));
|
|
}
|
|
|
|
// What the stockpile lacks for `cost`: { res: missing } (empty object = enough).
|
|
function missing(cost, pool) {
|
|
const out = {};
|
|
for (const r of RES) {
|
|
const lack = ((cost && cost[r]) || 0) - ((pool && pool[r]) || 0);
|
|
if (lack > 0) out[r] = lack;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// Who holds the site, seen from `allianceId`: 'mine' | 'rival' | 'incumbent'.
|
|
function holderKind(site, allianceId) {
|
|
if (!site.holder) return 'incumbent';
|
|
return allianceId && site.holder.allianceId === allianceId ? 'mine' : 'rival';
|
|
}
|
|
|
|
// May the node be built now? -> { ok, reason } with reason in: ended, forbidden, max_level, already_queued, not_enough_resources.
|
|
// `you` = the `you` block of GET /backbone/:id.
|
|
function buildCheck(site, you, ended) {
|
|
if (ended) return { ok: false, reason: 'ended' };
|
|
if (!you || !you.holder || !you.canBuild) return { ok: false, reason: 'forbidden' };
|
|
if (!site.next) return { ok: false, reason: 'max_level' };
|
|
if (site.build) return { ok: false, reason: 'already_queued' };
|
|
if (Object.keys(missing(site.next.cost, site.pool)).length) return { ok: false, reason: 'not_enough_resources' };
|
|
return { ok: true, reason: null };
|
|
}
|
|
|
|
// The phase as the clock sees it: the status says 'running' until it is refetched, but the sites open at opensAt.
|
|
function phaseNow(status, now) {
|
|
if (!status) return 'running';
|
|
if (status.phase === 'ended') return 'ended';
|
|
if (status.phase === 'running' && now >= status.opensAt) return 'backbone_open';
|
|
return status.phase;
|
|
}
|
|
|
|
// Share of the way to the opening day, 0..1.
|
|
const openProgress = (status) => (status && status.openDay ? Math.max(0, Math.min(1, status.ageDays / status.openDay)) : 0);
|
|
|
|
// The garrison of a site as the holders' manage sheet shows it (GET /backbone/:id: `defence` + `garrison`): rows sorted by defence, with the
|
|
// share of the whole defence, and what the static part is worth. -> { static, garrison, total, rows: [{ ownerTownId, townName, name, units, power, share, mine, online }] }
|
|
function garrisonView(data) {
|
|
const def = (data && data.defence) || { static: 0, garrison: 0, total: 0 };
|
|
const rows = ((data && data.garrison) || []).map((g) => ({
|
|
ownerTownId: g.ownerTownId, townName: g.townName, name: g.operator ? g.operator.name : '', units: g.units || {}, power: g.power || 0,
|
|
share: def.total > 0 ? (g.online ? (g.power || 0) / def.total : 0) : 0, mine: !!g.mine, online: g.online !== false,
|
|
})).sort((a, b) => b.power - a.power || a.ownerTownId - b.ownerTownId);
|
|
return { static: def.static, garrison: def.garrison, total: def.total, rows };
|
|
}
|
|
|
|
const sumCargo = (c) => Object.keys(c || {}).reduce((s, k) => s + (c[k] || 0), 0);
|
|
|
|
// Rows of a final ranking list for display: the first `n` (a snapshot holds the top 100).
|
|
const topRows = (list, n) => (list || []).slice(0, n);
|
|
|
|
return { leading, buildProgress, missing, holderKind, buildCheck, phaseNow, openProgress, sumCargo, topRows, garrisonView };
|
|
}));
|