Overview
Pica y Fija uses a room-based multiplayer system where players create or join rooms to start a game. Each room supports exactly 2 players and can be configured with custom settings like turn timers and visibility.Creating a Room
Room Configuration
When creating a room, the host can configure three key settings:Turn Timer
Set the duration (in seconds) each player has to make a guess
Public/Private
Control whether the room appears in public room searches
Approval Required
Require host approval for players joining (future feature)
Client-Side Room Creation
Players create rooms through the lobby interface:index.html
Server-Side Room Creation
The server handles room creation by generating a unique code and initializing the room state:server.js
Room codes are 6-character alphanumeric strings generated using
Math.random().toString(36).substring(2, 8).toUpperCase()Joining a Room
Players can join existing rooms in two ways:Method 1: Join by Room Code
If you have a specific room code, you can join directly:index.html
server.js
Method 2: Search for Public Rooms
Players can automatically join an available public room:index.html
server.js
Room Lifecycle
Joining Process
When a second player joins, thejoinRoom function handles initialization:
server.js
Room Ready Event
When both players are connected, each receives thesalaLista event:
index.html
Player Disconnection
Rooms are automatically cleaned up when players disconnect:server.js
Room State Structure
Room State Structure
Each room object contains:
host: Socket reference to the room creatorplayers: Array of socket references (max 2)secrets: Object mapping player IDs to their secret numbersturn: Current player’s turn (0 or 1)turnCounts: Array tracking turn numbers for each playeroptions: Room configuration (tiempoTurno, publica, requiereAprobacion)gameOver: Boolean flag indicating if the game has ended
Socket.IO Events
Client → Server
| Event | Payload | Description |
|---|---|---|
crearSala | { tiempoTurno, publica, requiereAprobacion } | Creates a new room with settings |
buscarSala | None | Searches for an available public room |
unirseSala | codigo (string) | Joins a room by code |
Server → Client
| Event | Payload | Description |
|---|---|---|
salaCreada | { codigo } | Confirms room creation with code |
salaLista | { jugador, sala, tiempoTurno } | Notifies when room is full and ready |
info | message (string) | General information messages |
error | message (string) | Error messages |
Best Practices
Room Code Validation
Always validate room codes on both client and server to prevent invalid join attempts
Capacity Limits
Enforce the 2-player limit strictly to prevent room overflow
Cleanup
Remove empty rooms and update public room lists on disconnection
Player ID Assignment
Host always gets ID 0, joiner gets ID 1 for consistent game logic