Files
wehub-resource-sync c48612c494
CI / E2E Tests (push) Has been cancelled
CI / Lint, Typecheck & Unit Tests (push) Has been cancelled
Docs Build / Build docs site (push) Has been cancelled
Publish @openmaic packages / Build, validate & publish (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:03:23 +08:00

34 lines
1.1 KiB
TypeScript

import type { Scene, SceneType, QuizContent } from '@/lib/types/stage';
import { gradeChoiceQuestions } from '@/lib/quiz/grading';
export interface CompleteSummary {
countsByType: Partial<Record<SceneType, number>>;
quiz: { correct: number; total: number; pct: number } | null;
}
export type AnswerReader = (sceneId: string) => Record<string, string | string[]>;
export function summarizeScenes(scenes: Scene[], readAnswers: AnswerReader): CompleteSummary {
const countsByType: Partial<Record<SceneType, number>> = {};
for (const scene of scenes) {
countsByType[scene.type] = (countsByType[scene.type] ?? 0) + 1;
}
let correct = 0;
let total = 0;
for (const scene of scenes) {
if (scene.type !== 'quiz') continue;
const questions = (scene.content as QuizContent).questions ?? [];
const answers = readAnswers(scene.id);
const results = gradeChoiceQuestions(questions, answers);
for (const r of results) {
total += 1;
if (r.correct === true) correct += 1;
}
}
const quiz = total > 0 ? { correct, total, pct: Math.round((correct / total) * 100) } : null;
return { countsByType, quiz };
}