Files
speedcode/public/stats.js
T
2026-04-04 12:28:36 -05:00

47 lines
1.9 KiB
JavaScript

const STORAGE_KEY = "levelup-typing-save-v1";
const q = (id) => document.getElementById(id);
const set_text = (id, value) => {
const el = q(id);
if (el) el.textContent = value;
};
const read_save = () => {
try {
return JSON.parse(localStorage.getItem(STORAGE_KEY) || "null");
} catch {
return null;
}
};
function render_history(recent_rounds) {
const list = q("historyList");
if (!list) return;
list.innerHTML = "";
if (!recent_rounds?.length) {
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;
}
recent_rounds.forEach((round) => {
const item = document.createElement("li");
const date = new Date(round.stamp || Date.now());
const when = `${date.toLocaleDateString()} ${date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })}`;
const best_combo = round.best_combo ?? round.bestCombo ?? 0;
item.className = "history-item";
item.textContent = `${when} | ${round.wpm || 0} wpm / ${round.accuracy || 0}% acc / combo ${best_combo} / +${round.xp || 0} xp / ${(round.difficulty || "normal").toLowerCase()}`;
list.appendChild(item);
});
}
(function boot() {
const save = read_save() || {};
const stats = save.stats || {};
set_text("sTotalRounds", String(stats.total_rounds ?? stats.totalRounds ?? 0));
set_text("sBestWpm", String(stats.best_wpm ?? stats.bestWpm ?? 0));
set_text("sBestAcc", `${Math.round(stats.best_accuracy ?? stats.bestAccuracy ?? 0)}%`);
set_text("sAvgWpm", String(Math.round(stats.average_wpm ?? stats.averageWpm ?? 0)));
set_text("sAvgAcc", `${Math.round(stats.average_accuracy ?? stats.averageAccuracy ?? 0)}%`);
render_history(Array.isArray(save.recent_rounds ?? save.recentRounds) ? save.recent_rounds ?? save.recentRounds : []);
})();