Skip to content

Commit 8f99b99

Browse files
changing lb functionality
1 parent 28cf202 commit 8f99b99

File tree

2 files changed

+72
-20
lines changed

2 files changed

+72
-20
lines changed

app.js

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const db = firebase.database();
1515
const signInBtn = document.getElementById("googleSignInBtn");
1616
const userInfo = document.getElementById("user-info");
1717
let currentUser = null;
18-
let testEmailYet = false;
18+
let currentUserUid = null;
1919

2020
function isNumeric(str) {
2121
if (typeof str != "string") return false // we only process strings!
@@ -82,6 +82,7 @@ async function afterSignIn(user) {
8282
const uid = user.uid;
8383
const email = user.email;
8484
currentUser = user;
85+
currentUserUid = uid;
8586
userInfo.innerText = `Signed in as ${user.displayName}`;
8687
signInBtn.style.display = "none";
8788

@@ -119,25 +120,6 @@ function hideIframeLoader(iframe) {
119120
iframe.style.visibility = 'visible';
120121
}
121122

122-
function loadLeaderboard() {
123-
document.getElementById('leaderboard-loader').style.width = '100%';
124-
db.ref("users").once("value").then(snapshot => {
125-
const users = snapshot.val();
126-
const leaderboard = Object.values(users).sort((a, b) => (b.score || 0) - (a.score || 0));
127-
128-
const tbody = document.querySelector("#leaderboard tbody");
129-
tbody.innerHTML = "";
130-
leaderboard.forEach(user => {
131-
const row = document.createElement("tr");
132-
row.innerHTML = `<td>${user.name}</td><td>${user.score || 0}</td>`;
133-
tbody.appendChild(row);
134-
});
135-
});
136-
setTimeout(() => {
137-
document.getElementById('leaderboard-loader').style.width = '0%';
138-
}, 300); // delay so users see the bar
139-
}
140-
141123
function checkResults() {
142124
fetch("https://year8-chess-tournament-backend.glitch.me/check-results", { method: "POST" })
143125
.then(res => res.json())
@@ -290,6 +272,72 @@ async function loadMatches() {
290272
document.getElementById('games-loading-bar-container').style.display = 'none';
291273
}
292274

275+
let leaderboardData = [];
276+
let currentVisibleCount = 5;
277+
278+
function loadLeaderboard() {
279+
document.getElementById('leaderboard-loader').style.width = '100%';
280+
281+
db.ref("users").once("value").then(snapshot => {
282+
const users = snapshot.val();
283+
leaderboardData = Object.entries(users)
284+
.map(([uid, data]) => ({
285+
uid,
286+
name: data.name || "Anonymous",
287+
score: data.score || 0
288+
}))
289+
.sort((a, b) => b.score - a.score);
290+
291+
renderLeaderboard();
292+
293+
setTimeout(() => {
294+
document.getElementById('leaderboard-loader').style.width = '0%';
295+
}, 300);
296+
});
297+
}
298+
299+
function renderLeaderboard() {
300+
const tbody = document.querySelector("#leaderboard tbody");
301+
tbody.innerHTML = "";
302+
303+
let shown = 0;
304+
let userIsShown = false;
305+
306+
for (let i = 0; i < leaderboardData.length; i++) {
307+
const user = leaderboardData[i];
308+
const isCurrentUser = user.uid === currentUserUid;
309+
const withinVisible = shown < currentVisibleCount;
310+
311+
if (withinVisible || isCurrentUser) {
312+
const row = document.createElement("tr");
313+
row.innerHTML = `<td>${user.name}</td><td>${user.score}</td>`;
314+
if (isCurrentUser) row.style.fontWeight = 'bold';
315+
tbody.appendChild(row);
316+
317+
if (!isCurrentUser) shown++;
318+
if (isCurrentUser) userIsShown = true;
319+
}
320+
}
321+
322+
document.getElementById('show-more-btn').style.display =
323+
shown < leaderboardData.length ? 'inline-block' : 'none';
324+
325+
document.getElementById('minimise-btn').style.display =
326+
currentVisibleCount > 5 ? 'inline-block' : 'none';
327+
}
328+
329+
// Button event listeners
330+
331+
document.getElementById('show-more-btn').addEventListener('click', () => {
332+
currentVisibleCount += 20;
333+
renderLeaderboard();
334+
});
335+
336+
document.getElementById('minimise-btn').addEventListener('click', () => {
337+
currentVisibleCount = 5;
338+
renderLeaderboard();
339+
});
340+
293341
/*
294342
// Reload matches every 30 seconds
295343
setInterval(() => {

index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ <h2>Leaderboard</h2>
4141
</thead>
4242
<tbody></tbody>
4343
</table>
44+
<div id="leaderboard-controls" style="text-align:center; margin-top: 1rem;">
45+
<button id="show-more-btn">Show More</button>
46+
<button id="minimise-btn" style="display: none;">Minimise Leaderboard</button>
47+
</div>
4448

4549
<div id="games-loading-bar-container">
4650
<div id="games-loading-bar"></div>

0 commit comments

Comments
 (0)