refactor: dependency injection for GameSessionStore via composition root

This commit is contained in:
lila 2026-04-28 13:48:50 +02:00
parent 4f59f3bc14
commit a4a4bfff57
6 changed files with 107 additions and 79 deletions

View file

@ -8,14 +8,13 @@ import type {
AnswerSubmission,
AnswerResult,
} from "@lila/shared";
import { InMemoryGameSessionStore } from "../gameSessionStore/index.js";
import type { GameSessionStore } from "../gameSessionStore/index.js";
import { NotFoundError } from "../errors/AppError.js";
import { shuffleArray } from "../lib/utils.js";
const gameSessionStore = new InMemoryGameSessionStore();
export const createGameSession = async (
request: GameRequest,
store: GameSessionStore,
): Promise<GameSession> => {
const terms = await getGameTerms(
request.source_language,
@ -60,15 +59,16 @@ export const createGameSession = async (
);
const sessionId = randomUUID();
await gameSessionStore.create(sessionId, { answers: answerKey });
await store.create(sessionId, { answers: answerKey });
return { sessionId, questions };
};
export const evaluateAnswer = async (
submission: AnswerSubmission,
store: GameSessionStore,
): Promise<AnswerResult> => {
const session = await gameSessionStore.get(submission.sessionId);
const session = await store.get(submission.sessionId);
if (!session) {
throw new NotFoundError(`Game session not found: ${submission.sessionId}`);