hughe update
This commit is contained in:
@@ -3,7 +3,8 @@
|
||||
|
||||
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 rows = await scores.top(10); // [{ key, name, score, level, time }] best first,
|
||||
// one row per player (their best run)
|
||||
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
|
||||
@@ -36,16 +37,22 @@
|
||||
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) });
|
||||
// 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) });
|
||||
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)
|
||||
const rows = 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);
|
||||
.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);
|
||||
},
|
||||
async submit({ name, score, level = 1 }) {
|
||||
name = Kit.cleanName(name);
|
||||
|
||||
Reference in New Issue
Block a user