Complete the game answer flow: - Add evaluateAnswer service function: looks up the session in the GameSessionStore, compares the submitted optionId against the stored correct answer, returns an AnswerResult. - Add submitAnswer controller with safeParse validation and error handling (session/question not found → 404). - Add POST /api/v1/game/answer route. - Fix createGameSession: was missing the answerKey tracking and the gameSessionStore.create() call, so sessions were never persisted. The full singleplayer game loop now works end-to-end: POST /game/start → GameSession, POST /game/answer → AnswerResult.
32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import type { Request, Response } from "express";
|
|
import { GameRequestSchema, AnswerSubmissionSchema } from "@glossa/shared";
|
|
import { createGameSession, evaluateAnswer } from "../services/gameService.js";
|
|
|
|
export const createGame = async (req: Request, res: Response) => {
|
|
const gameSettings = GameRequestSchema.safeParse(req.body);
|
|
|
|
// TODO: remove when global error handler is implemented
|
|
if (!gameSettings.success) {
|
|
res.status(400).json({ success: false });
|
|
return;
|
|
}
|
|
|
|
const gameQuestions = await createGameSession(gameSettings.data);
|
|
|
|
res.json({ success: true, data: gameQuestions });
|
|
};
|
|
|
|
export const submitAnswer = async (req: Request, res: Response) => {
|
|
const submission = AnswerSubmissionSchema.safeParse(req.body);
|
|
// TODO: remove when global error handler is implemented
|
|
if (!submission.success) {
|
|
res.status(400).json({ success: false });
|
|
return;
|
|
}
|
|
try {
|
|
const result = await evaluateAnswer(submission.data);
|
|
res.json({ success: true, data: result });
|
|
} catch (error) {
|
|
res.status(404).json({ success: false });
|
|
}
|
|
};
|