Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions database.rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"users": {
"$userId": {
".write": "auth != null && auth.uid == $userId && data.parent().parent().child('status').val() == 'waiting' && !(root.hasChild('users/' + auth.uid + '/banned') && now < root.child('users/' + auth.uid + '/banned').val())",
".write": "auth != null && auth.uid == $userId && data.parent().parent().child('status').val() == 'waiting' && newData.parent().parent().parent().parent().hasChild('userGames/' + $userId + '/' + $gameId) == newData.exists() && !(root.hasChild('users/' + auth.uid + '/banned') && now < root.child('users/' + auth.uid + '/banned').val())",
".validate": "newData.isNumber() && newData.val() == now"
}
},
Expand Down Expand Up @@ -117,7 +117,7 @@
".read": "auth != null",
".indexOn": ".value",
"$gameId": {
".write": "auth != null && auth.uid == $userId",
".write": "auth != null && auth.uid == $userId && newData.parent().parent().parent().hasChild('games/' + $gameId + '/users/' + $userId) == newData.exists()",
".validate": "newData.isNumber() && newData.val() == root.child('games/' + $gameId + '/createdAt').val()"
}
}
Expand Down
80 changes: 39 additions & 41 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,58 +258,56 @@ export const createGame = functions.https.onCall(async (data, context) => {
"The function must be called while authenticated."
);
}

const userId = context.auth.uid;
const oneHourAgo = Date.now() - 3600000;
const recentGameIds = await admin
.database()
.ref(`userGames/${userId}`)
.orderByValue()
.startAt(oneHourAgo)
.once("value");

const recentGames = await Promise.all(
Object.keys(recentGameIds.val() || {}).map((recentGameId) =>
admin.database().ref(`games/${recentGameId}`).once("value")
)
);
if (access === "public") {
const oneHourAgo = Date.now() - 3600000;
const recentGameIds = await admin
.database()
.ref(`userGames/${userId}`)
.orderByValue()
.startAt(oneHourAgo)
.once("value");

let unfinishedGames = 0;
for (const snap of recentGames) {
if (
snap.child("host").val() === userId &&
snap.child("status").val() !== "done" &&
snap.child("access").val() === "public"
) {
++unfinishedGames;
const recentGames = await Promise.all(
Object.keys(recentGameIds.val() || {}).map((recentGameId) =>
admin.database().ref(`games/${recentGameId}`).once("value")
)
);

let unfinishedGames = 0;
for (const snap of recentGames) {
if (
snap.child("access").val() === "public" &&
snap.child("status").val() !== "done" &&
snap.child("host").val() === userId
) {
++unfinishedGames;
}
}
if (unfinishedGames >= MAX_UNFINISHED_GAMES_PER_HOUR) {
throw new functions.https.HttpsError(
"resource-exhausted",
"Too many unfinished public games were recently created."
);
}
}

const { committed, snapshot } = await admin
.database()
.ref(`games/${gameId}`)
.transaction((currentData) => {
if (currentData === null) {
if (
unfinishedGames >= MAX_UNFINISHED_GAMES_PER_HOUR &&
access === "public"
) {
throw new functions.https.HttpsError(
"resource-exhausted",
"Too many unfinished public games were recently created."
);
}
return {
host: userId,
createdAt: admin.database.ServerValue.TIMESTAMP,
status: "waiting",
access,
mode,
enableHint,
};
} else {
return;
if (currentData !== null) {
return; // the game already exists, bail out
}
return {
host: userId,
createdAt: admin.database.ServerValue.TIMESTAMP,
status: "waiting",
access,
mode,
enableHint,
};
});
if (!committed) {
throw new functions.https.HttpsError(
Expand Down
3 changes: 1 addition & 2 deletions src/pages/RoomPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ function RoomPage({ match, location }) {
useEffect(() => {
if (
!leaving &&
game &&
game.status === "waiting" &&
game?.status === "waiting" &&
(!game.users || !(user.id in game.users))
) {
const updates = {
Expand Down