Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions public/assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ function showIntro() {

displayLaTeXInBody();
$("#container").show();

loadTopScoresThisMonth();
}

function endGame() {
Expand Down Expand Up @@ -344,6 +346,46 @@ async function loadLeaderboard(timeRange) {
}
}

async function loadTopScoresThisMonth() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a duplicate of the existing loadLeaderboard function which already supports monthly filtering via timeRange='month'. Consider extending the existing function with parameters for limit and target element instead of creating a new one.

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('<p>No scores yet!</p>');
return;
}

leaderboardSection.append('<h2>Top Scores This Month</h2>');

let rank = 1;
snapshot.forEach((doc) => {
const data = doc.data();
const scoreEntry = `
<div class="leaderboard-entry" style="margin: 5px 0;">
<span class="rank">#${rank}</span>
<span class="name">${escapeHtml(data.name)}</span>
<span class="score">${data.score} points</span>
</div>
`;
leaderboardSection.append(scoreEntry);
rank++;
});
} catch (error) {
console.error("Error loading top scores this month:", error);
leaderboardSection.append('<p>Error loading top scores</p>');
}
}

// Start by showing the intro.
$(document).ready(function() {
// Handlers
Expand Down