fix: enforce single playing game and clean up stale shard monitors
Mark games as 'played' when shard detects game.ended or room_closed. Stop old shard monitors before demoting previous playing games on new game add or status change. Sync frontend playingGame state with the games list on every refresh to prevent stale UI. Use terminate() for probe connections to prevent shard connection leaks. Made-with: Cursor
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useEffect, useCallback } from 'react';
|
||||
import React, { useState, useEffect, useCallback, useRef } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useAuth } from '../context/AuthContext';
|
||||
import api from '../api/axios';
|
||||
@@ -952,17 +952,33 @@ function SessionInfo({ sessionId, onGamesUpdate, playingGame, setPlayingGame })
|
||||
const [editingPlayerCount, setEditingPlayerCount] = useState(null);
|
||||
const [newPlayerCount, setNewPlayerCount] = useState('');
|
||||
|
||||
const playingGameRef = useRef(playingGame);
|
||||
playingGameRef.current = playingGame;
|
||||
|
||||
const loadGames = useCallback(async () => {
|
||||
try {
|
||||
const response = await api.get(`/sessions/${sessionId}/games`);
|
||||
// Reverse chronological order (most recent first)
|
||||
setGames(response.data.reverse());
|
||||
const freshGames = response.data;
|
||||
setGames(freshGames.reverse());
|
||||
|
||||
const currentPlaying = freshGames.find(g => g.status === 'playing');
|
||||
const prev = playingGameRef.current;
|
||||
if (currentPlaying) {
|
||||
if (!prev || prev.id !== currentPlaying.id || prev.player_count !== currentPlaying.player_count) {
|
||||
setPlayingGame(currentPlaying);
|
||||
}
|
||||
} else if (prev) {
|
||||
const still = freshGames.find(g => g.id === prev.id);
|
||||
if (!still || still.status !== 'playing') {
|
||||
setPlayingGame(null);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to load session games');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [sessionId]);
|
||||
}, [sessionId, setPlayingGame]);
|
||||
|
||||
useEffect(() => {
|
||||
loadGames();
|
||||
|
||||
Reference in New Issue
Block a user