Add time sync
This commit is contained in:
parent
028bab23fd
commit
846ba2a69c
2 changed files with 13 additions and 1 deletions
|
|
@ -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');
|
||||
}
|
||||
|
||||
|
|
|
|||
1
types.ts
1
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[] } };
|
||||
Loading…
Add table
Add a link
Reference in a new issue