'use strict'; /* * The end game: the Backbone race (More > Backbone) and the read-only state of an ended world. * * Screen: GET /worlds/:id/status -> phase (running | backbone_open | ended), a countdown to the day the 7 landing sites appear, the sites with * holder / node level / build progress, the leading alliance, the winner banner; when the world has ended GET /worlds/:id/results -> final ranking. * Sites your alliance holds open a "manage" sheet (GET /backbone/:id): the stockpile, build the next node level (POST /backbone/:id/build), ship * resources from a POP (POST /backbone/:id/ship, a transport) and the contribution ledger. Other sites: scan or attack through the send dialog. * * Also here (loaded at start-up, whether or not the screen is ever opened): the ended-world mode of the whole client. Once the world has ended * (`/me` says so, the `world_ended` notification arrives, or any command answers `world_ended`) the page gets `data-ended`, a pill in the top bar, * a card on the POP screen, and every action button (primary / danger buttons, `[data-act]`) is disabled with an explanation. Reading, messages and * navigation keep working; the server refuses everything else with `world_ended` anyway, so this only makes that graceful. */ (function () { const { h, clear, icon, toast, openSheet } = UI; const R = UpstreamRules; const D = UpstreamData; const E = UpstreamEnd; const mil = () => App.mil; const num = (n) => App.fmt.num(n); const nonce = () => Math.random().toString(36).slice(2, 10) + Date.now().toString(36).slice(-4); const worldId = () => (App.state.world ? App.state.world.id : 0); const myAllianceId = () => (App.state.operator ? App.state.operator.allianceId : null); const speed = () => (App.state.world ? App.state.world.speed : 1); // ================================================================== ended-world mode (whole client) let lockObserver = null, lockQueued = false; const LOCK_SEL = 'button.btn.primary, button.btn.danger, button[data-act]'; const LOCK_SKIP = '.screen-auth, .screen-settings, .screen-messages, .screen-rankings, .soc-composer, .boot, [data-keep]'; function lockButtons() { lockQueued = false; if (!App.worldEnded()) return; const msg = App.t('ended.locked'); for (const b of document.querySelectorAll(LOCK_SEL)) { if (b.disabled || b.closest(LOCK_SKIP) || b.hasAttribute('data-keep')) continue; b.disabled = true; b.dataset.endedLock = '1'; b.title = msg; } } function unlockButtons() { for (const b of document.querySelectorAll('[data-ended-lock]')) { b.disabled = false; delete b.dataset.endedLock; b.removeAttribute('title'); } } function applyEnded() { const on = App.worldEnded() && !!App.state.user; document.documentElement.toggleAttribute('data-ended', on); const pill = document.getElementById('world-pill'); if (pill) { pill.hidden = !on; pill.textContent = App.t('ended.pill'); pill.title = App.t('ended.body'); } if (on && !lockObserver) { lockObserver = new MutationObserver(() => { if (!lockQueued) { lockQueued = true; requestAnimationFrame(lockButtons); } }); lockObserver.observe(document.body, { childList: true, subtree: true }); } else if (!on && lockObserver) { lockObserver.disconnect(); lockObserver = null; unlockButtons(); } if (on) lockButtons(); for (const el of document.querySelectorAll('.wld-ended-card')) fillEndedCard(el); } App.worldEnded = () => !!(App.state.world && App.state.world.status === 'ended'); // Something told us the world has ended (a refused command, the notification, the status fetch). App.markWorldEnded = () => { if (!App.state.world) return; const was = App.worldEnded(); App.state.world.status = 'ended'; applyEnded(); if (!was) App.emit('worldended', App.state.world); }; App.checkWorld = () => applyEnded(); App.on('session', () => applyEnded()); App.on('lang', () => applyEnded()); App.on('towns', () => applyEnded()); document.addEventListener('DOMContentLoaded', () => { const pill = document.getElementById('world-pill'); if (pill) pill.addEventListener('click', () => App.go('world')); }); function fillEndedCard(el) { clear(el); el.hidden = !App.worldEnded(); if (el.hidden) return; el.appendChild(h('div', { class: 'row-ic' }, icon('trophy'))); el.appendChild(h('div', { class: 'row-main' }, h('b', { text: App.t('ended.title') }), h('span', { class: 'row-sub wrap', text: App.t('ended.body') }))); el.appendChild(h('button', { class: 'btn small', type: 'button', 'data-keep': '', onclick: () => App.go('world') }, App.t('ended.see'))); } App.popSections.push({ slot: 'top', render() { const el = h('section', { class: 'card wld-ended-card', role: 'status' }); fillEndedCard(el); return el; }, }); // ================================================================== the screen let box, bodyEl, visible = false, offTick = null, gen = 0, focusSite = null; const W = { status: null, results: null, err: null, loading: false, at: 0, rankTab: 'operators', rankN: 20, resErr: null }; const siteTitle = (s) => App.t('wld.site', { n: s.index + 1 }); const tagPill = (hd) => h('span', { class: 'pill wld-holder', title: hd.name }, '[' + hd.tag + '] ' + hd.name); const kv = (k, v) => h('div', { class: 'kv' }, h('span', { class: 'k', text: k }), h('span', { class: 'v' }, v)); const costChips = (cost, pool) => h('div', { class: 'costs' }, D.RESOURCES.map((r) => { const short = pool && (pool[r] || 0) < (cost[r] || 0); return h('span', { class: 'cost ' + r + (short ? ' short' : '') }, icon(r), h('b', { text: num(cost[r] || 0) })); })); const bar = (p, cls) => { const i = h('i'); i.style.setProperty('--p', String(Math.max(0, Math.min(1, p)))); return h('span', { class: 'bar wld-bar ' + (cls || ''), 'aria-hidden': 'true' }, i); }; const cd = (ms) => h('b', { class: 'mono wld-cd', dataset: { until: String(ms) }, text: App.fmt.dur(ms - App.now(), 3) }); async function loadStatus() { if (W.loading || !worldId()) return; W.loading = true; const my = gen; try { const st = await App.api.get('/worlds/' + worldId() + '/status'); W.status = st; W.err = null; W.at = Date.now(); if (st.phase === 'ended' || st.status === 'ended') { App.markWorldEnded(); try { const r = await App.api.get('/worlds/' + worldId() + '/results'); W.results = r.available ? r : null; W.resErr = null; } catch (e) { W.resErr = e; } } } catch (e) { W.err = e; } finally { W.loading = false; if (my === gen && visible) draw(); } } function phaseCard() { const st = W.status; const now = App.now(); const phase = E.phaseNow(st, now); const lead = E.leading(st.sites); if (phase === 'ended') { const w = st.winner; return h('section', { class: 'card wld-banner ended' }, h('div', { class: 'wld-trophy' }, icon('trophy')), h('div', { class: 'row-main' }, h('h2', { text: w && w.name ? App.t('wld.won', { name: w.name }) : App.t('wld.endedNoWinner') }), w && w.tag ? h('p', { class: 'row-sub wrap', text: '[' + w.tag + ']' + (w.wonAt ? ' · ' + App.t('wld.wonAt', { t: mil().dayTime(w.wonAt) }) : '') }) : null, h('p', { class: 'row-sub wrap', text: App.t('wld.endedAt', { t: st.endedAt ? mil().dayTime(st.endedAt) : '' }) + ' ' + App.t('ended.body') }))); } if (st.winner) { return h('section', { class: 'card wld-banner won' }, h('div', { class: 'wld-trophy' }, icon('trophy')), h('div', { class: 'row-main' }, h('h2', { text: App.t('wld.won', { name: st.winner.name }) }), h('p', { class: 'row-sub wrap' }, App.t('wld.endsIn') + ' ', st.endsAt ? cd(st.endsAt) : null), h('p', { class: 'row-sub wrap', text: App.t('wld.winnerKeep') }))); } if (phase === 'backbone_open') { return h('section', { class: 'card wld-banner open' }, h('div', { class: 'wld-trophy' }, icon('b-backbone_node')), h('div', { class: 'row-main' }, h('h2', { text: App.t('wld.open') }), lead ? h('p', { class: 'row-sub wrap', text: App.t('wld.leading', { tag: lead.tag, name: lead.name, n: lead.siteIndex + 1, level: lead.level, max: D.ENDGAME.maxLevel }) }) : h('p', { class: 'row-sub wrap', text: App.t('wld.noLeader') }), h('p', { class: 'row-sub wrap', text: App.t('wld.goal', { max: D.ENDGAME.maxLevel }) }))); } return h('section', { class: 'card wld-banner' }, h('div', { class: 'wld-trophy' }, icon('b-backbone_node')), h('div', { class: 'row-main' }, h('h2', { text: App.t('wld.opensIn') }), h('p', { class: 'wld-count' }, cd(st.opensAt)), bar(E.openProgress(st)), h('p', { class: 'row-sub wrap', text: App.t('wld.day', { day: App.fmt.dec(st.ageDays), open: st.openDay }) }))); } function howCard() { return h('section', { class: 'card' }, h('div', { class: 'card-head' }, h('h2', null, icon('flag'), App.t('wld.how.head'))), h('ol', { class: 'wld-how' }, h('li', { text: App.t('wld.how.1', { n: D.ENDGAME.sites, day: D.ENDGAME.openDay }) }), h('li', { text: App.t('wld.how.2') }), h('li', { text: App.t('wld.how.3', { max: D.ENDGAME.maxLevel }) }), h('li', { text: App.t('wld.how.4', { d: D.ENDGAME.graceDays }) }))); } function siteCard(s) { const kind = E.holderKind(s, myAllianceId()); const ended = App.worldEnded(); const now = App.now(); const prog = E.buildProgress(s, now); const kids = [ h('div', { class: 'wld-site-head' }, h('span', { class: 'row-ic' }, icon(kind === 'mine' ? 'flag' : 'b-backbone_node')), h('span', { class: 'row-main' }, h('span', { class: 'row-title' }, h('b', { text: siteTitle(s) }), mil().coordLink(s.x, s.y)), h('span', { class: 'row-sub wrap' }, s.holder ? [tagPill(s.holder), kind === 'mine' ? h('span', { class: 'pill accent', text: App.t('wld.yours') }) : null] : h('span', { class: 'pill dim', text: App.t('wld.incumbent') }), s.takenBy ? h('span', { text: ' ' + App.t('wld.takenBy', { name: s.takenBy.name }) }) : null))), h('div', { class: 'wld-level' }, h('b', { class: 'mono', text: App.t('wld.level', { n: s.level, max: s.maxLevel }) }), bar(s.progress, kind)), ]; if (s.build && !ended) { // a running build is frozen when the world ends: do not show a countdown that never finishes const b = bar(prog || 0, 'build'); b.dataset.build = String(s.index); kids.push(h('div', { class: 'wld-building' }, h('span', { class: 'row-sub wrap' }, App.t('wld.building', { n: s.build.toLevel }) + ' ', cd(s.build.dueAt)), b)); } if (s.holder && s.id) kids.push(h('div', { class: 'row-sub wrap' }, App.t('wld.stock') + ' ', mil().resChips(s.pool))); if (s.shieldUntil && s.shieldUntil > now && !ended) kids.push(h('div', { class: 'row-sub wrap' }, h('span', { class: 'pill amber' }, icon('shield'), App.t('wld.shield', { t: mil().dayTime(s.shieldUntil) })))); const acts = []; if (kind === 'mine' && s.id) acts.push(h('button', { class: 'btn small primary', type: 'button', disabled: ended, onclick: () => openManage(s) }, icon('up'), App.t('wld.manage'))); if (kind !== 'mine' && App.openSend && App.state.towns.length) acts.push(h('button', { class: 'btn small primary', type: 'button', disabled: ended, onclick: () => App.openSend({ x: s.x, y: s.y }) }, icon('target'), App.t('wld.attack'))); acts.push(h('button', { class: 'btn small', type: 'button', onclick: () => App.go('map', { x: s.x, y: s.y, zoom: 40 }) }, icon('map'), App.t('wld.onMap'))); kids.push(h('div', { class: 'wld-acts' }, acts)); return h('li', { class: 'card wld-site ' + kind, dataset: { index: String(s.index) } }, kids); } function resultsCard() { const r = W.results; if (W.resErr) return h('p', { class: 'form-error', role: 'alert', text: App.errMsg(W.resErr) }); if (!r || !r.results) return null; const res = r.results; const tabs = ['operators', 'alliances', 'attackers', 'defenders']; const list = res[W.rankTab] || []; const me = App.state.operator ? App.state.operator.id : 0; const row = (x) => { let name, sub, val; if (W.rankTab === 'alliances') { name = x.name; sub = '[' + x.tag + '] ' + App.t('soc.rank.members', { n: x.members }) + ' · ' + App.t('soc.rank.pops', { n: x.towns }); val = App.fmt.dec(x.footprint); } else if (W.rankTab === 'operators') { name = x.name; sub = (x.allianceTag ? '[' + x.allianceTag + '] ' : '') + App.t('soc.rank.pops', { n: x.towns }); val = App.fmt.dec(x.footprint); } else { name = x.name; sub = x.allianceTag ? '[' + x.allianceTag + ']' : ''; val = num(W.rankTab === 'attackers' ? x.attackPoints : x.defensePoints); } const you = (W.rankTab !== 'alliances' && x.operatorId === me) || (W.rankTab === 'alliances' && x.allianceId === myAllianceId()); const win = W.rankTab === 'alliances' && r.winner && x.allianceId === r.winner.allianceId; return h('li', { class: 'wld-rank' + (you ? ' you' : '') + (win ? ' win' : '') }, h('span', { class: 'soc-rk mono', text: '#' + x.rank }), h('span', { class: 'row-main' }, h('b', { text: name }), sub ? h('span', { class: 'row-sub', text: sub }) : null), win ? icon('trophy') : null, h('b', { class: 'mono', text: val })); }; const shown = E.topRows(list, W.rankN); return h('section', { class: 'card wld-results' }, h('div', { class: 'card-head' }, h('h2', null, icon('trophy'), App.t('wld.results'))), h('div', { class: 'soc-subtabs' }, tabs.map((k) => h('button', { class: 'chip-btn', type: 'button', 'data-keep': '', 'aria-pressed': String(W.rankTab === k), onclick: () => { W.rankTab = k; W.rankN = 20; draw(); } }, App.t('wld.rank.' + k)))), shown.length ? h('ul', { class: 'wld-ranks' }, shown.map(row)) : h('p', { class: 'empty', text: App.t('wld.rank.empty') }), list.length > shown.length ? h('button', { class: 'btn wide', type: 'button', 'data-keep': '', onclick: () => { W.rankN += 20; draw(); } }, App.t('trd.more')) : null, res.totals ? h('p', { class: 'row-sub', text: App.t('wld.rank.totals', { o: res.totals.operators, a: res.totals.alliances }) }) : null); } function draw() { if (!bodyEl) return; const top = box.scrollTop; clear(bodyEl); if (W.err && !W.status) { bodyEl.appendChild(h('div', { class: 'soc-err' }, h('p', { class: 'form-error', role: 'alert', text: App.errMsg(W.err) }), h('button', { class: 'btn small', type: 'button', onclick: () => loadStatus() }, icon('refresh'), App.t('common.retry')))); return; } if (!W.status) { bodyEl.appendChild(h('p', { class: 'empty', text: App.t('common.loading') })); return; } const st = W.status; bodyEl.appendChild(phaseCard()); const phase = E.phaseNow(st, App.now()); if (phase === 'running') bodyEl.appendChild(howCard()); if (st.sites.length) { bodyEl.appendChild(h('h3', { class: 'sub wld-sub', text: App.t('wld.sites') })); bodyEl.appendChild(h('ul', { class: 'wld-sites' }, st.sites.map(siteCard))); } else if (phase === 'backbone_open') { bodyEl.appendChild(h('p', { class: 'empty', text: App.t('wld.noSites') })); } if (phase === 'ended') { const rc = resultsCard(); if (rc) bodyEl.appendChild(rc); } box.scrollTop = top; if (focusSite !== null) { const el = bodyEl.querySelector('[data-index="' + focusSite + '"]'); focusSite = null; if (el) { el.scrollIntoView({ block: 'center' }); el.classList.add('flash'); } } } // ---- the manage sheet of a site your alliance holds async function openManage(site0) { const body = h('div', { class: 'detail wld-manage' }, h('p', { class: 'empty', text: App.t('common.loading') })); const sheet = openSheet({ title: siteTitle(site0), icon: 'b-backbone_node', closeLabel: App.t('common.close'), body }); let data = null, pops = null; const sh = { fromId: 0, cargo: {}, nonce: nonce() }; async function load() { try { const [d, p] = await Promise.all([ App.api.get('/backbone/' + site0.id), pops ? Promise.resolve(pops) : Promise.all(App.state.towns.map((t) => mil().loadTown(t.id).then((x) => ({ id: t.id, name: t.name, x: t.x, y: t.y, td: x.town, rt: App.rulesTownFrom(x.town, x.serverTime) })).catch(() => null))), ]); data = d; pops = p.filter(Boolean); if (!pops.some((q) => q.id === sh.fromId)) { sh.fromId = (pops.find((q) => q.td.military.transport.links > 0) || pops[0] || { id: 0 }).id; sh.cargo = {}; } drawManage(); } catch (e) { clear(body); body.appendChild(h('p', { class: 'form-error', role: 'alert', text: App.errMsg(e) })); } } function drawManage() { clear(body); const s = data.site, you = data.you; sheet.setTitle(siteTitle(s)); if (!you.holder) { body.appendChild(h('p', { class: 'reason', text: App.t('wld.lost') })); } body.appendChild(h('div', null, kv(App.t('wld.levelShort'), h('b', { class: 'mono', text: App.t('wld.level', { n: s.level, max: s.maxLevel }) })))); body.appendChild(bar(s.progress, 'mine')); body.appendChild(h('div', null, kv(App.t('wld.stock'), mil().resChips(s.pool)), kv(App.t('wld.coords'), mil().coordLink(s.x, s.y)))); // ---- build the next node level const chk = E.buildCheck(s, you, App.worldEnded()); const buildErr = h('p', { class: 'form-error', role: 'alert', hidden: true }); const sec = h('section', { class: 'wld-sec' }, h('h3', { class: 'sub', text: App.t('wld.build.head') })); if (s.build) sec.appendChild(h('div', { class: 'detail-queue' }, h('b', { text: App.t('wld.building', { n: s.build.toLevel }) }), h('span', null, cd(s.build.dueAt)), bar(E.buildProgress(s, App.now()) || 0, 'build'))); if (s.next && !s.build) { sec.appendChild(h('p', { class: 'row-sub wrap', text: App.t('wld.build.next', { n: s.next.toLevel, t: App.fmt.dur(s.next.durationMs) }) })); sec.appendChild(costChips(s.next.cost, s.pool)); const lack = E.missing(s.next.cost, s.pool); if (Object.keys(lack).length) sec.appendChild(h('p', { class: 'reason', text: App.t('wld.build.lack', { what: D.RESOURCES.filter((r) => lack[r]).map((r) => num(lack[r]) + ' ' + App.resName(r)).join(', ') }) })); } else if (!s.next) sec.appendChild(h('p', { class: 'good', text: App.t('wld.build.max') })); const btn = h('button', { class: 'btn primary wide', type: 'button', disabled: !chk.ok, onclick: async () => { btn.disabled = true; buildErr.hidden = true; try { const r = await App.api.post('/backbone/' + s.id + '/build', {}, { intent: 'bb-build:' + s.id + ':' + (s.next ? s.next.toLevel : 0) }); toast(App.t('wld.build.started', { n: r.toLevel, t: App.fmt.dur(r.dueAt - App.now()) }), 'ok'); await Promise.all([load(), loadStatus()]); } catch (e) { buildErr.textContent = App.errMsg(e); buildErr.hidden = false; btn.disabled = false; load(); } } }, icon('up'), s.next ? App.t('wld.build.go', { n: s.build ? s.build.toLevel : s.next.toLevel }) : App.t('wld.build.max')); sec.appendChild(buildErr); if (chk.reason && chk.reason !== 'not_enough_resources' && chk.reason !== 'max_level' && chk.reason !== 'already_queued') sec.appendChild(h('p', { class: 'reason', text: App.t('wld.build.why.' + chk.reason) })); sec.appendChild(btn); body.appendChild(sec); // ---- garrison: what defends the site on top of its static part (units of the holders' POPs); call your own home, station more const gv = E.garrisonView(data); const gar = h('section', { class: 'wld-sec' }, h('h3', { class: 'sub', text: App.t('wld.gar.head') })); const garErr = h('p', { class: 'form-error', role: 'alert', hidden: true }); gar.appendChild(h('div', null, kv(App.t('wld.gar.static'), h('b', { class: 'mono', text: num(gv.static) })), kv(App.t('wld.gar.units'), h('b', { class: 'mono', text: num(gv.garrison) })), kv(App.t('wld.gar.total'), h('b', { class: 'mono', text: num(gv.total) })))); if (!gv.rows.length) gar.appendChild(h('p', { class: 'row-sub wrap', text: App.t('wld.gar.empty') })); else { gar.appendChild(h('ul', { class: 'mil-list' }, gv.rows.map((r) => h('li', { class: 'mil-row' }, h('span', { class: 'row-main' }, h('b', { text: r.name + (r.mine ? ' · ' + App.t('wld.gar.mine') : '') }), h('span', { class: 'row-sub wrap', text: [r.townName, mil().unitsText(r.units), r.online ? App.t('wld.gar.share', { p: Math.round(r.share * 100) }) : App.t('wld.gar.offline')].filter(Boolean).join(' · ') })), r.mine ? h('button', { class: 'btn small', type: 'button', disabled: App.worldEnded(), onclick: async (ev) => { const b = ev.currentTarget; b.disabled = true; garErr.hidden = true; try { await App.api.post('/backbone/' + s.id + '/withdraw', { fromTownId: r.ownerTownId }, { intent: 'bb-wd:' + s.id + ':' + r.ownerTownId + ':' + nonce() }); toast(App.t('wld.gar.withdrawn'), 'ok'); App.refreshTown().catch(() => {}); App.refreshMovements().catch(() => {}); await load(); } catch (e) { garErr.textContent = App.errMsg(e); garErr.hidden = false; b.disabled = false; load(); } } }, icon('return'), App.t('wld.gar.withdraw')) : null)))); } gar.appendChild(garErr); gar.appendChild(h('p', { class: 'row-sub wrap', text: App.t('wld.gar.hint') })); if (you.canReinforce && App.openSend && App.state.towns.length) gar.appendChild(h('button', { class: 'btn wide', type: 'button', disabled: App.worldEnded(), onclick: () => App.openSend({ x: s.x, y: s.y, type: 'reinforce' }) }, icon('shield'), App.t('wld.gar.station'))); body.appendChild(gar); // ---- ship resources const ship = h('section', { class: 'wld-sec' }, h('h3', { class: 'sub', text: App.t('wld.ship.head') })); const cur = pops.find((q) => q.id === sh.fromId); if (!you.canShip) ship.appendChild(h('p', { class: 'reason', text: App.t('wld.ship.no') })); else if (!cur) ship.appendChild(h('p', { class: 'empty', text: App.t('pop.noTown') })); else { const tr = cur.td.military.transport, free = Math.max(0, tr.links - tr.active), cap = tr.capacityPerLink; const prev = h('div', { class: 'send-preview' }); const shipErr = h('p', { class: 'form-error', role: 'alert', hidden: true }); const go = h('button', { class: 'btn primary wide', type: 'button' }, icon('box'), App.t('wld.ship.go')); const refresh = () => { const total = CargoUI.cargoTotal(sh.cargo); clear(prev); const dist = R.distance(cur.x, cur.y, s.x, s.y); prev.appendChild(kv(App.t('wld.ship.cargo'), h('b', { class: 'mono' + (total > cap ? ' bad' : ''), text: num(total) + ' / ' + num(cap) }))); prev.appendChild(kv(App.t('send.distance'), h('span', { class: 'mono', text: App.fmt.dec(dist) + ' ' + App.t('common.tiles') }))); prev.appendChild(kv(App.t('send.latency'), h('span', { class: 'mono', text: App.fmt.dur(R.transportMs(dist, speed())) }))); prev.appendChild(kv(App.t('trd.links.head'), h('span', { class: 'mono', text: free + ' / ' + tr.links }))); go.disabled = total < 1 || total > cap || !free; }; if (pops.length > 1) ship.appendChild(h('label', { class: 'field' }, h('span', { class: 'field-lbl', text: App.t('send.from') }), h('select', { onchange: (e) => { sh.fromId = Number(e.target.value); sh.cargo = {}; drawManage(); } }, pops.map((q) => h('option', { value: q.id, selected: q.id === sh.fromId, text: q.name }))))); ship.appendChild(h('p', { class: 'row-sub wrap', text: App.t('wld.ship.hint', { n: num(cap) }) })); if (!tr.links) ship.appendChild(h('p', { class: 'reason', text: App.t('err.no_transport_link') })); const have = R.resourcesAt(cur.rt, App.now()).amounts; for (const row of CargoUI.cargoPickers(sh.cargo, have, refresh)) ship.appendChild(row); ship.appendChild(prev); ship.appendChild(shipErr); ship.appendChild(go); go.addEventListener('click', async () => { go.disabled = true; shipErr.hidden = true; try { await App.api.post('/backbone/' + s.id + '/ship', { fromTownId: sh.fromId, cargo: Object.assign({}, sh.cargo) }, { intent: 'bb-ship:' + sh.nonce }); sh.nonce = nonce(); sh.cargo = {}; pops = null; toast(App.t('wld.ship.done'), 'ok'); App.refreshTown().catch(() => {}); App.refreshMovements().catch(() => {}); await load(); } catch (e) { shipErr.textContent = App.errMsg(e); shipErr.hidden = false; if (e.code !== 'network_error') sh.nonce = nonce(); refresh(); } }); refresh(); } body.appendChild(ship); // ---- ledger const ledger = (data.contributions || []).filter((c) => c.fiber || c.hardware || c.capital); if (ledger.length) { body.appendChild(h('section', { class: 'wld-sec' }, h('h3', { class: 'sub', text: App.t('wld.ledger') }), h('ul', { class: 'mil-list' }, ledger.slice(0, 15).map((c) => h('li', { class: 'mil-row' }, h('span', { class: 'row-main' }, h('b', { text: c.name }), mil().resChips({ fiber: c.fiber, hardware: c.hardware, capital: c.capital }))))), )); } } load(); } // ---- ticks: countdown texts, build bars, phase flips function tick(now) { if (!box) return; for (const el of bodyEl.querySelectorAll('.wld-cd')) { const s = App.fmt.dur(Number(el.dataset.until) - now, 3); if (el.textContent !== s) el.textContent = s; } for (const el of bodyEl.querySelectorAll('[data-build]')) { const site = W.status && W.status.sites.find((x) => String(x.index) === el.dataset.build); const p = site ? E.buildProgress(site, now) : null; if (p !== null) el.firstChild.style.setProperty('--p', String(p)); } if (W.status && !W.loading) { const flip = W.status.phase === 'running' && now >= W.status.opensAt + 800; const due = W.status.sites.some((s) => s.build && s.build.dueAt < now - 1500) || (W.status.winner && W.status.endsAt && W.status.phase !== 'ended' && now > W.status.endsAt + 1500); if ((flip || due) && Date.now() - W.at > 4000) loadStatus(); else if (Date.now() - W.at > 60000) loadStatus(); } } App.registerScreen({ id: 'world', titleKey: 'screen.world', navKey: 'nav.world', icon: 'b-backbone_node', hud: false, navGroup: 'more', render(container) { box = container; bodyEl = h('div', { class: 'wld-body' }); box.appendChild(h('div', { class: 'page wld-page' }, h('div', { class: 'card-head page-head' }, h('h1', { id: 'wld-h', text: App.t('screen.world') }), h('button', { class: 'icon-btn', type: 'button', 'aria-label': App.t('common.retry'), 'data-keep': '', onclick: () => loadStatus() }, icon('refresh'))), bodyEl)); App.on('session', () => { W.status = null; W.results = null; W.err = null; }); App.api.on('push', (msg) => { if (!msg || msg.type !== 'notification' || !App.state.user) return; if (msg.kind === 'world_ended') { App.markWorldEnded(); App.refreshTowns().catch(() => {}); } if (/^(backbone_|world_ended)/.test(msg.kind || '')) { if (visible) loadStatus(); else W.at = 0; } }); }, onShow(params) { visible = true; gen++; document.getElementById('wld-h').textContent = App.t('screen.world'); if (params && Number.isInteger(params.site)) focusSite = params.site; draw(); loadStatus(); if (offTick) offTick(); offTick = App.onTick(tick); }, onHide() { visible = false; gen++; if (offTick) offTick(); offTick = null; }, onLang() { document.getElementById('wld-h').textContent = App.t('screen.world'); draw(); }, }); }());