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:
@@ -314,6 +314,14 @@ router.post('/:id/games', authenticateToken, (req, res) => {
|
||||
return res.status(404).json({ error: 'Game not found' });
|
||||
}
|
||||
|
||||
// Stop monitors for currently-playing games before demoting them
|
||||
const previouslyPlaying = db.prepare(
|
||||
'SELECT id FROM session_games WHERE session_id = ? AND status = ?'
|
||||
).all(req.params.id, 'playing');
|
||||
for (const prev of previouslyPlaying) {
|
||||
try { stopMonitor(req.params.id, prev.id); } catch (_) {}
|
||||
}
|
||||
|
||||
// Set all current 'playing' games to 'played' (except skipped ones)
|
||||
db.prepare(`
|
||||
UPDATE session_games
|
||||
@@ -590,8 +598,17 @@ router.patch('/:sessionId/games/:gameId/status', authenticateToken, (req, res) =
|
||||
return res.status(400).json({ error: 'Invalid status. Must be playing, played, or skipped' });
|
||||
}
|
||||
|
||||
// If setting to 'playing', first set all other games in session to 'played' or keep as 'skipped'
|
||||
// If setting to 'playing', first stop monitors and demote other playing games
|
||||
if (status === 'playing') {
|
||||
const previouslyPlaying = db.prepare(
|
||||
'SELECT id FROM session_games WHERE session_id = ? AND status = ?'
|
||||
).all(sessionId, 'playing');
|
||||
for (const prev of previouslyPlaying) {
|
||||
if (String(prev.id) !== String(gameId)) {
|
||||
try { stopMonitor(sessionId, prev.id); } catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
db.prepare(`
|
||||
UPDATE session_games
|
||||
SET status = CASE
|
||||
|
||||
Reference in New Issue
Block a user