Overview
Pica y Fija uses a real-time server architecture built on Node.js, Express, and Socket.IO to enable multiplayer gameplay. The server manages room creation, player connections, game state, and turn-based logic.Technology Stack
Express
HTTP server for serving static assets and handling requests
Socket.IO
WebSocket library for real-time bidirectional communication
Node.js
Runtime environment for server-side JavaScript
Vite
Development server and build tool for the frontend
Server Initialization
The server setup combines Express for static file serving and Socket.IO for real-time communication:server.js
The server uses CORS with
origin: "*" to allow connections from any client. In production, you should restrict this to specific domains.Room Management System
The server maintains game state using in-memory data structures. Each room is identified by a unique code and contains all game-related information.Room Data Structure
server.js
Room Object Schema
Each room in therooms object has the following structure:
The Socket.IO socket instance of the player who created the room
Array containing the socket instances of connected players. Maximum of 2 players.
Maps player IDs to their secret 4-digit numbers:
{ 0: "1234", 1: "5678" }Indicates which player’s turn it is:
0 for player 1, 1 for player 2Tracks the turn number for each player:
[playerOneTurns, playerTwoTurns]Time limit for each turn in seconds
If
true, the room is added to the public rooms listIf
true, the host must approve players joining the roomSet to
true when a player guesses correctly (4 fijas)Room Code Generation
Room codes are generated as random 6-character alphanumeric strings:server.js
XK9P2L, A7B3QW
Game State Management
Player Assignment
When players join a room, they are assigned sequential IDs:server.js
roomCode and playerId for tracking:
Turn Management
Turns alternate between players after each valid guess:server.js
turn value switches between 0 and 1:
server.js
Game Logic: Picas y Fijas
The core game algorithm compares the guess against the secret number:server.js
Algorithm Explanation
Algorithm Explanation
- Fijas (Fixed): Digits that match both value and position
- Picas (Almost): Digits that exist in the secret but are in the wrong position
- A winning guess has
fijas === 4 - Each digit must be unique (validated on both secret submission and guess)
Win Condition
The game ends when a player achieves 4 fijas:server.js
Connection Lifecycle
1. Player Connects
2. Room Creation or Joining
Players either create a new room or join an existing one:server.js
3. Game Play
Players submit secrets, then alternate guessing until someone wins.4. Disconnection Handling
server.js
Alternative WebSocket Implementation
The project also includesserver2.js, a simpler WebSocket implementation using the ws library:
server2.js
- Supports only one game at a time (no room system)
- Uses native WebSocket instead of Socket.IO
- Has simpler message handling with JSON stringification
- Automatically resets state on disconnection
The main production server uses
server.js with Socket.IO for its advanced room management and scalability.Scalability Considerations
For production deployment at scale, consider:- Redis Adapter for Socket.IO to enable multi-server deployments
- Database persistence for game history and statistics
- Session recovery to handle temporary disconnections
- Rate limiting to prevent abuse of room creation