Upstream/public/js/screens/rankings.js
jvanvik b18645dca1 Phase 2: client military/social screens, WebSocket push, trade + endgame, review findings pending
Built: units/movements/reports/send/expansion screens, alliance/messages/rankings screens, ws client + server /ws push
(docs/PROTOCOL.md), Internet exchange trade, backbone endgame + world end, shared reports, buildDone relative levels.
Tested: npm test 175/175. Two adversarial reviews found issues (trade-leg recall exploit, __proto__ via validate.js,
rankings DoS, static headers, ...): fixes follow in the next commit.
NOT done: bots/balance, e2e suite, real-device checks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
2026-10-11 19:05:03 +02:00

146 lines
8.6 KiB
JavaScript

'use strict';
/*
* Rankings: operators by footprint, alliances by summed footprint, battle points (attack / defense / total). The server
* returns `you` (your own rank, even when it is not on the loaded page) and a rank-offset cursor (`next`). Rows of your own
* operator or alliance are highlighted. Names are set with textContent only.
*/
(function () {
const { h, clear, icon, openSheet } = UI;
const Soc = UpstreamSocial;
const PAGE = 50;
const X = () => window.SocUI;
let box, listBox, tabsEl, subEl;
let tab = 'operators', by = 'attack';
const sets = {};
let gen = 0;
const keyOf = () => (tab === 'battles' ? 'battles:' + by : tab);
const cur = () => sets[keyOf()] || (sets[keyOf()] = { items: [], next: null, total: 0, you: null, available: true, loaded: false, loading: false, err: null });
const path = () => '/rankings/' + tab;
async function load(more) {
const key = keyOf(), s = cur();
if (s.loading) return;
s.loading = true; s.err = null;
const my = gen;
if (key === keyOf()) draw();
try {
const q = { limit: PAGE };
if (more && s.next !== null) q.after = s.next;
if (tab === 'battles') q.by = by;
const r = await App.api.get(path(), q);
s.items = more ? Soc.mergeRanks(s.items, r.items) : r.items;
s.next = r.next; s.total = r.total; s.you = r.you; s.loaded = true;
if (typeof r.available === 'boolean') s.available = r.available;
} catch (e) { s.err = e; } finally {
s.loading = false;
if (my === gen && key === keyOf()) draw();
}
}
const num = (n) => App.fmt.num(Math.round(n));
// One row: rank, name (+ tag), the value columns. `main` is the metric shown big, `sub` the small line.
function row(r) {
const you = Soc.isYou(tab, r, youRow());
let name, sub, val, tap;
if (tab === 'operators') {
name = r.name; sub = (r.allianceTag ? '[' + r.allianceTag + '] ' : '') + App.t('soc.rank.pops', { n: r.towns }); val = App.fmt.dec(r.footprint);
tap = () => openOperator(r);
} else if (tab === 'alliances') {
name = r.name; sub = '[' + r.tag + '] ' + App.t('soc.rank.members', { n: r.members }) + ' · ' + App.t('soc.rank.pops', { n: r.towns }); val = App.fmt.dec(r.footprint);
tap = () => X().openProfile(r.allianceId, { name: r.name });
} else {
name = r.name; sub = (r.allianceTag ? '[' + r.allianceTag + '] ' : '') + App.t('soc.rank.att', { n: num(r.attackPoints) }) + ' · ' + App.t('soc.rank.def', { n: num(r.defensePoints) });
val = num(Soc.battlePoints(r, by));
tap = () => openOperator(r);
}
return h('li', null, h('button', { class: 'soc-rank' + (you ? ' you' : ''), type: 'button', onclick: tap, 'aria-current': you ? 'true' : null },
h('span', { class: 'soc-rk mono', text: '#' + r.rank }),
h('span', { class: 'row-main' }, h('span', { class: 'row-title' }, h('b', { text: name }), you ? h('span', { class: 'pill accent', text: App.t('soc.you') }) : null), h('span', { class: 'row-sub', text: sub })),
h('span', { class: 'soc-val mono', text: val })));
}
const youRow = () => cur().you;
function openOperator(r) {
const me = X().myOperatorId();
const kv = (k, v) => h('div', { class: 'kv' }, h('span', { class: 'k', text: k }), h('span', { class: 'v', text: v }));
const body = h('div', { class: 'detail' },
h('div', { class: 'pill-row' }, h('span', { class: 'pill soc-tag mono', text: '#' + r.rank }), r.allianceTag ? X().tagPill(r.allianceTag) : null, r.asNumber ? h('span', { class: 'pill mono', text: UpstreamI18n.fmtAs(r.asNumber) }) : null),
h('div', null,
r.footprint !== undefined ? kv(App.t('soc.footprint'), App.fmt.dec(r.footprint)) : null,
r.towns !== undefined ? kv(App.t('soc.member.pops'), String(r.towns)) : null,
r.attackPoints !== undefined ? kv(App.t('soc.rank.attack'), num(r.attackPoints)) : null,
r.defensePoints !== undefined ? kv(App.t('soc.rank.defense'), num(r.defensePoints)) : null));
const sheet = openSheet({ title: r.name, icon: 'users', closeLabel: App.t('common.close'), body });
if (r.allianceId) body.appendChild(h('button', { class: 'btn wide', type: 'button', onclick: () => { sheet.close(); X().openProfile(r.allianceId, { name: r.allianceTag }); } }, icon('users'), App.t('soc.rank.viewAlliance')));
if (r.operatorId !== me) body.appendChild(h('button', { class: 'btn primary wide', type: 'button', onclick: () => { sheet.close(); X().openCompose({ operator: { operatorId: r.operatorId, name: r.name } }); } }, icon('mail'), App.t('soc.member.message')));
}
function draw() {
if (!listBox) return;
clear(listBox);
for (const b of tabsEl.children) b.setAttribute('aria-pressed', String(b.dataset.tab === tab));
subEl.hidden = tab !== 'battles';
for (const b of subEl.children) b.setAttribute('aria-pressed', String(b.dataset.by === by));
const s = cur();
if (s.err) { listBox.appendChild(X().errorBox(s.err, () => load(false))); return; }
if (!s.loaded) { listBox.appendChild(X().loading()); return; }
if (tab === 'battles' && !s.available) { listBox.appendChild(h('p', { class: 'empty pad', text: App.t('soc.rank.unavailable') })); return; }
if (s.you) {
const y = s.you;
const inList = s.items.some((r) => r.rank === y.rank);
listBox.appendChild(h('div', { class: 'card soc-card soc-yourank' },
h('span', { class: 'soc-yr-n mono', text: '#' + y.rank }),
h('span', { class: 'row-main' }, h('b', { text: App.t('soc.rank.yours') }),
h('span', { class: 'row-sub', text: App.t('soc.rank.of', { n: s.total }) + (tab === 'battles' ? ' · ' + num(Soc.battlePoints(y, by)) : ' · ' + App.fmt.dec(y.footprint)) })),
inList ? null : h('span', { class: 'pill dim', text: App.t('soc.rank.notShown') })));
} else if (tab === 'alliances') {
listBox.appendChild(h('p', { class: 'field-hint soc-hint', text: App.t('soc.rank.noAlliance') }));
} else if (tab === 'battles') {
listBox.appendChild(h('p', { class: 'field-hint soc-hint', text: App.t('soc.rank.noPoints') }));
}
if (!s.items.length) { listBox.appendChild(h('p', { class: 'empty pad', text: App.t(tab === 'battles' ? 'soc.rank.noBattles' : 'soc.rank.empty') })); return; }
const ol = h('ol', { class: 'soc-ranklist' });
for (const r of s.items) ol.appendChild(row(r));
listBox.appendChild(ol);
if (s.next !== null) listBox.appendChild(h('button', { class: 'btn small wide', type: 'button', disabled: s.loading, text: App.t('soc.more') + ' (' + s.items.length + '/' + s.total + ')', onclick: () => load(true) }));
}
function setTab(t) {
tab = t;
draw();
if (!cur().loaded) load(false);
}
App.registerScreen({
id: 'rankings', titleKey: 'screen.rankings', navKey: 'nav.rankings', icon: 'trophy', hud: false, navGroup: 'more',
render(container) {
box = container;
const tabs = [['operators', 'soc.rank.tab.operators'], ['alliances', 'soc.rank.tab.alliances'], ['battles', 'soc.rank.tab.battles']];
tabsEl = h('div', { class: 'seg soc-tabs', role: 'group', 'aria-label': App.t('soc.tab.label') },
tabs.map(([id, key]) => h('button', { class: 'seg-btn', type: 'button', dataset: { tab: id }, 'aria-pressed': String(tab === id), text: App.t(key), onclick: () => setTab(id) })));
subEl = h('div', { class: 'seg soc-subtabs', role: 'group', 'aria-label': App.t('soc.rank.by'), hidden: true },
['attack', 'defense', 'total'].map((k) => h('button', { class: 'chip-btn', type: 'button', dataset: { by: k }, 'aria-pressed': String(by === k), text: App.t('soc.rank.by.' + k), onclick: () => { by = k; draw(); if (!cur().loaded) load(false); } })));
listBox = h('div', { class: 'soc-listbox' });
box.appendChild(h('div', { class: 'page soc-page' },
h('div', { class: 'card-head page-head' }, h('h1', { id: 'soc-rank-h', text: App.t('screen.rankings') }),
h('button', { class: 'icon-btn', type: 'button', 'aria-label': App.t('common.retry'), onclick: () => { delete sets[keyOf()]; load(false); } }, icon('refresh'))),
tabsEl, subEl, listBox));
App.on('session', () => { for (const k of Object.keys(sets)) delete sets[k]; });
},
onShow() {
gen++;
box.querySelector('#soc-rank-h').textContent = App.t('screen.rankings');
const labels = { operators: 'soc.rank.tab.operators', alliances: 'soc.rank.tab.alliances', battles: 'soc.rank.tab.battles' };
for (const b of tabsEl.children) b.textContent = App.t(labels[b.dataset.tab]);
for (const b of subEl.children) b.textContent = App.t('soc.rank.by.' + b.dataset.by);
draw();
load(false); // rankings move: always refresh the first page when the screen opens
},
onHide() { gen++; },
});
}());