# peteradeojo/watersense architecture

> Architecture diagram of the GitHub repository peteradeojo/watersense, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/peteradeojo/watersense
- Repository: https://github.com/peteradeojo/watersense

## Overview

##### Project type and purpose
- This is a full-stack web application (“WaterSense Game”) combining a React-based single-page frontend with a Node.js/Express backend.
- Its purpose is to provide a real-time, session-based maze game with educational quizzes, session management for educators, and a leaderboard.

##### File structure
- Top-level:
- README.md describes project, timeline, features, stack, deployments.
- backend/ contains Express server code, models, routes, libraries.
- frontend/ holds a React app with components, assets, WebSocket & REST client.
- Patterns:
- Backend follows an MVC‐like separation (models/, routes/, lib/).
- Frontend uses component hierarchy, a global state context, and API/WebSocket modules.
- Deployment files:
- .gitignore, .prettierrc in both; package.json in both; server.js and app.js bootstrap backend.

##### Key README insights
- Uses REST for authentication and session CRUD.
- Uses Socket.io for real-time game state updates.
- Deployed on Railway (backend) and Vercel (frontend).
- Database implied by model files (likely MongoDB or SQL).

##### Components
a. Frontend (React SPA)
– React components (landing page, lobby, game canvas, leaderboard)
– API module for REST calls (auth, session management)
– WebSocket client for real-time game events and updates
b. Backend (Node.js/Express)
– REST API layer (routes/auth.js, routes/sessions.js)
– WebSocket server (integrated via Socket.io in server.js/app.js)
– Business logic / libraries (iomanager.js, ratelimit.js)
– Data models (User, Session, PlayerSession)
c. Database
– Persistent store for users, sessions, scores
d. External Services
– Vercel (hosts frontend)
– Railway (hosts backend)
– Postman documentation (external link)

##### Relationships & interactions
- User’s browser → Frontend SPA
- SPA → REST API calls → Backend → Database
- SPA ↔ WebSocket events ↔ Backend (real-time game state & scoring)
- Backend → Database for data persistence on REST and socket events
- Admin/Educator session creation → REST → DB → WebSocket notifications to clients
- Leaderboard reads → REST → DB → SPA

##### Architectural patterns & principles
- Client-server separation
- MVC-style layering in backend
- Event-driven real-time updates via WebSockets
- Rate-limiting library for API protection
- Single-page app with state management context

##### Final checklist
- Labels for each component and sub-component
- Callouts for important libraries (Socket.io, Express middleware, iomanager, ratelimit)
- Layer separation: UI layer, API layer, data/storage layer

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend (React SPA)"
        direction TB
        Landing["Landing Page Component"]:::frontend
        CreateRoom["Lobby / Create Room Component"]:::frontend
        JoinRoom["Lobby / Join Room Component"]:::frontend
        SelectRoom["Session Selection Component"]:::frontend
        GameCanvas["Game Canvas Component"]:::frontend
        Leaderboard["Leaderboard Component"]:::frontend
        APIModule["API Module for REST Calls & WebSocket Client"]:::frontend
        GlobalState["Global State Context"]:::frontend
    end

    subgraph "Backend (Node.js / Express)"
        direction TB
        AppBootstrap["Express App Bootstrap (app.js)"]:::backend
        ServerInit["Server & Socket.io Initialization (server.js)"]:::backend
        subgraph "REST API Routes"
            AuthRoutes["Authentication Routes (auth.js)"]:::backend
            SessionRoutes["Session Routes (sessions.js)"]:::backend
        end
        subgraph "Business Logic / Libraries"
            IOManager["I/O Manager (iomanager.js)"]:::backend
            RateLimit["Rate Limiting Middleware (ratelimit.js)"]:::backend
        end
        subgraph "Data Models"
            UserModel["User Model (User.js)"]:::backend
            SessionModel["Session Model (Session.js)"]:::backend
            PlayerSessionModel["PlayerSession Model (PlayerSession.js)"]:::backend
        end
    end

    DB["Database (Users, Sessions, Scores)"]:::db

    subgraph "External Services"
        direction TB
        Vercel["Vercel (Frontend Hosting)"]:::external
        Railway["Railway (Backend Hosting)"]:::external
        Postman["Postman Documentation"]:::external
    end

    Browser["User's Browser"]:::external

    Browser -->|"Load SPA"| Landing
    Vercel -->|"Hosts SPA"| Landing
    Railway -->|"Hosts API & Socket Server"| AppBootstrap

    Landing -->|"User Interaction & State"| GlobalState
    GlobalState -->|"REST & WS Calls"| APIModule

    APIModule -->|"REST: GET/POST /auth, /sessions"| AuthRoutes
    APIModule -->|"REST: GET/POST /auth, /sessions"| SessionRoutes
    APIModule -->|"WebSocket: joinRoom, gameUpdate, scoreUpdate"| ServerInit

    ServerInit -->|"Integrates Socket.io"| IOManager
    IOManager -->|"Handles socket events"| SessionRoutes
    IOManager -->|"Handles socket events"| AuthRoutes

    AuthRoutes -->|"DB Read/Write"| DB
    SessionRoutes -->|"DB Read/Write"| DB
    IOManager -->|"DB Persist Score"| DB
    RateLimit -->|"Protects Routes"| AuthRoutes
    RateLimit -->|"Protects Routes"| SessionRoutes

    Postman -->|"Docs for REST API"| AuthRoutes
    Postman -->|"Docs for REST API"| SessionRoutes

    classDef frontend fill:#cfe2ff,stroke:#0366d6,color:#000
    classDef backend fill:#d1e7dd,stroke:#198754,color:#000
    classDef db fill:#ffe5b4,stroke:#d59800,color:#000
    classDef external fill:#e2e3e5,stroke:#6c757d,color:#000

    click Landing "https://github.com/peteradeojo/watersense/blob/main/frontend/src/components/landing-page/landing-page.js"
    click CreateRoom "https://github.com/peteradeojo/watersense/blob/main/frontend/src/components/createroom/createroom.js"
    click JoinRoom "https://github.com/peteradeojo/watersense/blob/main/frontend/src/components/joinroom/joinroom.js"
    click SelectRoom "https://github.com/peteradeojo/watersense/blob/main/frontend/src/components/select-room/select-room.js"
    click GameCanvas "https://github.com/peteradeojo/watersense/blob/main/frontend/src/components/game/game.js"
    click Leaderboard "https://github.com/peteradeojo/watersense/blob/main/frontend/src/components/leaderboard/leaderboard.js"
    click APIModule "https://github.com/peteradeojo/watersense/blob/main/frontend/src/components/api.js"
    click GlobalState "https://github.com/peteradeojo/watersense/blob/main/frontend/src/components/globalstatecontext.js"
    click AppBootstrap "https://github.com/peteradeojo/watersense/blob/main/backend/app.js"
    click ServerInit "https://github.com/peteradeojo/watersense/blob/main/backend/server.js"
    click AuthRoutes "https://github.com/peteradeojo/watersense/blob/main/backend/routes/auth.js"
    click SessionRoutes "https://github.com/peteradeojo/watersense/blob/main/backend/routes/sessions.js"
    click IOManager "https://github.com/peteradeojo/watersense/blob/main/backend/lib/iomanager.js"
    click RateLimit "https://github.com/peteradeojo/watersense/blob/main/backend/lib/ratelimit.js"
    click UserModel "https://github.com/peteradeojo/watersense/blob/main/backend/models/User.js"
    click SessionModel "https://github.com/peteradeojo/watersense/blob/main/backend/models/Session.js"
    click PlayerSessionModel "https://github.com/peteradeojo/watersense/blob/main/backend/models/PlayerSession.js"
```
