Files
Robot-Clash/game/kit/kit-scores.js

72 lines
3.7 KiB
JavaScript
Raw Normal View History

2026-11-13 00:41:54 +01:00
/* Kit scores (v1) - online highscores via Firebase Realtime Database REST (no SDK, no login).
From _Tools\Library\web\kit. Rules to install: template docs/firebase/database.rules.json.
const scores = Kit.scores({ url: 'https://<project>-default-rtdb.europe-west1.firebasedatabase.app' });
if (scores.enabled) {
2026-11-15 17:08:11 +01:00
const rows = await scores.top(10); // [{ key, name, score, level, time }] best first,
// one row per player (their best run)
2026-11-13 00:41:54 +01:00
const key = await scores.submit({ name, score, level }); // key of the new entry or null
}
Kit.cleanName(s) -> trimmed, max 20 chars, no control chars / angle brackets
Kit.badName(s) -> true for slurs/insults (also l33t spelling); block on entry, hide in lists
Without a url (or with the placeholder) scores.enabled is false and both calls resolve to
[] / null - the game runs offline. The rules only check format and range: anyone can post a
fake score by hand. Real protection needs a server check (Cloud Function). */
(function (root) {
'use strict';
const Kit = (root.Kit = root.Kit || {});
const BAD_WORDS = ['nigg', 'niga', 'neger', 'negro', 'kike', 'fag', 'cunt', 'fuck', 'fick', 'hitler', 'nazi',
'siegheil', 'kkk', 'whore', 'hure', 'fotze', 'wichs', 'schwuchtel', 'spast', 'retard', 'slut', 'bitch',
'kanake', 'zigeuner', 'judensau'];
Kit.cleanName = s => String(s || '').replace(/[\u0000-\u001f<>]/g, '').trim().slice(0, 20);
Kit.badName = s => {
const norm = String(s || '').toLowerCase()
.replace(/[1!|]/g, 'i').replace(/3/g, 'e').replace(/0/g, 'o').replace(/[4@]/g, 'a')
.replace(/[5$]/g, 's').replace(/7/g, 't').replace(/ph/g, 'f')
.replace(/[^a-zäöüß]/g, '');
return BAD_WORDS.some(w => norm.includes(w));
};
Kit.scores = (opt = {}) => {
const url = (opt.url || '').replace(/\/+$/, '');
const path = opt.path || 'highscores';
const enabled = /^https:\/\/.+firebase(io|database)\.(com|app)$/.test(url);
return {
enabled,
async top(n = 10) {
if (!enabled) return [];
2026-11-15 17:08:11 +01:00
// fetch spare rows: filtered names and repeat entries by the same player must not leave the list short
const q = new URLSearchParams({ orderBy: '"score"', limitToLast: String(n * 4 + 25) });
2026-11-13 00:41:54 +01:00
const res = await fetch(`${url}/${path}.json?${q}`, { cache: 'no-store' });
if (!res.ok) throw new Error('scores ' + res.status);
const data = (await res.json()) || {};
2026-11-15 17:08:11 +01:00
const rows = Object.entries(data)
2026-11-13 00:41:54 +01:00
.map(([key, v]) => Object.assign({ key }, v))
.filter(v => typeof v.name === 'string' && typeof v.score === 'number' && !Kit.badName(v.name))
2026-11-15 17:08:11 +01:00
.sort((a, b) => b.score - a.score || (a.time || 0) - (b.time || 0));
// every player appears once, with their best run (each submit writes its own row)
const best = new Map();
for (const r of rows) {
const who = r.name.trim().toLowerCase();
if (!best.has(who)) best.set(who, r);
}
return Array.from(best.values()).slice(0, n);
2026-11-13 00:41:54 +01:00
},
async submit({ name, score, level = 1 }) {
name = Kit.cleanName(name);
if (!enabled || !name || !(score > 0) || Kit.badName(name)) return null;
try {
const res = await fetch(`${url}/${path}.json`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, score: Math.floor(score), level: Math.floor(level), time: { '.sv': 'timestamp' } }),
});
return res.ok ? (await res.json()).name : null;
} catch (e) { return null; }
},
};
};
})(typeof window !== 'undefined' ? window : globalThis);