diff --git a/app/(game)/play/PlayClient.tsx b/app/(game)/play/PlayClient.tsx
index ca3db0c..05e9736 100644
--- a/app/(game)/play/PlayClient.tsx
+++ b/app/(game)/play/PlayClient.tsx
@@ -9,6 +9,7 @@ import ScoreSummary from "@/components/game/ScoreSummary";
import StageIndicator from "@/components/game/StageIndicator";
import KatexRenderer from "@/components/math/KatexRenderer";
import { useGameStore } from "@/store/gameStore";
+import type { ProblemCount } from "@/store/gameStore";
function parseAnswer(value: string) {
const trimmed = value.trim();
@@ -18,8 +19,16 @@ function parseAnswer(value: string) {
return Number(trimmed);
}
-export default function PlayClient() {
+interface PlayClientProps {
+ problemCount: ProblemCount;
+}
+
+export default function PlayClient({ problemCount }: PlayClientProps) {
const session = useGameStore((state) => state.session);
+ const selectedProblemCount = useGameStore(
+ (state) => state.selectedProblemCount,
+ );
+ const setProblemCount = useGameStore((state) => state.setProblemCount);
const initSession = useGameStore((state) => state.initSession);
const submitAnswer = useGameStore((state) => state.submitAnswer);
const nextProblem = useGameStore((state) => state.nextProblem);
@@ -34,15 +43,28 @@ export default function PlayClient() {
const isComplete = Boolean(
session && session.currentIndex >= session.problems.length,
);
+ const sessionNeedsInit =
+ session === null || session.problems.length !== problemCount;
const correctCount =
session?.results.filter((result) => result?.correct).length ?? 0;
const totalCount = session?.problems.length ?? 0;
useEffect(() => {
- if (session === null) {
- initSession();
+ if (selectedProblemCount !== problemCount) {
+ setProblemCount(problemCount);
}
- }, [initSession, session]);
+
+ if (sessionNeedsInit) {
+ initSession(problemCount);
+ }
+ }, [
+ initSession,
+ problemCount,
+ selectedProblemCount,
+ session,
+ sessionNeedsInit,
+ setProblemCount,
+ ]);
useEffect(() => {
setAnswerA("");
@@ -67,7 +89,7 @@ export default function PlayClient() {
submitAnswer({ a: parsedA, b: parsedB });
}
- if (!session) {
+ if (sessionNeedsInit) {
return (
이번 단계에서는 화면 이동만 확인합니다.
diff --git a/store/gameStore.ts b/store/gameStore.ts index 344bf36..abbfe88 100644 --- a/store/gameStore.ts +++ b/store/gameStore.ts @@ -16,17 +16,21 @@ export interface GameSession { results: (GradeResult | null)[]; } +export type ProblemCount = 5 | 10; + interface GameState { session: GameSession | null; - initSession: (count?: number) => void; + selectedProblemCount: ProblemCount; + setProblemCount: (count: ProblemCount) => void; + initSession: (count?: ProblemCount) => void; submitAnswer: (answer: UserAnswer) => void; nextProblem: () => void; resetSession: () => void; } -const DEFAULT_SESSION_COUNT = 10; +const DEFAULT_SESSION_COUNT: ProblemCount = 10; -function createSession(count = DEFAULT_SESSION_COUNT): GameSession { +function createSession(count: ProblemCount = DEFAULT_SESSION_COUNT): GameSession { const problems = generateSession(count); return { @@ -38,8 +42,14 @@ function createSession(count = DEFAULT_SESSION_COUNT): GameSession { export const useGameStore = create