# jake-purton/towertech architecture

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

- Interactive diagram: https://gitdiagram.com/jake-purton/towertech
- Repository: https://github.com/jake-purton/towertech

## Overview

##### Main Components
a. Client/Frontend
- The base of the UI is in the Next.js “app” directory, which includes pages for login, register, profile, game display, game control, leaderboards (game and player), and additional components like joining or hosting a game.
- The “public” folder holds game assets (images etc.) used by the front-end.
b. API Layer / Backend
- The “app/api” directory holds subdirectories for each API endpoint (Register, checkToken, endGame, gameleaderboard, etc.). These endpoints are responsible for handling RESTful operations such as authentication, game logic updates, and data retrieval.
- The server.js file and additional files in “src” (like eventHandlers.js and rooms.js) indicate further backend logic, possibly for managing game events and room/session state.
c. Game Engine & Components
- The “components” folder (and its "src") contains the game engine’s core logic with modules for game controllers, rendering (PhaserGame.js), player management, enemy behavior, waves, UI widgets, and more. These represent the dynamic aspects of the game world.
d. Additional Services & Configuration
- Authentication is handled using JWTs (as indicated by the secret generation instructions).
- Database connectivity to PostgreSQL for persisting game data, user profiles, and leaderboards.
- Supporting libraries and frameworks like Tailwind (tailwind.config.ts), ESLint, and the Next.js config denote underlying tech choices.

##### Relationships and Interactions
- The Client (Next.js pages and assets) communicates with the API endpoints hosted under “app/api” to manage user sessions (registration, login, token validation) and to perform game-related operations (like retrieving leaderboards, updating game status, etc.).
- The API layer often orchestrates interactions between the client and the game engine logic. For example, playing a game or hosting a session may trigger functionality in the game components, which manage game state and events.
- Both real-time and regular HTTP communication may be in use. Files like “app/src/socket.ts” suggest use of WebSockets for real-time game interactions, while the REST API endpoints handle standard HTTP requests.
- The backend communicates with the PostgreSQL database to store persistent data, authentication details, and game statistics.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Clear separation among presentation (Next.js pages and UI assets), game logic (components and Phaser integration), and business logic (API endpoints and service logic in “src/”).
- Modular Design: Both the API endpoints and game engine components are designed modularly (with individual directories for each API route and detailed organization for game entities, enemies, UI widgets, etc.).
- Client-Server Communication: Use of RESTful API endpoints and WebSocket connections facilitates real-time communication which is essential for interactive online gaming.
- Configuration Driven: Use of environment variables (JWT secrets, database tokens) emphasizes secure configuration practices.

##### Final Advice for the Principal Software Engineer
- Given the modular nature observed (with each API route and game module being separated into its own folder), aim to represent this modular breakdown in the system design so that every key functionality point (like user authentication, game logic, and leaderboard management) is individually highlighted.

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Client/Frontend"
        NextJSUI["Next.js UI (app/Pages)"]:::frontend
        FrontAssets["Frontend Assets (public)"]:::frontend
    end

    subgraph "API/Backend"
        RestAPI["REST API Endpoints (app/api)"]:::backend
        WebSocket["WebSocket Service (app/src/socket.ts & server.js)"]:::realtime
    end

    subgraph "Game Engine"
        GameEngine["Game Engine Core (PhaserGame.js)"]:::game
        GameModules["Game Modules (components/src)"]:::game
    end

    PostgresDB["Database (PostgreSQL)"]:::database

    NextJSUI -->|"HTTP_Request"| RestAPI
    NextJSUI -->|"WebSocket_Msg"| WebSocket
    FrontAssets -.->|"Provides"| NextJSUI
    RestAPI -->|"Data_Query"| PostgresDB
    RestAPI -->|"Game_Logic"| GameEngine
    WebSocket -->|"RealTime_Evt"| GameEngine
    GameEngine -->|"Module_Comm"| GameModules

    click NextJSUI "https://github.com/jake-purton/towertech/tree/main/app"
    click RestAPI "https://github.com/jake-purton/towertech/tree/main/app/api"
    click WebSocket "https://github.com/jake-purton/towertech/blob/main/app/src/socket.ts"
    click GameEngine "https://github.com/jake-purton/towertech/blob/main/components/PhaserGame.js"
    click GameModules "https://github.com/jake-purton/towertech/tree/main/components/src"
    click FrontAssets "https://github.com/jake-purton/towertech/tree/main/public"

    classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef backend fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef game fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef database fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef realtime fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
```
