Files
speedcode/public/stats.js
T
2026-04-04 11:32:43 -05:00

59 lines
1.7 KiB
JavaScript

const STORAGE_KEY = "levelup-typing-save-v1";
function readSave() {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return null;
try {
return JSON.parse(raw);
} catch (error) {
return null;
}
}
function setText(id, value) {
const el = document.getElementById(id);
if (el) el.textContent = value;
}
function renderHistory(recentRounds) {
const list = document.getElementById("historyList");
if (!list) return;
list.innerHTML = "";
if (!recentRounds || recentRounds.length === 0) {
const item = document.createElement("li");
item.className = "history-item";
item.textContent = "no rounds played yet... complete a run to see your history!";
list.appendChild(item);
return;
}
recentRounds.forEach((round) => {
const item = document.createElement("li");
item.className = "history-item";
const date = new Date(round.stamp || Date.now());
const when = `${date.toLocaleDateString()} ${date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })}`;
item.textContent = `${when} | ${round.wpm || 0} wpm / ${round.accuracy || 0}% acc / combo ${round.bestCombo || 0} / +${round.xp || 0} xp / ${(round.difficulty || "normal").toLowerCase()}`;
list.appendChild(item);
});
}
function boot() {
const save = readSave() || {};
const stats = save.stats || {};
setText("sTotalRounds", String(stats.totalRounds || 0));
setText("sBestWpm", String(stats.bestWpm || 0));
setText("sBestAcc", `${Math.round(stats.bestAccuracy || 0)}%`);
setText("sAvgWpm", String(Math.round(stats.averageWpm || 0)));
setText("sAvgAcc", `${Math.round(stats.averageAccuracy || 0)}%`);
renderHistory(Array.isArray(save.recentRounds) ? save.recentRounds : []);
}
boot();