refactor: extract shuffleArray to lib/utils, rename correctAnswers to terms
This commit is contained in:
parent
c46729f365
commit
2ff7d1759e
5 changed files with 59 additions and 60 deletions
10
apps/api/src/lib/utils.ts
Normal file
10
apps/api/src/lib/utils.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
export const shuffleArray = <T>(array: T[]): T[] => {
|
||||
const result = [...array];
|
||||
for (let i = result.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
const temp = result[i]!;
|
||||
result[i] = result[j]!;
|
||||
result[j] = temp;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
|
@ -10,13 +10,14 @@ import type {
|
|||
} from "@lila/shared";
|
||||
import { InMemoryGameSessionStore } 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,
|
||||
): Promise<GameSession> => {
|
||||
const correctAnswers = await getGameTerms(
|
||||
const terms = await getGameTerms(
|
||||
request.source_language,
|
||||
request.target_language,
|
||||
request.pos,
|
||||
|
|
@ -27,19 +28,19 @@ export const createGameSession = async (
|
|||
const answerKey = new Map<string, number>();
|
||||
|
||||
const questions: GameQuestion[] = await Promise.all(
|
||||
correctAnswers.map(async (correctAnswer) => {
|
||||
terms.map(async (term) => {
|
||||
const distractorTexts = await getDistractors(
|
||||
correctAnswer.termId,
|
||||
correctAnswer.targetText,
|
||||
term.termId,
|
||||
term.targetText,
|
||||
request.target_language,
|
||||
request.pos,
|
||||
request.difficulty,
|
||||
3,
|
||||
);
|
||||
|
||||
const optionTexts = [correctAnswer.targetText, ...distractorTexts];
|
||||
const shuffledTexts = shuffle(optionTexts);
|
||||
const correctOptionId = shuffledTexts.indexOf(correctAnswer.targetText);
|
||||
const optionTexts = [term.targetText, ...distractorTexts];
|
||||
const shuffledTexts = shuffleArray(optionTexts);
|
||||
const correctOptionId = shuffledTexts.indexOf(term.targetText);
|
||||
|
||||
const options: AnswerOption[] = shuffledTexts.map((text, index) => ({
|
||||
optionId: index,
|
||||
|
|
@ -51,8 +52,8 @@ export const createGameSession = async (
|
|||
|
||||
return {
|
||||
questionId,
|
||||
prompt: correctAnswer.sourceText,
|
||||
gloss: correctAnswer.sourceGloss,
|
||||
prompt: term.sourceText,
|
||||
gloss: term.sourceGloss,
|
||||
options,
|
||||
};
|
||||
}),
|
||||
|
|
@ -64,17 +65,6 @@ export const createGameSession = async (
|
|||
return { sessionId, questions };
|
||||
};
|
||||
|
||||
const shuffle = <T>(array: T[]): T[] => {
|
||||
const result = [...array];
|
||||
for (let i = result.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
const temp = result[i]!;
|
||||
result[i] = result[j]!;
|
||||
result[j] = temp;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
export const evaluateAnswer = async (
|
||||
submission: AnswerSubmission,
|
||||
): Promise<AnswerResult> => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue