diff --git a/public/assets/js/index.js b/public/assets/js/index.js index 2989935..8e8c972 100644 --- a/public/assets/js/index.js +++ b/public/assets/js/index.js @@ -85,6 +85,8 @@ function showIntro() { displayLaTeXInBody(); $("#container").show(); + + loadTopScoresThisMonth(); } function endGame() { @@ -344,6 +346,46 @@ async function loadLeaderboard(timeRange) { } } +async function loadTopScoresThisMonth() { + const leaderboardSection = $("#leaderboard-section"); + leaderboardSection.empty(); + + try { + let query = db.collection('leaderboard'); + + // Add time constraints for the current month + const now = new Date(); + const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1); + query = query.where('timestamp', '>=', startOfMonth); + + const snapshot = await query.orderBy('score', 'desc').limit(5).get(); + + if (snapshot.empty) { + leaderboardSection.append('

No scores yet!

'); + return; + } + + leaderboardSection.append('

Top Scores This Month

'); + + let rank = 1; + snapshot.forEach((doc) => { + const data = doc.data(); + const scoreEntry = ` +
+ #${rank} + ${escapeHtml(data.name)} + ${data.score} points +
+ `; + leaderboardSection.append(scoreEntry); + rank++; + }); + } catch (error) { + console.error("Error loading top scores this month:", error); + leaderboardSection.append('

Error loading top scores

'); + } +} + // Start by showing the intro. $(document).ready(function() { // Handlers