Skip to content

Commit 314b8ba

Browse files
committed
Add "next game" button for spectators
1 parent 3d54cfe commit 314b8ba

File tree

3 files changed

+48
-31
lines changed

3 files changed

+48
-31
lines changed

src/components/Navbar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function Navbar() {
5050
const user = useContext(UserContext);
5151
const classes = useStyles();
5252
const settings = useContext(SettingsContext);
53-
const [siteTitleDb] = useFirebaseRef("/site/title");
53+
const [siteTitleDb] = useFirebaseRef("site/title");
5454
const siteTitle = siteTitleDb || "Set with Forks";
5555
const [anchorEl, setAnchorEl] = useState(null);
5656
const [changeSiteTitle, setChangeSiteTitle] = useState(false);
@@ -77,7 +77,7 @@ function Navbar() {
7777
setChangeSiteTitle(false);
7878
title = (title || "").trim();
7979
if (title) {
80-
firebase.database().ref("/site/title").set(title);
80+
firebase.database().ref("site/title").set(title);
8181
}
8282
}
8383

src/pages/GamePage.js

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
8495
function 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
)}

src/pages/LobbyPage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function LobbyPage() {
128128
const gamesQuery = useMemo(() => {
129129
return firebase
130130
.database()
131-
.ref("/publicGames")
131+
.ref("publicGames")
132132
.orderByValue()
133133
.limitToLast(35);
134134
}, []);
@@ -143,7 +143,7 @@ function LobbyPage() {
143143
}, [user.id]);
144144
const myGames = useFirebaseQuery(myGamesQuery);
145145

146-
const [stats, loadingStats] = useFirebaseRef("/stats");
146+
const [stats, loadingStats] = useFirebaseRef("stats");
147147

148148
const handleTabChange = (event, newValue) => {
149149
setTabValue(newValue);

0 commit comments

Comments
 (0)