# metal-code/drawzy architecture

> Architecture diagram of the GitHub repository metal-code/drawzy, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/metal-code/drawzy
- Repository: https://github.com/metal-code/drawzy

## Overview

1. Drawzy is a two-part monorepo: a Node.js backend in `backend/` and a React/Vite frontend in `frontend/`. The backend exposes both REST APIs and a Socket.io server on the same HTTP process; the frontend talks to both.

2. The backend HTTP layer is organized conventionally around `routes/ -> controllers/ -> models/`, with shared middleware and utilities. `app.js` wires Express middleware and route registration, while `index.js` boots the server and attaches Socket.io.

3. Persistent data lives in SQLite via `better-sqlite3`, with schema initialization in `src/db/schema.js`. The database stores users, groups, group membership, and group match history; it also backs leaderboard stats like total points, games played, and games won.

4. Authentication is separate from gameplay. `auth.controller.js` plus `auth.middleware.js` implement register/login/logout/me flows, using JWTs in HTTP-only cookies. This supports both registered users and guest players, but only registered users are persisted in the database and leaderboard.

5. Group management is a distinct backend domain. `group.controller.js`, `group.routes.js`, and `group.model.js` handle group creation, invite-code joining, member listing, and match history. Groups are also tied to a persistent Socket.io channel so online members can be notified when a group game starts.

6. The leaderboard is read-only from the frontend perspective and is served over HTTP by `leaderboard.controller.js`. Its source of truth is accumulated player stats in SQLite; guests are excluded.

7. Real-time gameplay is isolated in `src/socket/`, split by responsibility: `game.js` for the turn/state machine, `room.js` for room lifecycle and host migration, `draw.js` for canvas stroke relay, `chat.js` for guesses and scoring, and `index.js` for event registration. This is the core architecture boundary of the app.

8. Game state is intentionally in-memory on the server during an active match. Rooms, players, scores, strokes, timers, and word choices are held in JavaScript objects and broadcast over Socket.io; the database is not touched during normal turn-by-turn play. Persistence happens mainly at game end, and for group-related writes.

9. AI word generation is a backend service boundary in `src/services/words.service.js`. Words are generated before a match starts, using OpenRouter through the OpenAI SDK, so no API calls happen during active rounds. If generation fails, the backend falls back to curated word lists.

10. The frontend is a routed single-page app with page-level screens in `src/pages/` and reusable gameplay UI in `src/components/`. `AuthContext`, `RoomContext`, `SocketContext`, and `ToastContext` provide cross-app state, while `utils/api.js` and `utils/socket.js` centralize REST and Socket.io client setup.

11. The main client-side flow is: landing/login -> lobby/group/room setup -> game screen -> post-game results. The `Game.jsx` page combines the canvas, chat, timer, scoreboard, and word picker components, while socket events drive most live state updates.

12. Competitive mode is implemented as a gameplay feature on top of the socket layer, not as a separate subsystem. The client detects tab switching with browser visibility/blur signals, reports it to the server, and the server rebroadcasts alerts and counts to all players in the room.

13. Operationally, the repo is built for split deployment: backend on Render, frontend on Vercel, with GitHub Actions automating both. The frontend points to the backend through explicit base/socket URLs, and the backend’s SQLite file persists on the Render disk.

14. For an architecture diagram, the key boundaries are: React UI and contexts on the client, REST controllers for account/group/leaderboard data, Socket.io for the live game loop, SQLite for durable user/group stats, and an in-memory room state layer that sits between sockets and persistence.

Main entry points: [`backend/src/app.js`](https://github.com/metal-code/drawzy/blob/main/backend/src/app.js) (Express app), [`backend/src/socket/index.js`](https://github.com/metal-code/drawzy/blob/main/backend/src/socket/index.js) (Socket entry), [`backend/src/db/schema.js`](https://github.com/metal-code/drawzy/blob/main/backend/src/db/schema.js) (Schema init), [`backend/src/index.js`](https://github.com/metal-code/drawzy/blob/main/backend/src/index.js) (Server boot)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Frontend"]
  node_landing["Landing<br/>page<br/>[Landing.jsx]"]
  node_lobby["Lobby<br/>page<br/>[Lobby.jsx]"]
  node_groups["Groups<br/>page<br/>[Groups.jsx]"]
  node_group_detail["Group detail<br/>page<br/>[GroupDetail.jsx]"]
  node_game_page["Game<br/>page<br/>[Game.jsx]"]
  node_postgame["Post-game<br/>page<br/>[PostGame.jsx]"]
  node_leaderboard_page["Leaderboard<br/>page<br/>[Leaderboard.jsx]"]
  node_auth_ctx(("Auth context<br/>client state<br/>[AuthContext.jsx]"))
  node_room_ctx(("Room context<br/>client state<br/>[RoomContext.jsx]"))
  node_socket_ctx(("Socket context<br/>client state<br/>[SocketContext.jsx]"))
  node_api_client["API client<br/>rest client<br/>[api.js]"]
  node_socket_client["Socket client<br/>[socket.js]"]
  node_app["App<br/>router shell<br/>[App.jsx]"]
end

subgraph group_server["Backend"]
  node_api_app["Express app<br/>web app<br/>[app.js]"]
  node_server_boot["Server boot<br/>http+socket bootstrap<br/>[index.js]"]
  node_auth_controller["Auth<br/>controller<br/>[auth.controller.js]"]
  node_group_controller["Groups API<br/>controller"]
  node_leaderboard_controller["Leaderboard API<br/>controller"]
  node_socket_index["Socket entry<br/>socket registry<br/>[index.js]"]
  node_game_socket["Game state<br/>socket state machine<br/>[game.js]"]
  node_room_socket["Room lifecycle<br/>socket room manager<br/>[room.js]"]
  node_draw_socket["Draw relay<br/>socket relay<br/>[draw.js]"]
  node_chat_socket["Chat & scoring<br/>socket scoring<br/>[chat.js]"]
  node_words_service["Word service<br/>ai service<br/>[words.service.js]"]
end

subgraph group_data["Persistence"]
  node_db_schema[("Schema init<br/>sqlite schema<br/>[schema.js]")]
  node_user_model["Users<br/>model<br/>[user.model.js]"]
  node_group_model["Groups<br/>model<br/>[group.model.js]"]
  node_score_model["Scores<br/>model<br/>[score.model.js]"]
end

subgraph group_infra["Ops"]
  node_deploy["Deploy workflow<br/>ci pipeline<br/>[deploy.yml]"]
end

node_app -->|"renders"| node_api_app
node_landing -->|"navigates"| node_lobby
node_lobby -->|"enters"| node_game_page
node_groups -->|"opens"| node_group_detail
node_game_page -->|"finishes"| node_postgame
node_auth_ctx -->|"uses"| node_api_client
node_room_ctx -->|"uses"| node_socket_ctx
node_socket_ctx -->|"provides"| node_socket_client
node_api_client -->|"calls"| node_api_app
node_socket_client -->|"connects"| node_server_boot
node_server_boot -->|"hosts"| node_api_app
node_api_app -->|"routes to"| node_auth_controller
node_api_app -->|"routes to"| node_group_controller
node_api_app -->|"routes to"| node_leaderboard_controller
node_auth_controller -->|"reads/writes"| node_user_model
node_group_controller -->|"reads/writes"| node_group_model
node_leaderboard_controller -->|"reads"| node_score_model
node_api_app -->|"initializes"| node_db_schema
node_db_schema -->|"supports"| node_user_model
node_db_schema -->|"supports"| node_group_model
node_db_schema -->|"supports"| node_score_model
node_server_boot -->|"registers"| node_socket_index
node_socket_index -->|"loads"| node_game_socket
node_socket_index -->|"loads"| node_room_socket
node_socket_index -->|"loads"| node_draw_socket
node_socket_index -->|"loads"| node_chat_socket
node_game_socket -->|"coordinates"| node_room_socket
node_game_socket -->|"requests words"| node_words_service
node_chat_socket -->|"persists end stats"| node_score_model
node_group_controller -->|"notifies"| node_socket_index
node_deploy -->|"deploys"| node_api_app
node_deploy -->|"deploys"| node_app

click node_landing "https://github.com/metal-code/drawzy/blob/main/frontend/src/pages/Landing.jsx"
click node_lobby "https://github.com/metal-code/drawzy/blob/main/frontend/src/pages/Lobby.jsx"
click node_groups "https://github.com/metal-code/drawzy/blob/main/frontend/src/pages/Groups.jsx"
click node_group_detail "https://github.com/metal-code/drawzy/blob/main/frontend/src/pages/GroupDetail.jsx"
click node_game_page "https://github.com/metal-code/drawzy/blob/main/frontend/src/pages/Game.jsx"
click node_postgame "https://github.com/metal-code/drawzy/blob/main/frontend/src/pages/PostGame.jsx"
click node_leaderboard_page "https://github.com/metal-code/drawzy/blob/main/frontend/src/pages/Leaderboard.jsx"
click node_auth_ctx "https://github.com/metal-code/drawzy/blob/main/frontend/src/context/AuthContext.jsx"
click node_room_ctx "https://github.com/metal-code/drawzy/blob/main/frontend/src/context/RoomContext.jsx"
click node_socket_ctx "https://github.com/metal-code/drawzy/blob/main/frontend/src/context/SocketContext.jsx"
click node_api_client "https://github.com/metal-code/drawzy/blob/main/frontend/src/utils/api.js"
click node_socket_client "https://github.com/metal-code/drawzy/blob/main/frontend/src/utils/socket.js"
click node_app "https://github.com/metal-code/drawzy/blob/main/frontend/src/App.jsx"
click node_api_app "https://github.com/metal-code/drawzy/blob/main/backend/src/app.js"
click node_server_boot "https://github.com/metal-code/drawzy/blob/main/backend/src/index.js"
click node_auth_controller "https://github.com/metal-code/drawzy/blob/main/backend/src/controllers/auth.controller.js"
click node_group_controller "https://github.com/metal-code/drawzy/blob/main/backend/src/controllers/group.controller.js"
click node_leaderboard_controller "https://github.com/metal-code/drawzy/blob/main/backend/src/controllers/leaderboard.controller.js"
click node_socket_index "https://github.com/metal-code/drawzy/blob/main/backend/src/socket/index.js"
click node_game_socket "https://github.com/metal-code/drawzy/blob/main/backend/src/socket/game.js"
click node_room_socket "https://github.com/metal-code/drawzy/blob/main/backend/src/socket/room.js"
click node_draw_socket "https://github.com/metal-code/drawzy/blob/main/backend/src/socket/draw.js"
click node_chat_socket "https://github.com/metal-code/drawzy/blob/main/backend/src/socket/chat.js"
click node_words_service "https://github.com/metal-code/drawzy/blob/main/backend/src/services/words.service.js"
click node_db_schema "https://github.com/metal-code/drawzy/blob/main/backend/src/db/schema.js"
click node_user_model "https://github.com/metal-code/drawzy/blob/main/backend/src/models/user.model.js"
click node_group_model "https://github.com/metal-code/drawzy/blob/main/backend/src/models/group.model.js"
click node_score_model "https://github.com/metal-code/drawzy/blob/main/backend/src/models/score.model.js"
click node_deploy "https://github.com/metal-code/drawzy/blob/main/.github/workflows/deploy.yml"

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_landing,node_lobby,node_groups,node_group_detail,node_game_page,node_postgame,node_leaderboard_page,node_auth_ctx,node_room_ctx,node_socket_ctx,node_api_client,node_socket_client,node_app toneBlue
class node_api_app,node_server_boot,node_auth_controller,node_group_controller,node_leaderboard_controller,node_socket_index,node_game_socket,node_room_socket,node_draw_socket,node_chat_socket,node_words_service toneAmber
class node_db_schema,node_user_model,node_group_model,node_score_model toneMint
class node_deploy toneRose
```

## Components

### Frontend

React/Vite single-page client, UI screens, and shared browser state.

- [Landing](https://github.com/metal-code/drawzy/blob/main/frontend/src/pages/Landing.jsx): page · Entry screen and initial navigation. · `frontend/src/pages/Landing.jsx`
- [Lobby](https://github.com/metal-code/drawzy/blob/main/frontend/src/pages/Lobby.jsx): page · Pre-game setup and room entry screen. · `frontend/src/pages/Lobby.jsx`
- [Groups](https://github.com/metal-code/drawzy/blob/main/frontend/src/pages/Groups.jsx): page · Group browsing and management entry screen. · `frontend/src/pages/Groups.jsx`
- [Group detail](https://github.com/metal-code/drawzy/blob/main/frontend/src/pages/GroupDetail.jsx): page · Single group view with membership and match history. · `frontend/src/pages/GroupDetail.jsx`
- [Game](https://github.com/metal-code/drawzy/blob/main/frontend/src/pages/Game.jsx): page · Main gameplay screen combining canvas, chat, timer, scoreboard, and word selection. · `frontend/src/pages/Game.jsx`
- [Post-game](https://github.com/metal-code/drawzy/blob/main/frontend/src/pages/PostGame.jsx): page · Results and wrap-up screen after a match ends. · `frontend/src/pages/PostGame.jsx`
- [Leaderboard](https://github.com/metal-code/drawzy/blob/main/frontend/src/pages/Leaderboard.jsx): page · Read-only ranking and stats screen. · `frontend/src/pages/Leaderboard.jsx`
- [Auth context](https://github.com/metal-code/drawzy/blob/main/frontend/src/context/AuthContext.jsx): client state · Holds login session state and auth actions. · `frontend/src/context/AuthContext.jsx`
- [Room context](https://github.com/metal-code/drawzy/blob/main/frontend/src/context/RoomContext.jsx): client state · Tracks current room and match-facing client state. · `frontend/src/context/RoomContext.jsx`
- [Socket context](https://github.com/metal-code/drawzy/blob/main/frontend/src/context/SocketContext.jsx): client state · Provides the shared Socket.io client connection. · `frontend/src/context/SocketContext.jsx`
- [API client](https://github.com/metal-code/drawzy/blob/main/frontend/src/utils/api.js): rest client · Centralized REST client and backend base URL setup. · `frontend/src/utils/api.js`
- [Socket client](https://github.com/metal-code/drawzy/blob/main/frontend/src/utils/socket.js): socket client · Centralized Socket.io client setup and connection config. · `frontend/src/utils/socket.js`
- [App](https://github.com/metal-code/drawzy/blob/main/frontend/src/App.jsx): router shell · Top-level client router and app composition. · `frontend/src/App.jsx`

### Backend

Express API, Socket.io game loop, and application services.

- [Express app](https://github.com/metal-code/drawzy/blob/main/backend/src/app.js): web app · Express middleware pipeline and route registration. · `backend/src/app.js`
- [Server boot](https://github.com/metal-code/drawzy/blob/main/backend/src/index.js): http+socket bootstrap · Starts HTTP server and attaches Socket.io. · `backend/src/index.js`
- [Auth](https://github.com/metal-code/drawzy/blob/main/backend/src/controllers/auth.controller.js): controller · Register, login, logout, and me endpoints. · `backend/src/controllers/auth.controller.js`
- [Groups API](https://github.com/metal-code/drawzy/blob/main/backend/src/controllers/group.controller.js): controller · Group creation, invite joining, membership, and history endpoints. · `backend/src/controllers/group.controller.js`
- [Leaderboard API](https://github.com/metal-code/drawzy/blob/main/backend/src/controllers/leaderboard.controller.js): controller · Read-only leaderboard endpoint backed by stored stats. · `backend/src/controllers/leaderboard.controller.js`
- [Socket entry](https://github.com/metal-code/drawzy/blob/main/backend/src/socket/index.js): socket registry · Registers game, room, draw, and chat event handlers. · `backend/src/socket/index.js`
- [Game state](https://github.com/metal-code/drawzy/blob/main/backend/src/socket/game.js): socket state machine · Turn flow, timers, word choice, and round transitions. · `backend/src/socket/game.js`
- [Room lifecycle](https://github.com/metal-code/drawzy/blob/main/backend/src/socket/room.js): socket room manager · Room creation, host migration, and online member coordination. · `backend/src/socket/room.js`
- [Draw relay](https://github.com/metal-code/drawzy/blob/main/backend/src/socket/draw.js): socket relay · Canvas stroke broadcasting and draw synchronization. · `backend/src/socket/draw.js`
- [Chat & scoring](https://github.com/metal-code/drawzy/blob/main/backend/src/socket/chat.js): socket scoring · Guess handling, scoring, and chat-driven game events. · `backend/src/socket/chat.js`
- [Word service](https://github.com/metal-code/drawzy/blob/main/backend/src/services/words.service.js): ai service · Generates match words via OpenRouter with fallback lists. · `backend/src/services/words.service.js`

### Persistence

SQLite schema and data-access models backing users, groups, and scores.

- [Schema init](https://github.com/metal-code/drawzy/blob/main/backend/src/db/schema.js): sqlite schema · Initializes SQLite tables and persistent structures. · `backend/src/db/schema.js`
- [Users](https://github.com/metal-code/drawzy/blob/main/backend/src/models/user.model.js): model · User persistence and account-facing data access. · `backend/src/models/user.model.js`
- [Groups](https://github.com/metal-code/drawzy/blob/main/backend/src/models/group.model.js): model · Group persistence, membership, and match history access. · `backend/src/models/group.model.js`
- [Scores](https://github.com/metal-code/drawzy/blob/main/backend/src/models/score.model.js): model · Leaderboard and aggregate score data access. · `backend/src/models/score.model.js`

### Ops

Deployment and automation for split frontend/backend hosting.

- [Deploy workflow](https://github.com/metal-code/drawzy/blob/main/.github/workflows/deploy.yml): ci pipeline · Automates split deployment for backend and frontend. · `.github/workflows/deploy.yml`

## Connections

- App → Express app: renders · Client routes and views are composed inside the app shell.
- Landing → Lobby: navigates · Landing leads into room setup and sign-in flows.
- Lobby → Game: enters · Lobby feeds the live match screen once a room is ready.
- Groups → Group detail: opens · Group list leads to a single group view.
- Game → Post-game: finishes · Match play transitions to the results screen.
- Auth context → API client: uses · Auth state drives REST calls for session actions.
- Room context → Socket context: uses · Room state is synchronized over the shared socket connection.
- Socket context → Socket client: provides · Socket context wraps the client socket instance.
- API client → Express app: calls · Frontend REST traffic targets the Express API.
- Socket client → Server boot: connects · Frontend live updates connect to the Socket.io server.
- Server boot → Express app: hosts · HTTP server serves the Express app and Socket.io.
- Express app → Auth: routes to · Auth endpoints are registered on the Express app.
- Express app → Groups API: routes to · Group endpoints are registered on the Express app.
- Express app → Leaderboard API: routes to · Leaderboard endpoint is exposed over HTTP.
- Auth → Users: reads/writes · Registered users are persisted through the user model.
- Groups API → Groups: reads/writes · Group membership and history are managed through the group model.
- Leaderboard API → Scores: reads · Leaderboard data is read from stored score aggregates.
- Express app → Schema init: initializes · Database schema is prepared for persistent storage.
- Schema init → Users: supports · User records rely on the SQLite schema.
- Schema init → Groups: supports · Group and membership records rely on the SQLite schema.
- Schema init → Scores: supports · Leaderboard aggregates rely on the SQLite schema.
- Server boot → Socket entry: registers · Socket handlers are attached during server startup.
- Socket entry → Game state: loads · Game state machine is one of the core socket modules.
- Socket entry → Room lifecycle: loads · Room lifecycle logic is one of the core socket modules.
- Socket entry → Draw relay: loads · Draw relay logic is one of the core socket modules.
- Socket entry → Chat & scoring: loads · Chat and scoring logic is one of the core socket modules.
- Game state → Room lifecycle: coordinates · Gameplay depends on room membership and host state.
- Game state → Word service: requests words · Words are generated before rounds begin.
- Chat & scoring → Scores: persists end stats · Match outcomes contribute to stored leaderboard stats.
- Groups API → Socket entry: notifies · Group game start events propagate to room members over sockets.
- Deploy workflow → Express app: deploys · Workflow includes backend deployment.
- Deploy workflow → App: deploys · Workflow includes frontend deployment.
