in app/actions/create-game.ts [26:71]
export async function createGameAction({gameSettings, tokens}: {gameSettings: GameSettings, tokens: Tokens}): Promise<{gameId: string}> {
const authUser = await validateTokens(tokens);
// Parse request (throw an error if not correct)
const {timePerQuestion, timePerAnswer, questionAdvancement} = GameSettingsSchema.parse(gameSettings);
const querySnapshot = await questionsRef.get();
const validQuestionsArray = querySnapshot.docs.reduce((agg: Question[], doc: QueryDocumentSnapshot) => {
let question = doc.data();
try {
question = QuestionSchema.parse(question);
return [...agg, question];
} catch (error) {
console.warn(`WARNING: The question "${question?.prompt}" [Firestore ID: ${doc.id}] has an issue and will not be added to the game.`);
return agg;
}
}, []);
// convert array to object for Firebase
const questions = {...validQuestionsArray};
// create game with server endpoint
const leader = {
displayName: generateName(authUser.uid),
uid: authUser.uid,
};
const newGame: Game = {
questions,
leader,
players: {},
state: gameStates.NOT_STARTED,
currentQuestionIndex: 0,
timePerQuestion: timePerQuestion + 1, // add one for padding between questions
timePerAnswer: timePerAnswer + 1, // add one for padding between questions
questionAdvancement,
currentStateStartTime: {seconds: 0},
};
const gameRef = await gamesRef.add(newGame);
if (gameRef.id) return {gameId: gameRef.id};
throw new Error('no gameId returned in the response');
}