@@ -15,7 +15,7 @@ const db = firebase.database();
1515const signInBtn = document . getElementById ( "googleSignInBtn" ) ;
1616const userInfo = document . getElementById ( "user-info" ) ;
1717let currentUser = null ;
18- let testEmailYet = false ;
18+ let currentUserUid = null ;
1919
2020function 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-
141123function 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
295343setInterval(() => {
0 commit comments