Skip to content

Commit 4f7d2e4

Browse files
improving lb
1 parent 8f99b99 commit 4f7d2e4

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

app.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ function loadLeaderboard() {
283283
leaderboardData = Object.entries(users)
284284
.map(([uid, data]) => ({
285285
uid,
286-
name: data.name || "Anonymous",
286+
name: data.name,
287287
score: data.score || 0
288288
}))
289289
.sort((a, b) => b.score - a.score);
@@ -300,6 +300,8 @@ function renderLeaderboard() {
300300
const tbody = document.querySelector("#leaderboard tbody");
301301
tbody.innerHTML = "";
302302

303+
let placing = 1;
304+
let lastScore = null;
303305
let shown = 0;
304306
let userIsShown = false;
305307

@@ -308,14 +310,21 @@ function renderLeaderboard() {
308310
const isCurrentUser = user.uid === currentUserUid;
309311
const withinVisible = shown < currentVisibleCount;
310312

311-
if (withinVisible || isCurrentUser) {
313+
if (withinVisible || (isCurrentUser && !userIsShown)) {
314+
if (lastScore !== null && user.score < lastScore) {
315+
placing = shown + 1; // so 1st = index 0 + 1, etc.
316+
}
317+
312318
const row = document.createElement("tr");
313-
row.innerHTML = `<td>${user.name}</td><td>${user.score}</td>`;
319+
row.innerHTML = `<td>${placing}</td><td>${user.name}</td><td>${user.score}</td>`;
314320
if (isCurrentUser) row.style.fontWeight = 'bold';
321+
315322
tbody.appendChild(row);
316323

317324
if (!isCurrentUser) shown++;
318-
if (isCurrentUser) userIsShown = true;
325+
else userIsShown = true; row.classList.add("highlighted-user");
326+
327+
lastScore = user.score;
319328
}
320329
}
321330

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ <h2>Leaderboard</h2>
3737
<div id="leaderboard-loader"></div>
3838
<table id="leaderboard">
3939
<thead>
40-
<tr><th>Name</th><th>Score</th></tr>
40+
<tr><th>Placing</th><th>Name</th><th>Score</th></tr>
4141
</thead>
4242
<tbody></tbody>
4343
</table>

style.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ iframe {
165165
transition: width 0.3s ease;
166166
}
167167

168+
.highlighted-user {
169+
background-color: #ffeaa7;
170+
font-weight: bold;
171+
}
172+
168173
button,
169174
#googleSignInBtn {
170175
background-color: #4caf50;

0 commit comments

Comments
 (0)