@@ -81,10 +81,22 @@ const useStyles = makeStyles((theme) => ({
8181 } ,
8282} ) ) ;
8383
84+ function getNextGameId ( gameId ) {
85+ const idx = gameId . lastIndexOf ( "-" ) ;
86+ let id = gameId ,
87+ num = 0 ;
88+ if ( gameId . slice ( idx + 1 ) . match ( / [ 0 - 9 ] + / ) ) {
89+ id = gameId . slice ( 0 , idx ) ;
90+ num = parseInt ( gameId . slice ( idx + 1 ) ) ;
91+ }
92+ return `${ id } -${ num + 1 } ` ;
93+ }
94+
8495function GamePage ( { match } ) {
8596 const user = useContext ( UserContext ) ;
8697 const { volume, notifications } = useContext ( SettingsContext ) ;
8798 const gameId = match . params . id ;
99+ const nextGameId = useMemo ( ( ) => getNextGameId ( gameId ) , [ gameId ] ) ;
88100 const classes = useStyles ( ) ;
89101
90102 const [ waiting , setWaiting ] = useState ( false ) ;
@@ -95,6 +107,11 @@ function GamePage({ match }) {
95107
96108 const [ game , loadingGame ] = useFirebaseRef ( `games/${ gameId } ` ) ;
97109 const [ gameData , loadingGameData ] = useFirebaseRef ( `gameData/${ gameId } ` ) ;
110+ const [ hasNextGame ] = useFirebaseRef (
111+ game ?. status === "done" && ( ! game . users || ! ( user . id in game . users ) )
112+ ? `games/${ nextGameId } /status`
113+ : null
114+ ) ;
98115 const [ playSuccess ] = useSound ( foundSfx ) ;
99116 const [ playFail1 ] = useSound ( failSfx1 ) ;
100117 const [ playFail2 ] = useSound ( failSfx2 ) ;
@@ -333,35 +350,29 @@ function GamePage({ match }) {
333350 }
334351
335352 async function handlePlayAgain ( ) {
336- if ( game ?. status !== "done" || spectating || waiting ) {
353+ if ( game ?. status !== "done" || ( spectating && ! hasNextGame ) || waiting ) {
337354 return ;
338355 }
339- const idx = gameId . lastIndexOf ( "-" ) ;
340- let id = gameId ,
341- num = 0 ;
342- if ( gameId . slice ( idx + 1 ) . match ( / [ 0 - 9 ] + / ) ) {
343- id = gameId . slice ( 0 , idx ) ;
344- num = parseInt ( gameId . slice ( idx + 1 ) ) ;
345- }
346- setWaiting ( true ) ;
347- const newId = `${ id } -${ num + 1 } ` ;
348- const newGame = {
349- gameId : newId ,
350- access : game . access ,
351- mode : game . mode ,
352- enableHint : game . enableHint ,
353- } ;
354- firebase . analytics ( ) . logEvent ( "play_again" , newGame ) ;
355- try {
356- await createGame ( newGame ) ;
357- } catch ( error ) {
358- if ( error . code !== "functions/already-exists" ) {
359- alert ( error . toString ( ) ) ;
360- setWaiting ( false ) ;
361- return ;
356+ if ( ! spectating ) {
357+ setWaiting ( true ) ;
358+ const newGame = {
359+ gameId : nextGameId ,
360+ access : game . access ,
361+ mode : game . mode ,
362+ enableHint : game . enableHint ,
363+ } ;
364+ firebase . analytics ( ) . logEvent ( "play_again" , newGame ) ;
365+ try {
366+ await createGame ( newGame ) ;
367+ } catch ( error ) {
368+ if ( error . code !== "functions/already-exists" ) {
369+ alert ( error . toString ( ) ) ;
370+ setWaiting ( false ) ;
371+ return ;
372+ }
362373 }
363374 }
364- setRedirect ( `/room/${ newId } ` ) ;
375+ setRedirect ( `/room/${ nextGameId } ` ) ;
365376 }
366377
367378 return (
@@ -420,7 +431,7 @@ function GamePage({ match }) {
420431 Runner-up: < User id = { leaderboard [ 1 ] } />
421432 </ Typography >
422433 ) }
423- { ! spectating && (
434+ { ( ! spectating || hasNextGame ) && (
424435 < Tooltip
425436 placement = "top"
426437 title = "Create or join a new game with the same settings. (Ctrl+Enter)"
@@ -432,7 +443,13 @@ function GamePage({ match }) {
432443 style = { { marginTop : 12 } }
433444 disabled = { waiting }
434445 >
435- { waiting ? < Loading /> : "Play Again" }
446+ { waiting ? (
447+ < Loading />
448+ ) : spectating ? (
449+ "Next Game"
450+ ) : (
451+ "Play Again"
452+ ) }
436453 </ Button >
437454 </ Tooltip >
438455 ) }
0 commit comments