Fix host reconnection losing player state and first-answer bonus

- Persist firstCorrectPlayerId to backend during state sync
- Restore full players array (not just host) on host reconnect
- Add default values to WELCOME payload for new/unmatched players
- Add migration for first_correct_player_id column in game_sessions
This commit is contained in:
Joey Yakimowich-Payne 2026-01-14 10:04:51 -07:00
commit 035ea57274
No known key found for this signature in database
GPG key ID: 6BFE655FA5ABD1E1
4 changed files with 22 additions and 3 deletions

View file

@ -71,7 +71,8 @@ export const useGame = () => {
const gameConfigRef = useRef<GameConfig>(DEFAULT_GAME_CONFIG);
const gamePinRef = useRef<string | null>(null);
const hostSecretRef = useRef<string | null>(null);
const gameStateRef = useRef<GameState>('LANDING');
const gameStateRef = useRef<GameState>("LANDING");
const firstCorrectPlayerIdRef = useRef<string | null>(null);
useEffect(() => { timeLeftRef.current = timeLeft; }, [timeLeft]);
useEffect(() => { playersRef.current = players; }, [players]);
@ -81,6 +82,7 @@ export const useGame = () => {
useEffect(() => { gamePinRef.current = gamePin; }, [gamePin]);
useEffect(() => { hostSecretRef.current = hostSecret; }, [hostSecret]);
useEffect(() => { gameStateRef.current = gameState; }, [gameState]);
useEffect(() => { firstCorrectPlayerIdRef.current = firstCorrectPlayerId; }, [firstCorrectPlayerId]);
const generateGamePin = () => Math.floor(Math.random() * 900000) + 100000 + "";
@ -98,6 +100,7 @@ export const useGame = () => {
body: JSON.stringify({
gameState: gameStateRef.current,
currentQuestionIndex: currentQuestionIndexRef.current,
firstCorrectPlayerId: firstCorrectPlayerIdRef.current,
players: playersRef.current,
}),
});
@ -296,6 +299,7 @@ export const useGame = () => {
setQuiz(hostData.quiz);
setGameConfig(hostData.gameConfig);
setCurrentQuestionIndex(hostData.currentQuestionIndex || 0);
setFirstCorrectPlayerId(hostData.firstCorrectPlayerId || null);
const hostPlayer = (hostData.players || []).find((p: Player) => p.id === 'host');
if (hostPlayer) {
@ -303,7 +307,7 @@ export const useGame = () => {
setCurrentPlayerName('Host');
setCurrentPlayerScore(hostPlayer.score);
setCurrentStreak(hostPlayer.streak);
setPlayers([hostPlayer]);
setPlayers(hostData.players || []);
if (hostPlayer.lastAnswerCorrect !== null) {
setHasAnswered(true);