- net.js: REST + one WebSocket with reconnect; single-flight refresh shared across tabs; ?slot=N - lobby.js / netfight.js: server owns presence, challenges and the fight; client shows snapshots - game.js: every workshop change goes through RC.Eco via act() (server-checked online); results, rewards and achievements come from the server (match.end) - ranking/live/cloudsave on the new REST endpoints; online tournament removed until P2-6 BREAKING CHANGE: needs the new server (protocol v1); old online saves are not migrated.
43 lines
1.8 KiB
JavaScript
43 lines
1.8 KiB
JavaScript
/* Robot Clash - online ranking. The server computes it from the online saves (fame only changes through
|
|
server-booked fights), so the client only reads: GET /api/ranks, polled while a board is open.
|
|
DOM-free; RC.Ranking.rank is a pure function (Node-testable).
|
|
|
|
const rank = RC.Ranking(net);
|
|
if (rank.enabled) {
|
|
const sub = rank.watch(rows => draw(rows)); // { close() } rows best first, refreshed every 15 s
|
|
const rows = await rank.once(20);
|
|
}
|
|
RC.Ranking.rank(data, n) // [{uid,name,fame,flag}] or { key: {...} } -> sorted array, bad names out, capped */
|
|
(function (root) {
|
|
'use strict';
|
|
const RC = (root.RC = root.RC || {});
|
|
const Kit = root.Kit || {};
|
|
const bad = s => (Kit.badName ? Kit.badName(s) : false);
|
|
const POLL = 15000;
|
|
|
|
RC.Ranking = function (net) {
|
|
const enabled = !!(net && net.enabled);
|
|
const fetchRows = async n => RC.Ranking.rank(await net.api('GET', '/api/ranks?n=' + (n || 20)), n);
|
|
return {
|
|
enabled,
|
|
watch(cb, n) {
|
|
if (!enabled) { cb([]); return { close() {} }; }
|
|
let alive = true;
|
|
const tick = () => fetchRows(n).then(rows => { if (alive) cb(rows); });
|
|
tick();
|
|
const timer = setInterval(tick, POLL);
|
|
return { close() { alive = false; clearInterval(timer); } };
|
|
},
|
|
once: n => (enabled ? fetchRows(n) : Promise.resolve([])),
|
|
};
|
|
};
|
|
|
|
RC.Ranking.rank = function (data, n = 20) {
|
|
const list = Array.isArray(data) ? data.map(v => Object.assign({ key: v.uid }, v))
|
|
: Object.entries(data || {}).map(([key, v]) => Object.assign({ key }, v));
|
|
const rows = list.filter(v => v && typeof v.name === 'string' && typeof v.fame === 'number' && !bad(v.name));
|
|
rows.sort((a, b) => b.fame - a.fame || (b.ts || 0) - (a.ts || 0));
|
|
return rows.slice(0, n);
|
|
};
|
|
})(typeof window !== 'undefined' ? window : globalThis);
|