# samuel2770/guessing-game architecture

> Architecture diagram of the GitHub repository samuel2770/guessing-game, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/samuel2770/guessing-game
- Repository: https://github.com/samuel2770/guessing-game

## Overview

#### 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`](https://github.com/samuel2770/guessing-game/blob/main/server.js) (Socket.io gateway), [`public/index.html`](https://github.com/samuel2770/guessing-game/blob/main/public/index.html) (Application shell), [`public/js/game.js`](https://github.com/samuel2770/guessing-game/blob/main/public/js/game.js) (Game client), [`public/css/style.css`](https://github.com/samuel2770/guessing-game/blob/main/public/css/style.css) (Styles)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_browser["Browser client"]
  node_html["Application shell<br/>HTML UI<br/>[index.html]"]
  node_client["Game client<br/>browser controller<br/>[game.js]"]
  node_styles["Styles<br/>CSS<br/>[style.css]"]
  node_smoke["Smoke animation<br/>presentation layer<br/>[smoke.js]"]
end

subgraph group_server["Node.js server"]
  node_http["Express static server<br/>HTTP delivery<br/>[server.js]"]
  node_socket{{"Socket.io gateway<br/>real-time transport<br/>[server.js]"}}
  node_sessions[("Room session state<br/>in-memory authority<br/>[server.js]")]
  node_lobby["Lobby membership<br/>session commands<br/>[server.js]"]
  node_rounds["Round coordinator<br/>game lifecycle<br/>[server.js]"]
  node_timer["60-second timer<br/>round clock<br/>[server.js]"]
  node_guesses["Guess resolver<br/>round rules<br/>[server.js]"]
  node_chat["Chat distributor<br/>session messaging<br/>[server.js]"]
  node_node["Node.js runtime<br/>process runtime<br/>[server.js]"]
end

node_node -->|"hosts"| node_http
node_node -->|"hosts"| node_socket
node_http -->|"serves"| node_html
node_html -->|"loads"| node_client
node_html -->|"loads"| node_styles
node_html -->|"loads"| node_smoke
node_client -->|"Socket.io events"| node_socket
node_socket -->|"routes session commands"| node_lobby
node_socket -->|"routes game:start"| node_rounds
node_socket -->|"routes game:guess"| node_guesses
node_socket -->|"routes chat:send"| node_chat
node_lobby -->|"mutates membership"| node_sessions
node_rounds -->|"initializes and completes rounds"| node_sessions
node_rounds -->|"starts and stops"| node_timer
node_guesses -->|"updates attempts and scores"| node_sessions
node_timer -->|"updates remaining time"| node_sessions
node_chat -->|"targets room participants"| node_sessions
node_sessions -->|"room-scoped broadcasts"| node_socket
node_socket -->|"authoritative updates"| node_client

click node_html "https://github.com/samuel2770/guessing-game/blob/main/public/index.html"
click node_client "https://github.com/samuel2770/guessing-game/blob/main/public/js/game.js"
click node_styles "https://github.com/samuel2770/guessing-game/blob/main/public/css/style.css"
click node_smoke "https://github.com/samuel2770/guessing-game/blob/main/public/js/smoke.js"
click node_http "https://github.com/samuel2770/guessing-game/blob/main/server.js"
click node_socket "https://github.com/samuel2770/guessing-game/blob/main/server.js"
click node_sessions "https://github.com/samuel2770/guessing-game/blob/main/server.js"
click node_lobby "https://github.com/samuel2770/guessing-game/blob/main/server.js"
click node_rounds "https://github.com/samuel2770/guessing-game/blob/main/server.js"
click node_timer "https://github.com/samuel2770/guessing-game/blob/main/server.js"
click node_guesses "https://github.com/samuel2770/guessing-game/blob/main/server.js"
click node_chat "https://github.com/samuel2770/guessing-game/blob/main/server.js"
click node_node "https://github.com/samuel2770/guessing-game/blob/main/server.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_html,node_client,node_styles,node_smoke toneBlue
class node_http,node_socket,node_sessions,node_lobby,node_rounds,node_timer,node_guesses,node_chat,node_node toneAmber
```

## Components

### Browser client

- [Application shell](https://github.com/samuel2770/guessing-game/blob/main/public/index.html): HTML UI · Lobby, game, and chat panels. · `public/index.html`
- [Game client](https://github.com/samuel2770/guessing-game/blob/main/public/js/game.js): browser controller · Emits actions and renders authoritative session snapshots. · `public/js/game.js`
- [Styles](https://github.com/samuel2770/guessing-game/blob/main/public/css/style.css): CSS · `public/css/style.css`
- [Smoke animation](https://github.com/samuel2770/guessing-game/blob/main/public/js/smoke.js): presentation layer · Visual-only particle effects. · `public/js/smoke.js`

### Node.js server

- [Express static server](https://github.com/samuel2770/guessing-game/blob/main/server.js): HTTP delivery · Serves the browser application. · `server.js`
- [Socket.io gateway](https://github.com/samuel2770/guessing-game/blob/main/server.js): real-time transport · Connection and room-scoped event boundary. · `server.js`
- [Room session state](https://github.com/samuel2770/guessing-game/blob/main/server.js): in-memory authority · Owns rooms, players, roles, scores, and active round state; lost on restart. · `server.js`
- [Lobby membership](https://github.com/samuel2770/guessing-game/blob/main/server.js): session commands · Creates and joins four-letter rooms, then broadcasts snapshots. · `server.js`
- [Round coordinator](https://github.com/samuel2770/guessing-game/blob/main/server.js): game lifecycle · Validates starts, initializes attempts, and rotates Game Master after completion. · `server.js`
- [60-second timer](https://github.com/samuel2770/guessing-game/blob/main/server.js): round clock · Publishes per-second ticks and ends expired rounds. · `server.js`
- [Guess resolver](https://github.com/samuel2770/guessing-game/blob/main/server.js): round rules · Checks answers, consumes attempts, awards points, and reveals terminal answers. · `server.js`
- [Chat distributor](https://github.com/samuel2770/guessing-game/blob/main/server.js): session messaging · `server.js`
- [Node.js runtime](https://github.com/samuel2770/guessing-game/blob/main/server.js): process runtime · Listens on PORT, defaulting to 3000. · `server.js`

## Connections

- 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
