refactor: dependency injection for GameSessionStore via composition root
This commit is contained in:
parent
4f59f3bc14
commit
a4a4bfff57
6 changed files with 107 additions and 79 deletions
|
|
@ -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}`);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue