diff --git a/hooks/useGame.ts b/hooks/useGame.ts index 690b1a9..259cc53 100644 --- a/hooks/useGame.ts +++ b/hooks/useGame.ts @@ -265,10 +265,16 @@ export const useGame = () => { } if (timerRef.current) clearInterval(timerRef.current); + let tickCount = 0; timerRef.current = setInterval(() => { setTimeLeft(prev => { if (prev <= 100) { endQuestion(); return 0; } - return prev - 100; + const newTime = prev - 100; + tickCount++; + if (tickCount % 10 === 0) { + broadcast({ type: 'TIME_SYNC', payload: { timeLeft: newTime } }); + } + return newTime; }); }, 100); }; @@ -372,7 +378,12 @@ export const useGame = () => { } } + if (data.type === 'TIME_SYNC') { + setTimeLeft(data.payload.timeLeft); + } + if (data.type === 'TIME_UP') { + if (timerRef.current) clearInterval(timerRef.current); setGameState('REVEAL'); } diff --git a/types.ts b/types.ts index c9fc7c7..c9eb4e6 100644 --- a/types.ts +++ b/types.ts @@ -94,6 +94,7 @@ export type NetworkMessage = } | { type: 'ANSWER'; payload: { playerId: string; isCorrect: boolean } } | { type: 'RESULT'; payload: { isCorrect: boolean; scoreAdded: number; newScore: number } } + | { type: 'TIME_SYNC'; payload: { timeLeft: number } } | { type: 'TIME_UP'; payload: {} } | { type: 'SHOW_SCOREBOARD'; payload: { players: Player[] } } | { type: 'GAME_OVER'; payload: { players: Player[] } }; \ No newline at end of file