Files
Robot-Clash/game/live.js
2026-11-17 14:17:50 +01:00

86 lines
3.9 KiB
JavaScript

/* Robot Clash - live settings from the backend (Firebase node /live).
The pure part has no DOM and no network, so the tests can run it in Node.
RC.Live.parse(data, now, build) -> {
motd, // { de, en } or null
event, // the running event or null (outside its window it counts as none)
onlineOff, note, // kill switch + why
needsReload, // this copy is older than build.min
locked(kind, id), // true when the admin has not released this part/arena/class yet
}
RC.Live.load(net, onChange) reads /live once, then keeps listening, and remembers the last
answer in localStorage so the game still works offline.
Everything is best effort: no config, broken JSON or no network simply means "nothing special
is running" - the game must never wait for or depend on this. */
(function (root) {
'use strict';
const RC = (root.RC = root.RC || {});
const KEY = 'robotclash.live';
const obj = v => (v && typeof v === 'object' && !Array.isArray(v) ? v : null);
const list = v => (Array.isArray(v) ? v.filter(x => typeof x === 'string') : null);
const num = (v, d) => (typeof v === 'number' && isFinite(v) ? v : d);
// An empty or missing list means "everything is free" - that is the behaviour without a backend,
// so a broken config can never lock players out of their own parts.
function makeLocked(unlock) {
const u = obj(unlock) || {};
const sets = { parts: list(u.parts), arenas: list(u.arenas), classes: list(u.classes) };
return (kind, id) => {
const only = sets[kind];
return !!(only && only.length && only.indexOf(id) < 0);
};
}
function parse(data, now, build) {
const d = obj(data) || {};
const t = num(now, Date.now());
const ev = obj(d.event);
const inWindow = ev && (!num(ev.from, 0) || t >= ev.from) && (!num(ev.to, 0) || t <= ev.to);
const flags = obj(d.flags) || {};
const b = obj(d.build) || {};
const min = typeof b.min === 'string' ? b.min : '';
return {
motd: obj(d.motd),
event: inWindow ? {
id: String(ev.id || 'event'),
name: obj(ev.name) || null,
from: num(ev.from, 0), to: num(ev.to, 0),
type: String(ev.type || 'plain'),
cls: typeof ev.cls === 'string' ? ev.cls : null,
arena: typeof ev.arena === 'string' ? ev.arena : null,
prizeMul: Math.max(0.1, Math.min(5, num(ev.prizeMul, 1))),
tournament: !!ev.tournament,
} : null,
onlineOff: !!flags.onlineOff,
note: obj(flags.note),
tournament: !!flags.tournament || !!(ev && ev.tournament && inWindow), // tournament open?
needsReload: !!(min && typeof build === 'string' && build && build < min),
locked: makeLocked(d.unlock),
};
}
// last known config, so a fresh page load already knows about an event before the network answers
function cached() {
try { return JSON.parse(root.localStorage.getItem(KEY) || 'null'); } catch (e) { return null; }
}
function remember(data) {
try { root.localStorage.setItem(KEY, JSON.stringify(data)); } catch (e) { /* private mode: ignore */ }
}
function load(net, onChange) {
const build = (root.RC_BUILD || root.window && root.window.RC_BUILD) || '';
const fire = data => { if (data) remember(data); onChange(parse(data, Date.now(), build)); };
fire(cached()); // show something immediately
if (!net || !net.enabled) return { close() {} };
net.db.get('live').then(d => { if (d) fire(d); }, () => {});
let sub = null;
try { sub = net.db.stream('live', d => { if (d) fire(d); }); } catch (e) { sub = null; }
return { close() { if (sub) sub.close(); } };
}
RC.Live = { parse, load, KEY };
if (typeof module !== 'undefined' && module.exports) module.exports = RC;
})(typeof window !== 'undefined' ? window : globalThis);