export async function updateAnswerAction()

in app/actions/update-answer.ts [24:47]


export async function updateAnswerAction({gameId, answerSelection, tokens}: {gameId: string, answerSelection: boolean[], tokens: Tokens}) {
  const authUser = await validateTokens(tokens);

  // Parse request (throw an error if not correct)
  GameIdSchema.parse(gameId);

  const gameRef = await gamesRef.doc(gameId);
  const gameDoc = await gameRef.get();
  const game = GameSchema.parse(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,
  });
}