/* Robot Clash - cloud save for registered accounts (no DOM). Needs net.js. The whole save still lives in localStorage (fast cache); for a real e-mail account it is also mirrored to Firebase RTDB at saves/ so it survives a device change and is harder to fake than localStorage. Honest limit: the owner can still write their own node - rules only cap ranges. No hard copy protection. const cloud = RC.CloudSave(net); cloud.pull(uid) -> the cloud save object, or null (best effort, never throws) cloud.push(uid, save) -> debounced write (~2.5 s after the last change) cloud.flush(uid, save) -> write now (fight result, leaving garage, logout, unload) RC.CloudSave.newer(cloud, local) -> true if the cloud copy should win (by ts) */ (function (root) { 'use strict'; const RC = (root.RC = root.RC || {}); // cloud wins when it is at least as new as local (shared copy is the source once it exists) function newer(cloud, local) { if (!cloud || typeof cloud !== 'object') return false; const ct = +cloud.ts || 0, lt = (local && +local.ts) || 0; return ct >= lt; } RC.CloudSave = function (net) { let timer = null, sending = false, pending = null, pendingUid = null; const clone = s => { try { return JSON.parse(JSON.stringify(s)); } catch (e) { return null; } }; async function write(uid, save) { sending = true; try { await net.db.put('saves/' + uid, save); } catch (e) { /* best effort */ } sending = false; if (pending) { const s = pending, u = pendingUid; pending = null; pendingUid = null; write(u, s); } } function doPush(uid, save) { if (!net || !net.enabled || !uid || !save) return; if (sending) { pending = save; pendingUid = uid; return; } // one request at a time; keep only the latest write(uid, save); } return { newer, async pull(uid) { if (!net || !net.enabled || !uid) return null; try { return await net.db.get('saves/' + uid); } catch (e) { return null; } }, push(uid, save) { clearTimeout(timer); const snap = clone(save); timer = setTimeout(() => doPush(uid, snap), 2500); }, flush(uid, save) { clearTimeout(timer); doPush(uid, clone(save)); }, }; }; RC.CloudSave.newer = newer; if (typeof module !== 'undefined' && module.exports) module.exports = RC; })(typeof window !== 'undefined' ? window : globalThis);