Built: - Client: trade screen, world/end-game screen (landing sites, shipping, node build, final ranking), report sharing + alliance reports tab, alert describers for all notification types, error-code strings (en+nb) for every server CODE. - tools/bot.js (+ tools/bot/): 50 headless bots against the real server in-process with a fake clock; resource/unit conservation ledger, exit 1 on broken invariants. docs/BALANCE.md has method and every run behind a number. - Balance changes (evidence in docs/BALANCE.md): secondQueueCoreLevel 10 -> 3, TRANSPORT.capacityPerLink 500 -> 1000 (end game was shipping-limited in bot runs). - Server fixes: POST /reports/:id/share 500 without a pending reportCleanup; bounced link shipments were silent about cargo overflow. Tested: npm test 207/207 (new: bot determinism + invariants, client alerts/world/i18n over CODES, two regression tests that failed before their fix); openapi check up to date. Real-browser scratch runs (not in repo): WebSocket push shows an incoming-attack banner ~214 ms after launch, polling fallback ~52 s, 15 scripted flows, 71 screenshots. NOT tested: Playwright suite (e2e/ still empty), real devices/Safari, nginx proxy websockets, throttled networks, HTTP-level bot load. NOT claimed: balance. Bots are not humans; open balance items (loot waste, escrow refund clipping, no garrisons at landing sites, end game not scaling with world speed) are listed in docs/BALANCE.md. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
87 lines
4.3 KiB
JavaScript
87 lines
4.3 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);
|
|
|
|
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 };
|
|
}));
|