@@ -85,6 +85,7 @@ async function afterSignIn(user) {
8585 userInfo . innerText = `Signed in as ${ user . displayName } ` ;
8686 signInBtn . style . display = "none" ;
8787
88+ // Only shows admin panel to me
8889 if ( email === "[email protected] " ) { 8990 document . getElementById ( "admin-panel" ) . style . display = "block" ;
9091 loadReports ( ) ;
@@ -109,56 +110,72 @@ async function afterSignIn(user) {
109110}
110111
111112function loadLeaderboard ( ) {
112- db . ref ( "users" ) . once ( "value" ) . then ( snapshot => {
113- const users = snapshot . val ( ) ;
114- const leaderboard = Object . values ( users ) . sort ( ( a , b ) => ( b . score || 0 ) - ( a . score || 0 ) ) ;
115-
116- const tbody = document . querySelector ( "#leaderboard tbody" ) ;
117- tbody . innerHTML = "" ;
118- leaderboard . forEach ( user => {
119- const row = document . createElement ( "tr" ) ;
120- row . innerHTML = `<td>${ user . name } </td><td>${ user . score || 0 } </td>` ;
121- tbody . appendChild ( row ) ;
113+ db . ref ( "users" ) . once ( "value" ) . then ( snapshot => {
114+ const users = snapshot . val ( ) ;
115+ const leaderboard = Object . values ( users ) . sort ( ( a , b ) => ( b . score || 0 ) - ( a . score || 0 ) ) ;
116+
117+ const tbody = document . querySelector ( "#leaderboard tbody" ) ;
118+ tbody . innerHTML = "" ;
119+ leaderboard . forEach ( user => {
120+ const row = document . createElement ( "tr" ) ;
121+ row . innerHTML = `<td>${ user . name } </td><td>${ user . score || 0 } </td>` ;
122+ tbody . appendChild ( row ) ;
123+ } ) ;
122124 } ) ;
123- } ) ;
124125}
125126
126127function checkResults ( ) {
127- fetch ( "https://year8-chess-tournament-backend.glitch.me/check-results" , { method : "POST" } )
128- . then ( res => res . json ( ) )
129- . then ( data => alert ( `Updated games: ${ data . updatedGames . length } ` ) ) ;
128+ fetch ( "https://year8-chess-tournament-backend.glitch.me/check-results" , { method : "POST" } )
129+ . then ( res => res . json ( ) )
130+ . then ( data => alert ( `Updated games: ${ data . updatedGames . length } ` ) ) ;
130131}
131132
132133function startRound ( ) {
133- fetch ( "https://year8-chess-tournament-backend.glitch.me/start-round" , { method : "POST" } )
134- . then ( res => res . json ( ) )
135- . then ( data => alert ( `Started new round with ${ data . pairings } games.` ) ) ;
134+ fetch ( "https://year8-chess-tournament-backend.glitch.me/start-round" , { method : "POST" } )
135+ . then ( res => res . json ( ) )
136+ . then ( data => alert ( `Started new round with ${ data . pairings } games.` ) ) ;
136137}
137138
138- function reportTroll ( ) {
139- if ( ! currentUser ) {
140- alert ( "Please sign in first." ) ;
141- return ;
139+ async function checkIfFirstMove ( gameId ) {
140+ const res = await fetch ( `https://year8-chess-tournament-backend.glitch.me/check-if-first-move?gameId=${ gameId } ` , {
141+ method : "GET" ,
142+ headers : { "Content-Type" : "application/json" } ,
143+ } ) ;
144+
145+ const data = res . json ( ) ;
146+
147+ if ( data . success ) {
148+ return data . gameStarted ;
149+ } else {
150+ throw new Error ( "Failed to check if game has started" )
151+ }
142152}
143153
144- fetch ( "https://year8-chess-tournament-backend.glitch.me/report-troll" , {
145- method : "POST" ,
146- headers : { "Content-Type" : "application/json" } ,
147- body : JSON . stringify ( { reporterId : currentUser . uid } )
148- } )
149- . then ( res => res . json ( ) )
150- . then ( data => {
151- if ( data . success ) {
152- alert ( "New game created. Use your new link." ) ;
153- } else {
154- alert ( "Failed to create a new game." ) ;
155- console . error ( "Backend error:" , data . error || data ) ;
156- }
154+ function reportTroll ( ) {
155+ if ( ! currentUser ) {
156+ alert ( "Please sign in first." ) ;
157+ return ;
158+ }
159+
160+ fetch ( "https://year8-chess-tournament-backend.glitch.me/report-troll" , {
161+ method : "POST" ,
162+ headers : { "Content-Type" : "application/json" } ,
163+ body : JSON . stringify ( { reporterId : currentUser . uid } )
157164 } )
158- . catch ( ( err ) => {
159- alert ( "Failed to reach server." ) ;
160- console . error ( "Fetch error:" , err ) ;
161- } ) ;
165+ . then ( res => res . json ( ) )
166+ . then ( data => {
167+ if ( data . success ) {
168+ afterSignIn ( currentUser ) ;
169+ alert ( "New game created. Use your new link." ) ;
170+ } else {
171+ alert ( "Failed to create a new game." ) ;
172+ console . error ( "Backend error:" , data . error || data ) ;
173+ }
174+ } )
175+ . catch ( ( err ) => {
176+ alert ( "Failed to reach server." ) ;
177+ console . error ( "Fetch error:" , err ) ;
178+ } ) ;
162179}
163180
164181function loadReports ( ) {
@@ -217,10 +234,12 @@ async function loadMatches() {
217234 liveContainer . innerHTML = '' ;
218235 finishedContainer . innerHTML = '' ;
219236
220- Object . values ( games ) . forEach ( game => {
237+ for ( const game of Object . values ( games ) ) {
221238 const { lichessGameId, white, black, status } = game ;
222- if ( ! lichessGameId ) return ;
223- if ( status != 'finished' && status != 'pending' ) return ;
239+ if ( ! lichessGameId ) continue ;
240+ if ( status != 'finished' && status != 'pending' ) continue ;
241+ const hasStarted = await checkIfFirstMove ( lichessGameId ) ;
242+ if ( ! hasStarted ) continue ;
224243
225244 const card = document . createElement ( 'div' ) ;
226245 card . className = 'match-card' ;
@@ -250,7 +269,7 @@ async function loadMatches() {
250269 } else {
251270 finishedContainer . appendChild ( card ) ;
252271 }
253- } ) ;
272+ }
254273}
255274/*
256275// Reload matches every 30 seconds
0 commit comments