From 6c3288a2c541cf35f952bedddba0c7985e31a3f6 Mon Sep 17 00:00:00 2001 From: Eugene Toder Date: Sun, 1 Dec 2024 16:55:13 -0500 Subject: [PATCH] Small cleanup for enable hint logic (#53) 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. --- database.rules.json | 2 +- functions/src/index.ts | 6 +----- scripts/src/calcStats.js | 7 +------ src/components/GameSettings.js | 4 ++-- src/util.js | 5 ++--- 5 files changed, 7 insertions(+), 17 deletions(-) diff --git a/database.rules.json b/database.rules.json index cf16da3..6af671d 100644 --- a/database.rules.json +++ b/database.rules.json @@ -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'" } } }, diff --git a/functions/src/index.ts b/functions/src/index.ts index 8a15b8f..d720793 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -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; } diff --git a/scripts/src/calcStats.js b/scripts/src/calcStats.js index c7b3e7c..5aa4011 100644 --- a/scripts/src/calcStats.js +++ b/scripts/src/calcStats.js @@ -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; } diff --git a/src/components/GameSettings.js b/src/components/GameSettings.js index 0894301..4b95479 100644 --- a/src/components/GameSettings.js +++ b/src/components/GameSettings.js @@ -54,8 +54,8 @@ function GameSettings({ game, gameId, userId }) { control={} label="Enable Hints" disabled={ - Object.keys(game.users || {}).length > 1 || - game.access !== "private" + game.access !== "private" || + Object.keys(game.users || {}).length !== 1 } /> diff --git a/src/util.js b/src/util.js index 23933af..068a8a1 100644 --- a/src/util.js +++ b/src/util.js @@ -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 ); }