Pica y Fija implements a turn-based system where players alternate making guesses. Each turn has a configurable time limit, and the system automatically switches turns when time expires or a guess is made.
let countdown = null;let timeLeft = 60;let tiempoTurnoConfigurado = 60;function startTimer() { timeLeft = tiempoTurnoConfigurado; updateTimer(); countdown = setInterval(() => { timeLeft--; updateTimer(); if (timeLeft <= 0) { stopTimer(); guessInput.disabled = true; guessBtn.disabled = true; log.innerText += '⏰ Se acabó el tiempo del turno. Pasando turno...\n'; socket.emit('pasarTurnoPorTiempo'); } }, 1000);}function stopTimer() { clearInterval(countdown); timer.innerText = '';}function updateTimer() { timer.innerText = `⏳ Tiempo restante: ${timeLeft}s`;}
The timer runs on the client side, which means it’s vulnerable to manipulation. In a production environment, consider implementing server-side timer validation.
When time runs out, the client sends a turn-skip event:
server.js
socket.on('pasarTurnoPorTiempo', () => { const room = rooms[socket.roomCode]; if (!room) return; // Only the active player can force a turn skip if (room.turn !== socket.playerId) return; // Switch to the other player room.turn = room.turn === 0 ? 1 : 0; broadcastTurn(room);});
The server validates that only the player with the active turn can trigger pasarTurnoPorTiempo, preventing malicious turn skipping.
if (room.turn !== socket.playerId) { socket.emit('error', 'No es tu turno'); return;}if (room.gameOver) { socket.emit('error', 'El juego ya terminó'); return;}
Turn Order
Only the player whose playerId matches room.turn can make guesses
Game State
Once gameOver is true, no more guesses are accepted
This sequence shows how turns flow between players, including a timeout scenario where Player 2’s timer expires and the turn automatically passes back to Player 1.