-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
47 lines (43 loc) · 1.82 KB
/
Copy pathscript.js
File metadata and controls
47 lines (43 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// script.js — live daily leaderboard for runeca.st
const API_BASE = 'https://runecast.twkr.io';
const MEDALS = { 1: '🥇', 2: '🥈', 3: '🥉' };
export function escapeHtml(s) {
return String(s)
.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>')
.replaceAll('"', '"').replaceAll("'", ''');
}
export function renderLeaderboardRows(entries) {
return entries.map((e, i) => {
const rankCell = MEDALS[e.rank] || String(e.rank);
const top = i === 0 ? ' top' : '';
return `<div class="lb-row${top}">` +
`<div class="lb-rank">${escapeHtml(rankCell)}</div>` +
`<div class="lb-name">${escapeHtml(e.username)}</div>` +
`<div class="lb-score">${Number(e.score)}</div>` +
`</div>`;
}).join('');
}
async function loadDailyLeaderboard() {
const box = document.getElementById('daily-lb-rows');
if (!box) return;
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
try {
const res = await fetch(`${API_BASE}/api/daily/leaderboard?limit=6`, {
headers: { Accept: 'application/json' },
signal: controller.signal,
});
clearTimeout(timeoutId);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const entries = await res.json();
if (!Array.isArray(entries) || entries.length === 0) throw new Error('empty');
box.innerHTML = renderLeaderboardRows(entries);
} catch (err) {
// Graceful fallback — never show a broken widget.
box.innerHTML = `<div class="lb-row lb-fallback"><div class="lb-name">Today's board fills up as people play — <a href="${API_BASE}">be the first ▸</a></div></div>`;
}
}
// Only run in the browser (the module is also imported by the node test).
if (typeof document !== 'undefined') {
document.addEventListener('DOMContentLoaded', loadDailyLeaderboard);
}