How metal-code/drawzy works
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: app.js Express app · socket/index.js Socket entry · schema.js Schema init · src/index.js Server boot
Components 29
Frontend
React/Vite single-page client, UI screens, and shared browser state.
- Landing · Entry screen and initial navigation.
frontend/src/pages/Landing.jsx - Lobby · Pre-game setup and room entry screen.
frontend/src/pages/Lobby.jsx - Groups · Group browsing and management entry screen.
frontend/src/pages/Groups.jsx - Group detail · Single group view with membership and match history.
frontend/src/pages/GroupDetail.jsx - Game · Main gameplay screen combining canvas, chat, timer, scoreboard, and word selection.
frontend/src/pages/Game.jsx - Post-game · Results and wrap-up screen after a match ends.
frontend/src/pages/PostGame.jsx - Leaderboard · Read-only ranking and stats screen.
frontend/src/pages/Leaderboard.jsx - Auth context · Holds login session state and auth actions.
frontend/src/context/AuthContext.jsx - Room context · Tracks current room and match-facing client state.
frontend/src/context/RoomContext.jsx - Socket context · Provides the shared Socket.io client connection.
frontend/src/context/SocketContext.jsx - API client · Centralized REST client and backend base URL setup.
frontend/src/utils/api.js - Socket client · Centralized Socket.io client setup and connection config.
frontend/src/utils/socket.js - App · Top-level client router and app composition.
frontend/src/App.jsx
Backend
Express API, Socket.io game loop, and application services.
- Express app · Express middleware pipeline and route registration.
backend/src/app.js - Server boot · Starts HTTP server and attaches Socket.io.
backend/src/index.js - Auth · Register, login, logout, and me endpoints.
backend/src/controllers/auth.controller.js - Groups API · Group creation, invite joining, membership, and history endpoints.
backend/src/controllers/group.controller.js - Leaderboard API · Read-only leaderboard endpoint backed by stored stats.
backend/src/controllers/leaderboard.controller.js - Socket entry · Registers game, room, draw, and chat event handlers.
backend/src/socket/index.js - Game state · Turn flow, timers, word choice, and round transitions.
backend/src/socket/game.js - Room lifecycle · Room creation, host migration, and online member coordination.
backend/src/socket/room.js - Draw relay · Canvas stroke broadcasting and draw synchronization.
backend/src/socket/draw.js - Chat & scoring · Guess handling, scoring, and chat-driven game events.
backend/src/socket/chat.js - Word 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 · Initializes SQLite tables and persistent structures.
backend/src/db/schema.js - Users · User persistence and account-facing data access.
backend/src/models/user.model.js - Groups · Group persistence, membership, and match history access.
backend/src/models/group.model.js - Scores · Leaderboard and aggregate score data access.
backend/src/models/score.model.js
Ops
Deployment and automation for split frontend/backend hosting.
- Deploy workflow · Automates split deployment for backend and frontend.
.github/workflows/deploy.yml
Connections 32
- 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.