-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: remove server timeout progression * feat: clean nudge-game route * feat: add planned game nudge on time getting to 0 * feat: convert create-game from api to action * feat: convert nudge game from api route to action * feat: remove unknownParser from actions * feat: convert create game api to action * refactor: move start-game api to action * refactor: convert update answer api to action * refactor: change exit-game api to action * refactor: move actions out of folders * refactor: remove custom zod-parser * refactor: remove time-calculator * feat: improve visual cues for correct answers
- Loading branch information
1 parent
b2f863f
commit 9488bde
Showing
30 changed files
with
449 additions
and
665 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use server'; | ||
|
||
import {app, gamesRef} from '@/app/lib/firebase-server-initialization'; | ||
import {GameIdSchema} from '@/app/types'; | ||
import {getAuth} from 'firebase-admin/auth'; | ||
|
||
export async function deleteGameAction({gameId, token}: {gameId: string, token: string}) { | ||
// Authenticate user | ||
const authUser = await getAuth(app).verifyIdToken(token); | ||
|
||
// Parse request (throw an error if not correct) | ||
GameIdSchema.parse(gameId); | ||
|
||
const gameRef = await gamesRef.doc(gameId); | ||
const gameDoc = await gameRef.get(); | ||
const game = gameDoc.data(); | ||
|
||
if (game.leader.uid !== authUser.uid) { | ||
// Respond with JSON indicating no game was found | ||
throw new Error('Only the leader of this game may delete this game.'); | ||
} | ||
|
||
// update database to delete the game | ||
await gameRef.delete(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use server'; | ||
|
||
import {app, gamesRef} from '@/app/lib/firebase-server-initialization'; | ||
import {FieldValue} from 'firebase-admin/firestore'; | ||
import {GameIdSchema} from '@/app/types'; | ||
import {getAuth} from 'firebase-admin/auth'; | ||
|
||
export async function exitGameAction({gameId, token}: {gameId: string, token: string}) { | ||
// Authenticate user | ||
const authUser = await getAuth(app).verifyIdToken(token); | ||
|
||
// Parse request (throw an error if not correct) | ||
GameIdSchema.parse(gameId); | ||
|
||
const gameRef = await gamesRef.doc(gameId); | ||
|
||
// update database to exit the game | ||
await gameRef.update({ | ||
[`players.${authUser.uid}`]: FieldValue.delete(), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use server'; | ||
|
||
import {app, gamesRef} from '@/app/lib/firebase-server-initialization'; | ||
import {generateName} from '@/app/lib/name-generator'; | ||
import {GameIdSchema} from '@/app/types'; | ||
import {getAuth} from 'firebase-admin/auth'; | ||
|
||
export async function joinGameAction({gameId, token}: {gameId: string, token: string}) { | ||
// Authenticate user | ||
const authUser = await getAuth(app).verifyIdToken(token); | ||
|
||
// Parse request (throw an error if not correct) | ||
GameIdSchema.parse(gameId); | ||
|
||
const gameRef = await gamesRef.doc(gameId); | ||
const gameDoc = await gameRef.get(); | ||
const game = gameDoc.data(); | ||
const playerIdList = Object.keys(game.players); | ||
if (playerIdList.includes(authUser.uid)) return; | ||
if (game.leader.uid === authUser.uid) { | ||
// Respond with JSON indicating no game was found | ||
throw new Error('The game leader may not be a player.'); | ||
} | ||
|
||
// update database to join the game | ||
await gameRef.update({ | ||
[`players.${authUser.uid}`]: generateName(authUser.uid), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use server'; | ||
|
||
import {gamesRef} from '@/app/lib/firebase-server-initialization'; | ||
import {GameIdSchema, gameStates} from '@/app/types'; | ||
import {Timestamp} from 'firebase-admin/firestore'; | ||
|
||
export async function nudgeGame({gameId}: {gameId: string}) { | ||
// Validate request | ||
// Will throw an error if not a string | ||
GameIdSchema.parse(gameId); | ||
|
||
const gameRef = await gamesRef.doc(gameId); | ||
const gameDoc = await gameRef.get(); | ||
const game = gameDoc.data(); | ||
|
||
// force the game state to move to where the game should be | ||
|
||
const timeElapsedInMillis = Timestamp.now().toMillis() - game.startTime.seconds * 1000; | ||
const timeElapsed = timeElapsedInMillis / 1000; | ||
const timePerQuestionAndAnswer = game.timePerQuestion + game.timePerAnswer; | ||
|
||
const totalNumberOfQuestions = Object.keys(game.questions).length; | ||
const finalQuestionIndex = totalNumberOfQuestions - 1; | ||
const correctQuestionIndex = Math.floor(timeElapsed / timePerQuestionAndAnswer); | ||
if (correctQuestionIndex > finalQuestionIndex) { | ||
await gameRef.update({ | ||
state: gameStates.GAME_OVER, | ||
currentQuestionIndex: finalQuestionIndex, | ||
}); | ||
return; | ||
} | ||
|
||
const timeThisQuestionStarted = correctQuestionIndex * timePerQuestionAndAnswer; | ||
const shouldBeAcceptingAnswers = timeElapsed - timeThisQuestionStarted < game.timePerQuestion; | ||
const correctState = shouldBeAcceptingAnswers ? gameStates.AWAITING_PLAYER_ANSWERS : gameStates.SHOWING_CORRECT_ANSWERS; | ||
|
||
await gameRef.update({ | ||
state: correctState, | ||
currentQuestionIndex: correctQuestionIndex, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use server'; | ||
|
||
import {app, gamesRef} from '@/app/lib/firebase-server-initialization'; | ||
import {GameIdSchema, gameStates} from '@/app/types'; | ||
import {FieldValue} from 'firebase-admin/firestore'; | ||
import {getAuth} from 'firebase-admin/auth'; | ||
|
||
export async function startGameAction({gameId, token}: {gameId: string, token: string}) { | ||
// Authenticate user | ||
const authUser = await getAuth(app).verifyIdToken(token); | ||
|
||
// Parse request (throw an error if not correct) | ||
GameIdSchema.parse(gameId); | ||
|
||
const gameRef = await gamesRef.doc(gameId); | ||
const gameDoc = await gameRef.get(); | ||
const game = gameDoc.data(); | ||
|
||
if (game.leader.uid !== authUser.uid) { | ||
// Respond with JSON indicating no game was found | ||
throw new Error('Only the leader of this game may start this game.'); | ||
} | ||
|
||
// update database to start the game | ||
await gameRef.update({ | ||
state: gameStates.AWAITING_PLAYER_ANSWERS, | ||
startTime: FieldValue.serverTimestamp(), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use server'; | ||
|
||
import {app, gamesRef} from '@/app/lib/firebase-server-initialization'; | ||
import {GameIdSchema, gameStates} from '@/app/types'; | ||
import {getAuth} from 'firebase-admin/auth'; | ||
import {z} from 'zod'; | ||
|
||
export async function updateAnswerAction({gameId, answerSelection, token}: {gameId: string, answerSelection: boolean[], token: string}) { | ||
// Authenticate user | ||
const authUser = await getAuth(app).verifyIdToken(token); | ||
|
||
// Parse request (throw an error if not correct) | ||
GameIdSchema.parse(gameId); | ||
|
||
const gameRef = await gamesRef.doc(gameId); | ||
const gameDoc = await gameRef.get(); | ||
const game = gameDoc.data(); | ||
|
||
if (game.state !== gameStates.AWAITING_PLAYER_ANSWERS) { | ||
return new Error(`Answering is not allowed during ${game.state}.`); | ||
} | ||
|
||
// answerSelection must be an array of booleans as long as the game question answers | ||
const currentQuestion = game.questions[game.currentQuestionIndex]; | ||
const ValidAnswerSchema = z.array(z.boolean()).length(currentQuestion.answers.length); | ||
ValidAnswerSchema.parse(answerSelection); | ||
|
||
// update database to start the game | ||
await gameRef.update({ | ||
[`questions.${game.currentQuestionIndex}.playerGuesses.${authUser.uid}`]: answerSelection, | ||
}); | ||
} |
Oops, something went wrong.