Add time sync

This commit is contained in:
Joey Yakimowich-Payne 2026-01-13 22:56:56 -07:00
commit 846ba2a69c
No known key found for this signature in database
GPG key ID: 6BFE655FA5ABD1E1
2 changed files with 13 additions and 1 deletions

View file

@ -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');
}

View file

@ -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[] } };