How samuel2770/guessing-game works
Purpose and Entry Points
This is a browser-based, real-time multiplayer guessing game. A Node.js process serves the static client and hosts Socket.io room/game coordination. Start with node server.js; users load the single-page client at public/index.html.
Core entry points:
• server.js — Express HTTP server, Socket.io server, in-memory session authority.
• public/index.html — application shell and UI panels.
• public/js/game.js — Socket.io client, user actions, state-driven rendering.
Server Authority and Session Boundary
server.js owns all mutable multiplayer state in process memory: rooms, players, Game Master assignment, rounds, guesses, scores, and timer lifecycle. Clients request actions but should receive authoritative state through server broadcasts.
Rooms are identified by shared four-letter codes. Socket.io rooms provide the delivery boundary: events and state updates target only participants in the relevant game session. State is non-durable: restarting the Node process removes active rooms and games.
Connection and Lobby Flow
The client connects through Socket.io from public/js/game.js. A user either emits session:create to establish a room and become Game Master, or session:join with room code/name to enter an existing room.
The server validates and mutates room membership in server.js, then emits session:update as the full synchronization mechanism. The client uses this snapshot to render lobby membership, role-specific controls, score/status information, and game visibility.
Round Lifecycle
The current Game Master supplies a question and secret answer, then requests game:start. server.js enforces round start conditions, including the required player count, initializes per-player attempts and round state, and starts the 60-second clock.
During the round, the server publishes session:tick every second and uses session:update for material state transitions. Only non-master players are eligible to guess; the client UI in public/js/game.js exposes controls according to server-provided role and phase.
Guess Resolution and Role Rotation
Players submit guesses through game:guess. Server logic in server.js compares guesses to the secret answer, decrements attempts for wrong answers, exposes incorrect guesses to the session, and determines terminal conditions: correct guess, timer expiry, or all eligible players eliminated.
A correct guess awards points and ends the round; otherwise the answer is revealed without points. At round completion, Game Master responsibility rotates to the next player, establishing the authorization boundary for configuring the following round.
Chat and Client Presentation
Lobby/session chat is sent with chat:send, processed and distributed by server.js, and rendered by public/js/game.js. The static UI is defined in public/index.html and styled by public/css/style.css.
public/js/smoke.js is a separate browser-side visual particle/animation layer; it is presentation-only and does not participate in game state or networking.
Runtime and Deployment
Runtime is Node.js with Express for HTTP/static delivery and Socket.io for bidirectional real-time transport, centered in server.js. The client is plain browser HTML/CSS/JavaScript with no separate frontend build/runtime indicated.
The server listens on PORT, defaulting to 3000. Deployment targets are generic Node-compatible hosts such as Railway, Render, Fly.io, or Heroku. Scaling beyond one process would require shared session state and Socket.io coordination, since current room/timer state is in memory.
Main entry points: server.js Socket.io gateway · index.html Application shell · game.js Game client · style.css Styles
Components 13
Browser client
- Application shell · Lobby, game, and chat panels.
public/index.html - Game client · Emits actions and renders authoritative session snapshots.
public/js/game.js - Styles
public/css/style.css - Smoke animation · Visual-only particle effects.
public/js/smoke.js
Node.js server
- Express static server · Serves the browser application.
server.js - Socket.io gateway · Connection and room-scoped event boundary.
server.js - Room session state · Owns rooms, players, roles, scores, and active round state; lost on restart.
server.js - Lobby membership · Creates and joins four-letter rooms, then broadcasts snapshots.
server.js - Round coordinator · Validates starts, initializes attempts, and rotates Game Master after completion.
server.js - 60-second timer · Publishes per-second ticks and ends expired rounds.
server.js - Guess resolver · Checks answers, consumes attempts, awards points, and reveals terminal answers.
server.js - Chat distributor
server.js - Node.js runtime · Listens on PORT, defaulting to 3000.
server.js
Connections 19
- Node.js runtime → Express static server: hosts
- Node.js runtime → Socket.io gateway: hosts
- Express static server → Application shell: serves
- Application shell → Game client: loads
- Application shell → Styles: loads
- Application shell → Smoke animation: loads
- Game client → Socket.io gateway: Socket.io events · session:create, session:join, game:start, game:guess, and chat:send.
- Socket.io gateway → Lobby membership: routes session commands
- Socket.io gateway → Round coordinator: routes game:start
- Socket.io gateway → Guess resolver: routes game:guess
- Socket.io gateway → Chat distributor: routes chat:send
- Lobby membership → Room session state: mutates membership
- Round coordinator → Room session state: initializes and completes rounds
- Round coordinator → 60-second timer: starts and stops
- Guess resolver → Room session state: updates attempts and scores
- 60-second timer → Room session state: updates remaining time
- Chat distributor → Room session state: targets room participants
- Room session state → Socket.io gateway: room-scoped broadcasts · session:update and session:tick are authoritative client synchronization.
- Socket.io gateway → Game client: authoritative updates