1- // app.js
2-
31const firebaseConfig = {
4- apiKey : "AIzaSyDcXSHxWWCCwIypr62CG4OM69O4J9NLBNI" ,
5- authDomain : "sbhs-year-8-chess-tournament.firebaseapp.com" ,
6- databaseURL : "https://sbhs-year-8-chess-tournament-default-rtdb.firebaseio.com" ,
7- projectId : "sbhs-year-8-chess-tournament" ,
8- storageBucket : "sbhs-year-8-chess-tournament.firebasestorage.app" ,
9- messagingSenderId : "632119283742" ,
10- appId : "1:632119283742:web:ea2e5b59f5d80df1cede0f" ,
2+ apiKey : "AIzaSyDcXSHxWWCCwIypr62CG4OM69O4J9NLBNI" ,
3+ authDomain : "sbhs-year-8-chess-tournament.firebaseapp.com" ,
4+ databaseURL : "https://sbhs-year-8-chess-tournament-default-rtdb.firebaseio.com" ,
5+ projectId : "sbhs-year-8-chess-tournament" ,
6+ storageBucket : "sbhs-year-8-chess-tournament.firebasestorage.app" ,
7+ messagingSenderId : "632119283742" ,
8+ appId : "1:632119283742:web:ea2e5b59f5d80df1cede0f" ,
119} ;
1210
1311firebase . initializeApp ( firebaseConfig ) ;
@@ -21,103 +19,117 @@ let currentUser = null;
2119signInBtn . addEventListener ( "click" , ( ) => {
2220const provider = new firebase . auth . GoogleAuthProvider ( ) ;
2321auth . signInWithPopup ( provider )
24- . then ( ( result ) => {
22+ . then ( ( result ) => {
2523 const user = result . user ;
2624 const email = user . email ;
25+
2726 console . log ( email ) ;
2827 if ( ! email . endsWith ( "@student.sbhs.nsw.edu.au" ) ) {
29- alert ( "Please use your school Google account." ) ;
30- auth . signOut ( ) ;
31- return ;
28+ alert ( "Please use your school Google account." ) ;
29+ auth . signOut ( ) ;
30+ return ;
3231 }
3332
34- // Add user to database
3533 const uid = user . uid ;
36- db . ref ( "users/" + uid ) . set ( {
37- name : user . displayName ,
38- email : user . email ,
39- score : 0
34+ const userRef = db . ref ( "users/" + uid ) ;
35+
36+ // Only create the user record if it doesn't exist
37+ userRef . once ( "value" ) . then ( ( snapshot ) => {
38+ if ( ! snapshot . exists ( ) ) {
39+ userRef . set ( {
40+ name : user . displayName ,
41+ email : user . email ,
42+ score : 0 ,
43+ opponent : null ,
44+ currentGameUrl : null
45+ } ) ;
46+ }
4047 } ) ;
4148
4249 userInfo . innerText = `Signed in as ${ user . displayName } ` ;
4350 signInBtn . style . display = "none" ;
44- } )
45- . catch ( ( error ) => {
51+ } )
52+ . catch ( ( error ) => {
4653 console . error ( "Sign-in error:" , error ) ;
4754 alert ( "Sign-in failed." ) ;
48- } ) ;
55+ } ) ;
4956} ) ;
5057
5158auth . onAuthStateChanged ( async ( user ) => {
52- if ( user ) {
53- const uid = user . uid ;
54- const email = user . email ;
55- currentUser = user ;
56- userInfo . innerText = `Signed in as ${ user . displayName } ` ;
57- signInBtn . style . display = "none" ;
59+ if ( user ) {
60+ const uid = user . uid ;
61+ const email = user . email ;
62+ currentUser = user ;
63+ userInfo . innerText = `Signed in as ${ user . displayName } ` ;
64+ signInBtn . style . display = "none" ;
5865
59- if ( email === "[email protected] " ) { 60- document . getElementById ( "admin-panel" ) . style . display = "block" ;
61- }
66+ if ( email === "[email protected] " ) { 67+ document . getElementById ( "admin-panel" ) . style . display = "block" ;
68+ }
6269
63- loadLeaderboard ( ) ;
70+ loadLeaderboard ( ) ;
6471
65- // Show current game
66- const snap = await db . ref ( "users/" + uid ) . once ( "value" ) ;
67- const userData = snap . val ( ) ;
68- if ( userData && userData . currentGameUrl ) {
69- const gameLink = document . getElementById ( "game-link" ) ;
70- gameLink . href = userData . currentGameUrl ;
71- gameLink . innerText = "Join Your Game" ;
72- document . getElementById ( "game-panel" ) . style . display = "block" ;
73- }
72+ // Show current game
73+ const snap = await db . ref ( "users/" + uid ) . once ( "value" ) ;
74+ const userData = snap . val ( ) ;
75+ if ( userData && userData . currentGameUrl ) {
76+ const gameLink = document . getElementById ( "game-link" ) ;
77+ gameLink . href = userData . currentGameUrl ;
78+ gameLink . innerText = "Join Your Game" ;
79+ document . getElementById ( "game-panel" ) . style . display = "block" ;
7480 }
75- } ) ;
81+ }
82+ } ) ;
7683
7784function loadLeaderboard ( ) {
78- db . ref ( "users" ) . once ( "value" ) . then ( snapshot => {
79- const users = snapshot . val ( ) ;
80- const leaderboard = Object . values ( users ) . sort ( ( a , b ) => ( b . score || 0 ) - ( a . score || 0 ) ) ;
81-
82- const tbody = document . querySelector ( "#leaderboard tbody" ) ;
83- tbody . innerHTML = "" ;
84- leaderboard . forEach ( user => {
85- const row = document . createElement ( "tr" ) ;
86- row . innerHTML = `<td>${ user . name } </td><td>${ user . score || 0 } </td>` ;
87- tbody . appendChild ( row ) ;
88- } ) ;
89- } ) ;
90- }
85+ db . ref ( "users" ) . once ( "value" ) . then ( snapshot => {
86+ const users = snapshot . val ( ) ;
87+ const leaderboard = Object . values ( users ) . sort ( ( a , b ) => ( b . score || 0 ) - ( a . score || 0 ) ) ;
88+
89+ const tbody = document . querySelector ( "#leaderboard tbody" ) ;
90+ tbody . innerHTML = "" ;
91+ leaderboard . forEach ( user => {
92+ const row = document . createElement ( "tr" ) ;
93+ row . innerHTML = `<td>${ user . name } </td><td>${ user . score || 0 } </td>` ;
94+ tbody . appendChild ( row ) ;
95+ } ) ;
96+ } ) ;
97+ }
9198
9299function checkResults ( ) {
93- fetch ( "https://year8-chess-tournament-backend.glitch.me/check-results" , { method : "POST" } )
94- . then ( res => res . json ( ) )
95- . then ( data => alert ( `Updated games: ${ data . updatedGames . length } ` ) ) ;
100+ fetch ( "https://year8-chess-tournament-backend.glitch.me/check-results" , { method : "POST" } )
101+ . then ( res => res . json ( ) )
102+ . then ( data => alert ( `Updated games: ${ data . updatedGames . length } ` ) ) ;
96103}
97104
98105function startRound ( ) {
99- fetch ( "https://year8-chess-tournament-backend.glitch.me/start-round" , { method : "POST" } )
100- . then ( res => res . json ( ) )
101- . then ( data => alert ( `Started new round with ${ data . pairings } games.` ) ) ;
106+ fetch ( "https://year8-chess-tournament-backend.glitch.me/start-round" , { method : "POST" } )
107+ . then ( res => res . json ( ) )
108+ . then ( data => alert ( `Started new round with ${ data . pairings } games.` ) ) ;
102109}
103110
104111function reportTroll ( ) {
105- if ( ! currentUser ) {
106- alert ( "Please sign in first." ) ;
107- return ;
108- }
112+ if ( ! currentUser ) {
113+ alert ( "Please sign in first." ) ;
114+ return ;
115+ }
109116
110- fetch ( "https://year8-chess-tournament-backend.glitch.me/report-troll" , {
111- method : "POST" ,
112- headers : { "Content-Type" : "application/json" } ,
113- body : JSON . stringify ( { reporterId : currentUser . uid } )
117+ fetch ( "https://year8-chess-tournament-backend.glitch.me/report-troll" , {
118+ method : "POST" ,
119+ headers : { "Content-Type" : "application/json" } ,
120+ body : JSON . stringify ( { reporterId : currentUser . uid } )
121+ } )
122+ . then ( res => res . json ( ) )
123+ . then ( data => {
124+ if ( data . success ) {
125+ alert ( "New game created. Use your new link." ) ;
126+ } else {
127+ alert ( "Failed to create a new game." ) ;
128+ console . error ( "Backend error:" , data . error || data ) ;
129+ }
114130 } )
115- . then ( res => res . json ( ) )
116- . then ( data => {
117- if ( data . success ) {
118- alert ( "New game created. Use your new link." ) ;
119- } else {
120- alert ( "Failed to create a new game." ) ;
121- }
122- } ) ;
131+ . catch ( ( err ) => {
132+ alert ( "Failed to reach server." ) ;
133+ console . error ( "Fetch error:" , err ) ;
134+ } ) ;
123135}
0 commit comments