fix: Resolve React hooks violation, implement player waiting screen, and auto-start game upon deck submission.

This commit is contained in:
2025-12-14 22:41:26 +01:00
parent 9ff305f1ba
commit 65824a52d9
7 changed files with 190 additions and 53 deletions

View File

@@ -104,16 +104,6 @@ io.on('connection', (socket) => {
}
});
socket.on('pick_card', ({ roomId, cardId }) => {
// Find player from socket? Actually we trust clientId sent or inferred (but simpler to trust socket for now if we tracked map, but here just use helper?)
// We didn't store socket->player map here globally. We'll pass playerId in payload for simplicity but validation later.
// Wait, let's look at signature.. pickCard(roomId, playerId, cardId)
// Need playerId. Let's ask client to send it.
// Or we can find it if we know connection...
// Let's assume payload: { roomId, playerId, cardId }
});
// Revised pick_card to actual impl
socket.on('pick_card', ({ roomId, playerId, cardId }) => {
const draft = draftManager.pickCard(roomId, playerId, cardId);
@@ -131,6 +121,45 @@ io.on('connection', (socket) => {
}
});
socket.on('player_ready', ({ roomId, playerId, deck }) => {
const room = roomManager.setPlayerReady(roomId, playerId, deck);
if (room) {
io.to(roomId).emit('room_update', room);
// Check if all active players are ready
const activePlayers = room.players.filter(p => p.role === 'player');
if (activePlayers.length > 0 && activePlayers.every(p => p.ready)) {
console.log(`All players ready in room ${roomId}. Starting game...`);
room.status = 'playing';
io.to(roomId).emit('room_update', room);
// Initialize Game
const game = gameManager.createGame(roomId, room.players);
// Load decks
activePlayers.forEach(p => {
if (p.deck) {
p.deck.forEach((card: any) => {
gameManager.addCardToGame(roomId, {
ownerId: p.id,
controllerId: p.id,
oracleId: card.oracle_id || card.id,
name: card.name,
// Prioritize 'image' property which might hold the cached URL
imageUrl: card.image || card.image_uris?.normal || card.card_faces?.[0]?.image_uris?.normal || "",
zone: 'library'
});
});
// TODO: Shuffle library
}
});
io.to(roomId).emit('game_update', game);
}
}
});
socket.on('start_game', ({ roomId, decks }) => {
const room = roomManager.startGame(roomId);
if (room) {