Skip to content

Commit

Permalink
Small cleanup for enable hint logic (#53)
Browse files Browse the repository at this point in the history
Enforce that hints are only used in private games in db rules.

Skip all games with enableHint == true in stats on the server side. It's
not worth duplicating logic if someone manages to set enableHint for a
multiplayer game.
  • Loading branch information
eltoder authored Dec 1, 2024
1 parent 2b0bd24 commit 6c3288a
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 17 deletions.
2 changes: 1 addition & 1 deletion database.rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"enableHint": {
".write": "auth != null && auth.uid == data.parent().child('host').val() && newData.exists()",
".validate": "newData.isBoolean()"
".validate": "newData.isBoolean() && data.parent().child('access').val() == 'private'"
}
}
},
Expand Down
6 changes: 1 addition & 5 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ export const finishGame = functions.https.onCall(async (data, context) => {
}

// Check if hints are enabled; and if so, ignore the game in statistics
if (
snapshot.child("enableHint").val() &&
snapshot.child("users").numChildren() === 1 &&
snapshot.child("access").val() === "private"
) {
if (snapshot.child("enableHint").val()) {
return;
}

Expand Down
7 changes: 1 addition & 6 deletions scripts/src/calcStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ export async function calcStats() {
const { finalTime, scores } = replayEvents(gameData, gameMode);

// Check if hints are enabled; and if so, ignore the game in statistics
if (
game.child("enableHint").val() &&
game.child("users").numChildren() === 1 &&
game.child("access").val() === "private" &&
gameMode === "normal"
) {
if (game.child("enableHint").val()) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/GameSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ function GameSettings({ game, gameId, userId }) {
control={<Switch checked={hasHint(game)} onChange={toggleHint} />}
label="Enable Hints"
disabled={
Object.keys(game.users || {}).length > 1 ||
game.access !== "private"
game.access !== "private" ||
Object.keys(game.users || {}).length !== 1
}
/>
</Tooltip>
Expand Down
5 changes: 2 additions & 3 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,8 @@ export function formatTime(t, hideSubsecond) {
export function hasHint(game) {
return (
game.enableHint &&
game.users &&
Object.keys(game.users).length === 1 &&
game.access === "private"
game.access === "private" &&
Object.keys(game.users || {}).length === 1
);
}

Expand Down

0 comments on commit 6c3288a

Please sign in to comment.