2026-11-13 01:02:55 +01:00
|
|
|
/* Robot Clash - player accounts. v1: local profiles on this device (name + password, stored as a
|
|
|
|
|
salted hash in localStorage - keeps siblings apart, no real security). Later the same interface
|
2026-11-19 02:11:13 +01:00
|
|
|
gets a server backend (server/auth.js) without changing game.js.
|
2026-11-13 01:02:55 +01:00
|
|
|
|
|
|
|
|
const acc = RC.Accounts('robotclash');
|
|
|
|
|
acc.list() -> [{ id, name }]
|
|
|
|
|
await acc.register(name, pass) -> { ok, error: 'name_short' | 'name_bad' | 'name_taken' | 'pass_short' }
|
|
|
|
|
await acc.login(name, pass, remember) -> { ok, error: 'unknown' | 'wrong_pass' }
|
|
|
|
|
acc.guest() play without an account (own save slot)
|
|
|
|
|
acc.current { id, name, guest } or null
|
|
|
|
|
acc.resume() remembered login from the last session (true/false)
|
|
|
|
|
acc.logout() / acc.remove(id)
|
|
|
|
|
acc.saveKey() localStorage key of the current player's save
|
|
|
|
|
Needs kit-core.js (Kit.store, Kit.cleanName, Kit.badName). */
|
|
|
|
|
(function (root) {
|
|
|
|
|
'use strict';
|
|
|
|
|
const RC = (root.RC = root.RC || {});
|
|
|
|
|
|
|
|
|
|
// SHA-256 where available (secure contexts incl. file://), FNV-1a fallback
|
|
|
|
|
async function hash(text) {
|
|
|
|
|
try {
|
|
|
|
|
if (root.crypto && root.crypto.subtle) {
|
|
|
|
|
const buf = await root.crypto.subtle.digest('SHA-256', new TextEncoder().encode(text));
|
|
|
|
|
return Array.from(new Uint8Array(buf)).map(b => b.toString(16).padStart(2, '0')).join('');
|
|
|
|
|
}
|
|
|
|
|
} catch (e) { /* fall through */ }
|
|
|
|
|
let h = 0x811c9dc5;
|
|
|
|
|
for (let i = 0; i < text.length; i++) { h ^= text.charCodeAt(i); h = Math.imul(h, 0x01000193) >>> 0; }
|
|
|
|
|
return 'f' + h.toString(16);
|
|
|
|
|
}
|
|
|
|
|
const rid = () => Math.random().toString(36).slice(2, 10);
|
|
|
|
|
|
|
|
|
|
RC.Accounts = function (prefix) {
|
|
|
|
|
const st = Kit.store(prefix);
|
|
|
|
|
const all = () => st.get('profiles', []) || [];
|
|
|
|
|
const put = list => st.set('profiles', list);
|
|
|
|
|
const find = name => all().find(p => p.name.toLowerCase() === String(name).trim().toLowerCase());
|
|
|
|
|
const api = {
|
|
|
|
|
current: null,
|
|
|
|
|
list: () => all().map(p => ({ id: p.id, name: p.name })),
|
|
|
|
|
async register(name, pass) {
|
|
|
|
|
name = Kit.cleanName(name).slice(0, 18);
|
|
|
|
|
if (name.length < 3) return { ok: false, error: 'name_short' };
|
|
|
|
|
if (Kit.badName(name)) return { ok: false, error: 'name_bad' };
|
|
|
|
|
if (find(name)) return { ok: false, error: 'name_taken' };
|
|
|
|
|
if (String(pass || '').length < 4) return { ok: false, error: 'pass_short' };
|
|
|
|
|
const salt = rid() + rid();
|
|
|
|
|
const p = { id: 'p' + rid(), name, salt, hash: await hash(salt + pass), created: Date.now() };
|
|
|
|
|
put(all().concat(p));
|
|
|
|
|
api.current = { id: p.id, name: p.name, guest: false };
|
|
|
|
|
st.set('session', p.id);
|
|
|
|
|
return { ok: true };
|
|
|
|
|
},
|
|
|
|
|
async login(name, pass, remember) {
|
|
|
|
|
const p = find(name);
|
|
|
|
|
if (!p) return { ok: false, error: 'unknown' };
|
|
|
|
|
if (await hash(p.salt + pass) !== p.hash) return { ok: false, error: 'wrong_pass' };
|
|
|
|
|
api.current = { id: p.id, name: p.name, guest: false };
|
|
|
|
|
st.set('session', remember ? p.id : null);
|
|
|
|
|
return { ok: true };
|
|
|
|
|
},
|
|
|
|
|
guest() { api.current = { id: 'guest', name: '', guest: true }; st.set('session', null); },
|
|
|
|
|
resume() {
|
|
|
|
|
const id = st.get('session', null), p = id && all().find(q => q.id === id);
|
|
|
|
|
if (!p) return false;
|
|
|
|
|
api.current = { id: p.id, name: p.name, guest: false };
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
logout() { api.current = null; st.set('session', null); },
|
|
|
|
|
remove(id) {
|
|
|
|
|
put(all().filter(p => p.id !== id));
|
|
|
|
|
try { localStorage.removeItem(prefix + '.save.' + id); localStorage.removeItem(prefix + '.save.' + id + '.bak'); } catch (e) { /* ignore */ }
|
|
|
|
|
if (api.current && api.current.id === id) api.logout();
|
|
|
|
|
},
|
|
|
|
|
// the guest keeps the save of versions before accounts existed
|
|
|
|
|
saveKey: () => (!api.current || api.current.guest ? prefix + '.save' : prefix + '.save.' + api.current.id),
|
|
|
|
|
};
|
|
|
|
|
return api;
|
|
|
|
|
};
|
|
|
|
|
})(window);
|