65 lines
3.3 KiB
JavaScript
65 lines
3.3 KiB
JavaScript
|
|
/* 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) {
|
||
|
|
const rows = await scores.top(10); // [{ key, name, score, level, time }] best first
|
||
|
|
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 [];
|
||
|
|
// fetch spare rows: filtered names must not leave the list short
|
||
|
|
const q = new URLSearchParams({ orderBy: '"score"', limitToLast: String(n + 15) });
|
||
|
|
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()) || {};
|
||
|
|
return Object.entries(data)
|
||
|
|
.map(([key, v]) => Object.assign({ key }, v))
|
||
|
|
.filter(v => typeof v.name === 'string' && typeof v.score === 'number' && !Kit.badName(v.name))
|
||
|
|
.sort((a, b) => b.score - a.score || (a.time || 0) - (b.time || 0))
|
||
|
|
.slice(0, n);
|
||
|
|
},
|
||
|
|
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);
|