'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 }; }));