refactor: improve game start logic to ensure all active players are ready before starting
All checks were successful
Build and Push Docker Image / docker (push) Successful in 8s
All checks were successful
Build and Push Docker Image / docker (push) Successful in 8s
This commit is contained in:
@@ -145,9 +145,12 @@ export function setupWebSocket(server) {
|
||||
if (!player.roomId) return; const room = rooms.get(player.roomId); if (!room) return; room.players.delete(player.id); player.roomId = null; if (room.state.ready) delete room.state.ready[player.id]; if (room.state.spectators) delete room.state.spectators[player.id]; if (room.players.size === 0) rooms.delete(room.id); else broadcast(room, 'room_update', { room: roomSummary(room) }); return; }
|
||||
if (msg.type === 'set_ready') { const room = rooms.get(player.roomId); if (!room) return; const value = !!msg.ready; room.state.ready[player.id] = value; broadcast(room, 'room_update', { room: roomSummary(room) }); return; }
|
||||
if (msg.type === 'start_game') {
|
||||
const room = rooms.get(player.roomId); if (!room) return; if (room.hostId !== player.id) return send('error', { message: 'Only host can start' });
|
||||
const pids = [...room.players.values()].filter(p => !room.state.spectators?.[p.id]).map(p => p.id);
|
||||
const allReady = pids.every((pid) => !!room.state.ready?.[pid]); if (!allReady) return send('error', { message: 'All players must be ready' });
|
||||
const room = rooms.get(player.roomId); if (!room) return;
|
||||
if (room.hostId !== player.id) return send('error', { message: 'Only host can start' });
|
||||
const active = [...room.players.values()].filter(p => !room.state.spectators?.[p.id] && p.connected);
|
||||
const allReady = active.length>0 && active.every(p => !!room.state.ready?.[p.id]);
|
||||
if (!allReady) return send('error', { message: 'All active players must be ready' });
|
||||
const pids = active.map(p => p.id);
|
||||
room.state.status = 'playing'; room.state.turnOrder = shuffle(pids); room.state.currentGuesser = room.state.turnOrder[0];
|
||||
room.state.timeline = Object.fromEntries(room.state.turnOrder.map((pid) => [pid, []]));
|
||||
room.state.tokens = Object.fromEntries(room.state.turnOrder.map((pid) => [pid, 2]));
|
||||
|
||||
Reference in New Issue
Block a user