Skip to content

Commit

Permalink
Fix timer does not stop if game has ended (closes #45)
Browse files Browse the repository at this point in the history
  • Loading branch information
SSoelvsten committed Dec 2, 2022
1 parent 2019b37 commit 9fb3fce
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions game/src/game-screen/game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ const Game = ({ instance: { anagrams }, difficulty, language, accScore, round, o
<>
<div className="Game" tabIndex={0} onKeyDown={onKey} ref={divRef}>
<ScoreBoard endTime={endTime}
gameEnd={gameEnd}
language={language}
qualified={qualified}
score={accScore + currScore}
Expand Down
11 changes: 6 additions & 5 deletions game/src/game-screen/scoreboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import './scoreboard.scss';

export interface ScoreBoardProps {
endTime: number;
gameEnd: boolean;
language: Language;
qualified: boolean;
score: number;
round: number;
onTimeout: () => void;
};

const ScoreBoard = ({ endTime, language, qualified, round, score, onTimeout }: ScoreBoardProps) => {
const ScoreBoard = ({ endTime, gameEnd, language, qualified, round, score, onTimeout }: ScoreBoardProps) => {
const isTimed: boolean = endTime !== Infinity && !isNaN(endTime);

// ------------------------------------------------------------------------
Expand All @@ -41,21 +42,21 @@ const ScoreBoard = ({ endTime, language, qualified, round, score, onTimeout }: S

// TODO: stop timer update when 'won'.
useEffect(() => {
if (!isTimed) return;
if (!isTimed || gameEnd) return;

const timerId = setInterval(() => {
const tick = new Date().getTime();
setCurrTime(tick);
}, 50);
return () => clearInterval(timerId);
}, [isTimed]);
}, [isTimed, gameEnd]);

useEffect(() => {
if (!isTimed) return;
if (!isTimed || gameEnd) return;

const timeLeft = endTime - currTime;
if (timeLeft < 0) onTimeout();
}, [isTimed, currTime, endTime, onTimeout]);
}, [isTimed, gameEnd, currTime, endTime, onTimeout]);

const timeLeft = endTime - currTime;
const timeAlarm: boolean = 0 < timeLeft && timeLeft < 10 * 1000;
Expand Down

0 comments on commit 9fb3fce

Please sign in to comment.