working on useScore

This commit is contained in:
2025-10-15 18:28:55 +01:00
parent 5f3e3706fa
commit 2dd317e0dc
6 changed files with 128 additions and 47 deletions
+25 -2
View File
@@ -3,6 +3,9 @@ import { useTimer } from "./hooks/useTimer";
import { useScoreTime } from "./useScoreTime";
import type { GameStatus } from "../types";
const TIME_LIMIT_MS = 60000; // 60 seconds
const KEYSTROKE_LIMIT = 100;
type GameScoreContext = {
gameStatus: GameStatus;
setGameStatus: (status: GameStatus) => void;
@@ -10,17 +13,35 @@ type GameScoreContext = {
type UseScoreParams = {
gameContext: GameScoreContext;
keystrokeCount: number;
};
export function useScore({ gameContext }: UseScoreParams) {
export function useScore({ gameContext, keystrokeCount }: UseScoreParams) {
const timer = useTimer();
const { gameStatus } = gameContext;
const { gameStatus, setGameStatus } = gameContext;
const { timeValue, startTimer, stopTimer, resetTimer } = timer;
const [finalScore, setFinalScore] = useState<number | null>(null);
useScoreTime({ gameStatus, timer });
// Check for game-over conditions
useEffect(() => {
if (gameStatus !== "started") return;
// Time limit exceeded
if (timeValue >= TIME_LIMIT_MS) {
setGameStatus("game-over");
return;
}
// Keystroke limit exceeded
if (keystrokeCount >= KEYSTROKE_LIMIT) {
setGameStatus("game-over");
return;
}
}, [gameStatus, timeValue, keystrokeCount, setGameStatus]);
useEffect(() => {
if (gameStatus !== "game-won") return;
@@ -38,6 +59,8 @@ export function useScore({ gameContext }: UseScoreParams) {
stopTimer,
resetTimer,
finalScore,
timeLimit: TIME_LIMIT_MS,
keystrokeLimit: KEYSTROKE_LIMIT,
};
}