'use strict'; /* * Reports: newest first, cursor-paginated (GET /reports?after=), unread highlighted, a detail sheet per report * (GET /reports/:id, then POST /reports/:id/read). Detail layouts exist for battle (raid/attack/takeover), scan, * scan_detected, incumbent, settle, reinforce and movement_failed; coordinates are links that jump to the map. * `data` differs by report type AND by the recipient's role (attacker / defender / reinforcer / sender / target). */ (function () { const { h, clear, icon, toast, openSheet } = UI; const D = UpstreamData; const Mil = UpstreamMil; const mil = () => App.mil; const num = (n) => App.fmt.num(n); const PAGE = 25; let box, listEl, moreBtn, statusEl, items = [], next = null, loading = false, visible = false, offAgo = null; // ------------------------------------------------------------------ list rows const nameOf = (o) => (o && o.name) || '?'; const townOf = (t) => (t ? t.name : '?'); // { icon, title, sub, tone } for the list function describe(it) { const s = it.summary || {}; switch (it.type) { case 'raid': case 'attack': case 'takeover': { const res = Mil.perspective(it.role, s.outcome); const mine = it.role === 'attacker'; const title = mil().reportTypeLabel(it.type) + ': ' + townOf(s.target); const sub = mine ? App.t('rep.sub.you_vs', { name: nameOf(s.defender) }) : App.t('rep.sub.by', { name: nameOf(s.attacker) }); return { icon: Mil.TYPE_ICON[it.type], title, sub: sub + (s.captured ? ' · ' + App.t('rep.captured') : ''), tone: res === 'victory' ? 'good' : 'bad', badge: App.t('rep.' + res) }; } case 'scan': { const where = s.target ? townOf(s.target) : (s.site ? s.site.x + ', ' + s.site.y : '?'); return { icon: 'eye', title: App.t('rep.scan.title', { where }), sub: s.success ? App.t('rep.scan.ok') : App.t('rep.scan.failed'), tone: s.success ? 'good' : 'bad' }; } case 'scan_detected': return { icon: 'eye', title: App.t('rep.detected.title', { town: townOf(s.target) }), sub: s.identityRevealed && s.by ? App.t('rep.sub.by', { name: nameOf(s.by) }) : App.t('rep.detected.unknown'), tone: 'warn' }; case 'incumbent': return { icon: 'b-core_router', title: App.t('rep.incumbent.title', { x: s.site ? s.site.x : '?', y: s.site ? s.site.y : '?' }), sub: '', tone: s.outcome === 'attacker_won' ? 'good' : 'bad', badge: s.outcome === 'attacker_won' ? App.t('rep.victory') : App.t('rep.defeat') }; case 'settle': return { icon: 'flag', title: App.t('rep.settle.title', { name: s.town ? s.town.name : '?' }), sub: '', tone: 'good' }; case 'reinforce': return { icon: 'shield', title: it.role === 'sender' ? App.t('rep.reinforce.sender', { town: townOf(s.host) }) : App.t('rep.reinforce.target', { town: townOf(s.host) }), sub: mil().unitsText(s.units), tone: 'neutral' }; case 'movement_failed': return { icon: 'alert', title: App.t('rep.failed.title', { type: mil().typeLabel(s.movementType) }), sub: reasonText(s.reason), tone: 'bad' }; default: return { icon: 'report', title: mil().reportTypeLabel(it.type), sub: '', tone: 'neutral' }; } } function reasonText(code) { if (!code) return ''; if (UpstreamI18n.strings.en['rep.reason.' + code]) return App.t('rep.reason.' + code); if (UpstreamI18n.strings.en['err.' + code]) return App.t('err.' + code); return code; } function row(it) { const d = describe(it); return h('li', null, h('button', { class: 'alert rep tone-' + d.tone + (it.read ? '' : ' new'), type: 'button', dataset: { id: String(it.id) }, onclick: () => openReport(it.id) }, h('span', { class: 'row-ic' }, icon(d.icon)), h('span', { class: 'row-main' }, h('span', { class: 'row-title' }, h('b', { text: d.title }), d.badge ? h('span', { class: 'pill ' + (d.tone === 'good' ? 'green' : 'amber'), text: d.badge }) : null, it.read ? null : h('span', { class: 'pill accent', text: App.t('alerts.new') })), d.sub ? h('span', { class: 'row-sub', text: d.sub }) : null), h('span', { class: 'when mono', dataset: { at: String(it.createdAt) }, text: App.fmt.ago(App.now() - it.createdAt) }))); } function draw() { clear(listEl); if (!items.length) { listEl.appendChild(h('li', null, h('p', { class: 'empty pad', text: loading ? App.t('common.loading') : App.t('rep.empty') }))); } for (const it of items) listEl.appendChild(row(it)); moreBtn.hidden = next === null; } async function load(more) { if (loading) return; loading = true; statusEl.hidden = true; try { const r = await App.api.get('/reports', { limit: PAGE, after: more ? next : undefined }); items = more ? items.concat(r.items.filter((x) => !items.some((y) => y.id === x.id))) : r.items; next = r.next; mil().reportsUnread = r.unread; App.setBadge('reports', r.unread, 'info'); } catch (e) { statusEl.textContent = App.errMsg(e); statusEl.hidden = false; } finally { loading = false; } draw(); } async function markAll() { try { await App.api.post('/reports/read-all', {}, { intent: 'reports:read-all' }); for (const it of items) it.read = true; App.setBadge('reports', 0, 'info'); draw(); } catch (e) { toast(App.errMsg(e), 'error'); } } // ------------------------------------------------------------------ detail pieces const sec = (title, ...kids) => h('section', { class: 'rep-sec' }, h('h3', { class: 'sub', text: title }), kids); const kv = (k, v) => h('div', { class: 'kv' }, h('span', { class: 'k', text: k }), h('span', { class: 'v' }, v)); function unitsTable(sent, lost, survivors) { const ids = D.UNIT_ORDER.filter((u) => (sent && sent[u]) || (lost && lost[u]) || (survivors && survivors[u])); if (!ids.length) return h('p', { class: 'empty', text: App.t('rep.noUnits') }); return h('table', { class: 'mil-table' }, h('thead', null, h('tr', null, h('th', { text: '' }), h('th', { text: App.t('rep.sent') }), h('th', { text: App.t('rep.lost') }), h('th', { text: App.t('rep.left') }))), h('tbody', null, ids.map((u) => h('tr', null, h('td', { text: mil().unitName(u) }), h('td', { class: 'mono', text: num((sent && sent[u]) || 0) }), h('td', { class: 'mono' + (lost && lost[u] ? ' bad' : ''), text: num((lost && lost[u]) || 0) }), h('td', { class: 'mono', text: num((survivors && survivors[u]) || 0) }))))); } const party = (label, operator, town) => kv(label, h('span', { class: 'rep-party' }, h('b', { text: operator ? operator.name : '?' }), operator && operator.asNumber ? h('small', { class: 'dimmer', text: ' ' + UpstreamI18n.fmtAs(operator.asNumber) }) : null, town ? mil().endLink(town) : null)); function verdict(role, outcome) { const res = Mil.perspective(role, outcome); return h('div', { class: 'rep-verdict ' + res }, icon(res === 'victory' ? 'check' : 'alert'), h('b', { text: App.t('rep.' + res) })); } function combatBody(r) { const d = r.data, out = []; out.push(verdict(r.role, d.outcome)); out.push(h('p', { class: 'row-sub', text: App.t('rep.arrived', { t: mil().dayTime(d.arrivedAt) }) + ' · ' + mil().reportTypeLabel(d.type) })); out.push(party(App.t('rep.attacker'), d.attacker.operator, d.attacker.town)); out.push(party(App.t('rep.defender'), d.defender.operator, d.defender.town)); const p = d.power || {}; out.push(sec(App.t('rep.power'), kv(App.t('rep.power.attack'), h('b', { class: 'mono', text: num(p.attack || 0) })), kv(App.t('rep.power.defence'), h('b', { class: 'mono', text: num(p.defence || 0) })), p.firewallLevel !== undefined ? kv(App.t('rep.power.firewall'), h('span', { class: 'mono', text: p.firewallLevel + (p.effectiveFirewall !== undefined && p.effectiveFirewall !== p.firewallLevel ? ' → ' + App.fmt.dec(p.effectiveFirewall) : '') })) : null)); out.push(sec(App.t('rep.attackerUnits'), unitsTable(d.attacker.units.sent, d.attacker.units.lost, d.attacker.units.survivors))); const groups = (d.defender.groups || []); out.push(sec(App.t('rep.defenderUnits'), groups.length ? groups.map((g) => h('div', { class: 'rep-group' }, h('p', { class: 'row-sub wrap' }, g.ownerTownId === (d.defender.town && d.defender.town.id) ? App.t('rep.group.home') : App.t('rep.group.reinf', { town: '#' + g.ownerTownId }), g.offline ? ' · ' + App.t('rep.group.offline') : ''), unitsTable(g.sent, g.lost, g.survivors))) : h('p', { class: 'empty', text: App.t('rep.noUnits') }))); if (d.loot && D.RESOURCES.some((x) => d.loot[x])) { out.push(sec(App.t('rep.loot'), mil().resChips(d.loot, true), d.cacheHidden ? h('p', { class: 'row-sub', text: App.t('rep.cache', { p: Math.round(d.cacheHidden * 100) }) }) : null)); } if (d.siege) out.push(sec(App.t('rep.siege'), h('p', { text: App.t('rep.siege.line', { name: App.buildingName(d.siege.type), a: d.siege.from, b: d.siege.to }) }))); if (d.takeover) { const t = d.takeover; const lines = []; if (t.blocked) lines.push(h('p', { class: 'reason', text: App.t('rep.takeover.blocked.' + t.blocked) })); else { lines.push(kv(App.t('rep.takeover.loyalty'), h('b', { class: 'mono', text: num(t.loyaltyBefore) + ' → ' + num(t.loyaltyAfter) + ' (−' + num(t.drop) + ')' }))); if (t.capital) lines.push(h('p', { class: 'row-sub wrap', text: App.t('rep.takeover.capital') })); } if (d.capturedTown) lines.push(h('p', { class: 'good', text: App.t('rep.takeover.captured') })); out.push(sec(App.t('rep.takeover'), lines)); } return out; } function scanBody(r) { const d = r.data, out = []; out.push(h('div', { class: 'rep-verdict ' + (d.success ? 'victory' : 'defeat') }, icon(d.success ? 'eye' : 'alert'), h('b', { text: d.success ? App.t('rep.scan.ok') : App.t('rep.scan.failed') }))); if (d.site) out.push(kv(App.t('rep.site'), mil().coordLink(d.site.x, d.site.y))); if (d.target) out.push(party(App.t('rep.defender'), d.operator, d.target)); if (d.strength) out.push(kv(App.t('rep.scan.strength'), h('span', { class: 'mono', text: App.fmt.dec(d.strength.attacker) + ' vs ' + App.fmt.dec(d.strength.defender) }))); out.push(sec(App.t('rep.scanners'), unitsTable(d.scanners && d.scanners.sent, d.scanners && d.scanners.lost, d.scanners && d.scanners.survivors))); const intel = d.intel; if (!intel) { out.push(h('p', { class: 'reason', text: App.t('rep.scan.noIntel') })); return out; } if (d.site) { // incumbent site out.push(sec(App.t('rep.intel'), kv(App.t('rep.power.defence'), h('b', { class: 'mono', text: num(intel.defence) })), intel.bonusAvailable ? h('p', { class: 'row-sub wrap', text: App.t('rep.incumbent.bonus', { n: num(intel.hardwareBonus) }) }) : h('p', { class: 'row-sub wrap', text: App.t('rep.incumbent.taken') }))); return out; } out.push(sec(App.t('rep.intel.resources'), mil().resChips(intel.resources))); out.push(sec(App.t('rep.intel.units'), !intel.online ? h('p', { class: 'reason', text: App.t('rep.intel.offline') }) : null, unitsTable(intel.units.home, {}, intel.units.home), (intel.units.stationed || []).map((g) => h('div', { class: 'rep-group' }, h('p', { class: 'row-sub wrap', text: App.t('rep.group.reinf', { town: '#' + g.ownerTownId }) }), h('p', { text: mil().unitsText(g.units) }))))); if (intel.buildings) { out.push(sec(App.t('rep.intel.buildings'), h('ul', { class: 'rep-buildings' }, intel.buildings.filter((b) => b.level > 0).map((b) => h('li', null, h('span', { text: App.buildingName(b.type) }), h('b', { class: 'mono', text: String(b.level) })))))); } if (intel.loyalty !== undefined) out.push(kv(App.t('pop.loyalty'), h('b', { class: 'mono', text: num(intel.loyalty) }))); if (intel.outgoing && intel.outgoing.length) { out.push(sec(App.t('rep.intel.outgoing'), h('ul', { class: 'rep-buildings' }, intel.outgoing.map((m) => h('li', null, h('span', { text: mil().typeLabel(m.type) }), mil().coordLink(m.to.x, m.to.y), h('small', { class: 'dimmer', text: mil().clockTime(m.arriveAt) })))))); } return out; } function detectedBody(r) { const d = r.data, out = []; out.push(h('div', { class: 'rep-verdict defeat' }, icon('eye'), h('b', { text: App.t('rep.detected.title', { town: townOf(d.target) }) }))); if (d.target) out.push(kv(App.t('rep.target'), mil().endLink(d.target))); if (d.identityRevealed && d.by) out.push(party(App.t('rep.scannedBy'), d.by.operator, d.by.town)); else out.push(h('p', { class: 'row-sub wrap', text: App.t('rep.detected.unknown') })); return out; } function incumbentBody(r) { const d = r.data, out = []; out.push(verdict('attacker', d.outcome)); out.push(kv(App.t('rep.site'), mil().coordLink(d.site.x, d.site.y))); if (d.origin) out.push(kv(App.t('rep.from'), mil().endLink(d.origin))); out.push(sec(App.t('rep.power'), kv(App.t('rep.power.attack'), h('b', { class: 'mono', text: num(d.power.attack) })), kv(App.t('rep.power.defence'), h('b', { class: 'mono', text: num(d.power.defence) })))); out.push(sec(App.t('rep.attackerUnits'), unitsTable(d.attacker.units.sent, d.attacker.units.lost, d.attacker.units.survivors))); if (d.bonus) out.push(sec(App.t('rep.incumbent.reward'), mil().resChips({ hardware: d.bonus.hardware }, true), d.bonus.researchSpeedBonus ? h('p', { class: 'row-sub', text: App.t('units.research.speed', { p: Math.round(d.bonus.researchSpeedBonus * 100) }) }) : null)); return out; } function settleBody(r, ctl) { const d = r.data, out = []; out.push(h('div', { class: 'rep-verdict victory' }, icon('flag'), h('b', { text: App.t('rep.settle.title', { name: d.town.name }) }))); out.push(kv(App.t('rep.site'), mil().coordLink(d.town.x, d.town.y))); out.push(kv(App.t('rep.settle.wild'), h('span', { text: (d.wild || []).map((w) => App.buildingName(w)).join(', ') }))); out.push(h('button', { class: 'btn primary wide', type: 'button', onclick: async () => { ctl.close(); try { await App.refreshTowns(); } catch (e) { /* the POP list may be stale; setTown below copes */ } App.setTown(d.town.id); App.go('pop'); } }, icon('pop'), App.t('rep.settle.open'))); out.push(h('p', { class: 'row-sub wrap', text: App.t('rep.settle.hint') })); return out; } function reinforceBody(r) { const d = r.data, out = []; out.push(kv(App.t('rep.from'), h('span', null, h('b', { text: nameOf(d.operator) }), mil().endLink(d.from)))); out.push(kv(App.t('rep.host'), h('span', null, h('b', { text: nameOf(d.hostOperator) }), mil().endLink(d.host)))); out.push(sec(App.t('rep.units'), h('p', { text: mil().unitsText(d.units) }))); return out; } function failedBody(r) { const d = r.data, out = []; out.push(h('div', { class: 'rep-verdict defeat' }, icon('alert'), h('b', { text: App.t('rep.failed.title', { type: mil().typeLabel(d.movementType) }) }))); out.push(h('p', { text: reasonText(d.reason) })); if (d.to) out.push(kv(App.t('rep.target'), mil().coordLink(d.to.x, d.to.y))); out.push(sec(App.t('rep.units'), h('p', { text: mil().unitsText(d.units) }), h('p', { class: 'row-sub wrap', text: App.t('rep.failed.home') }))); return out; } function genericBody(r) { return [h('pre', { class: 'rep-raw', text: JSON.stringify(r.summary || {}, null, 1).slice(0, 1500) })]; } function bodyFor(r, ctl) { switch (r.type) { case 'raid': case 'attack': case 'takeover': return combatBody(r); case 'scan': return scanBody(r); case 'scan_detected': return detectedBody(r); case 'incumbent': return incumbentBody(r); case 'settle': return settleBody(r, ctl); case 'reinforce': return reinforceBody(r); case 'movement_failed': return failedBody(r); default: return genericBody(r); } } // ------------------------------------------------------------------ detail sheet async function openReport(id) { const body = h('div', { class: 'detail rep-detail' }, h('div', { class: 'boot' }, h('div', { class: 'spinner', 'aria-hidden': 'true' }))); const ctl = openSheet({ title: App.t('screen.reports'), icon: 'report', closeLabel: App.t('common.close'), body }); try { const res = await App.api.get('/reports/' + id); const r = res.report; ctl.setTitle(mil().reportTypeLabel(r.type)); clear(body); body.appendChild(h('p', { class: 'row-sub', text: mil().dayTime(r.createdAt) + ' · ' + App.t('rep.role.' + r.role) + ' · ' + App.t('rep.expires', { t: mil().dayTime(r.expiresAt) }) })); let parts; try { parts = bodyFor(r, ctl); } catch (e) { parts = genericBody(r); } for (const p of parts.flat(2)) if (p) body.appendChild(p); if (!r.read) { App.api.post('/reports/' + id + '/read', {}).then(() => { const it = items.find((x) => x.id === id); if (it) it.read = true; if (visible) draw(); App.refreshReportsUnread().catch(() => {}); }).catch(() => {}); } } catch (e) { clear(body); body.appendChild(h('p', { class: 'form-error', text: App.errMsg(e) })); } } // ------------------------------------------------------------------ screen App.registerScreen({ id: 'reports', titleKey: 'screen.reports', navKey: 'nav.reports', icon: 'report', navGroup: 'more', hud: false, render(container) { box = container; listEl = h('ul', { class: 'alert-list' }); statusEl = h('p', { class: 'form-error', role: 'alert', hidden: true }); moreBtn = h('button', { class: 'btn wide', type: 'button', hidden: true, onclick: () => load(true) }, App.t('rep.older')); box.appendChild(h('div', { class: 'page' }, h('div', { class: 'card-head page-head' }, h('h1', { text: App.t('screen.reports') }), h('div', { class: 'head-actions' }, h('button', { class: 'btn small', type: 'button', onclick: markAll }, icon('check'), App.t('rep.markAll')), h('button', { class: 'icon-btn', type: 'button', 'aria-label': App.t('common.retry'), onclick: () => load(false) }, icon('refresh')))), statusEl, listEl, moreBtn)); App.on('session', () => { items = []; next = null; }); App.on('report', () => { if (visible) load(false); }); }, onShow(params) { visible = true; box.querySelector('h1').textContent = App.t('screen.reports'); moreBtn.textContent = App.t('rep.older'); draw(); load(false); if (params && params.open) openReport(Number(params.open)); if (offAgo) offAgo(); offAgo = App.onTick(() => { const now = App.now(); for (const el of listEl.querySelectorAll('.when')) { const s = App.fmt.ago(now - Number(el.dataset.at)); if (el.textContent !== s) el.textContent = s; } }); }, onHide() { visible = false; if (offAgo) offAgo(); offAgo = null; }, }); }());