# fuiz/game architecture

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

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

## Overview

##### Project Type and Purpose
- Fuiz is a backend server written in Rust (evidenced by Cargo.toml, Cargo.lock, and the use of Cargo).
- Its purpose is to host live quizzes. Clients can create games, check if a game is active, and join games via a WebSocket connection.
- The README highlights key API endpoints:
– POST /add to create a new game.
– GET /alive/:gameid to check if a game is active.
– GET /watch/:gameid to join a game via WebSocket.
- This clearly indicates a networked, real-time application serving quiz content.

##### File Structure for Architectural Clues
- The src directory contains multiple Rust source files and modules such as:
– A main “lib.rs” file, which likely contains the core application logic.
– Dedicated modules for various quiz functionalities: “game.rs”, “session.rs”, “teams.rs”, “leaderboard.rs”, “names.rs”, and “watcher.rs”.
– A subdirectory “src/fuiz” contains additional modules like “config.rs”, “media.rs”, “multiple_choice.rs”, “order.rs”, and “type_answer.rs”, suggesting specialized components for quiz configuration and questions.
- The structure shows a modular design with logical grouping for aspects such as configuration, game logic, and user interaction (teams, sessions, etc.).

##### Main Components
a. Client Interaction Layer:
– HTTP API Endpoints:
- POST /add (for game creation)
- GET /alive/:gameid (for checking game status)
– WebSocket Endpoint (GET /watch/:gameid):
- Represents the real-time client connection for participating in quizzes.

b. Core Engine / Game Manager:
– Central module managing the lifecycle of a quiz game.
– Coordinates between configuration (from fuiz/config.rs), game logic (from game.rs and associated modules like multiple_choice, order, type_answer), and session handling.

c. Supporting Modules:
– Session Management (session.rs): likely responsible for tracking active quiz sessions.
– Team Handling (teams.rs): manages groupings or teams within a quiz.
– Leaderboard (leaderboard.rs): aggregates and displays quiz results and rankings.
– Watcher (watcher.rs): possibly monitors live quiz events or handles cleanup, reconnections, etc.
– Miscellaneous utilities (names.rs, game_id.rs, etc.) provided for managing identifiers and auxiliary data.

d. Configuration:
– Module(s) under fuiz, especially config.rs, load and validate game configurations (with artifacts like config.toml also in the project root).

e. External Communication:
– Client devices’ interactions via HTTP and WebSocket, which should be depicted as external components interfacing with the backend engine.

##### Relationships and Data Flow
- Clients send an HTTP POST to /add to create a new game. The request passes through a HTTP server layer (entrypoint) to the Game Manager, which uses configuration and options to instantiate a quiz game.
- When a client checks for game activity, the HTTP GET request to /alive/:gameid routes naturally to the Game Manager to fetch the status.
- For joining the game, clients initiate a WebSocket connection via GET /watch/:gameid. The WebSocket handler (possibly within the “watcher” module) binds the client to the game session and facilitates real-time communication.
- Core modules communicate internally:
– The Game Manager interacts with session, leaderboard, teams, and various fuiz sub-modules to manage game logic.
– Utility modules like game_id and names help maintain unique game sessions and identity information.

##### Architectural Patterns and Design Principles
- Modular Design: The code is organized into multiple modules, each handling a specific concern.
- Separation of Concerns: There is a clear division between API handling (HTTP/WebSocket endpoints) and game logic (the engine and supporting modules).
- Extensibility: The fuiz submodules (like multiple choice, ordering, and type answer) indicate that the design is meant to support various quiz question types.
- Real-time Communication: The integration of a WebSocket mechanism shows the system’s focus on interactive, live engagement.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Clients
    subgraph "External Clients"
        A["Client Devices"]:::client
    end

    %% API Layer
    subgraph "API Layer"
        B["HTTP API & WebSocket Endpoint"]:::api
    end

    %% Core Engine
    subgraph "Core Engine"
        C["Game Manager"]:::engine
    end

    %% Supporting Modules
    subgraph "Supporting Modules"
        D["Session Manager"]:::module
        E["Teams Handler"]:::module
        F["Leaderboard"]:::module
        G["WebSocket Handler"]:::module
    end

    %% Quiz Modules
    subgraph "Quiz Modules"
        H["Configuration"]:::config
        I["Multiple Choice"]:::module
        J["Order Questions"]:::module
        K["Type Answer"]:::module
        L["Media Handler"]:::module
        M["Name Utilities"]:::util
        N["Game ID Utilities"]:::util
    end

    %% Connections
    A -->|"HTTP/WebSocketRequest"| B
    B -->|"RoutesRequest"| C
    B -->|"WebSocketConnect"| G
    C -->|"ManageSessions"| D
    C -->|"ManageTeams"| E
    C -->|"UpdateLeaderboard"| F
    C -->|"RealTimeUpdates"| G
    C -->|"LoadConfig"| H
    C -->|"ProcessQuiz(MultipleChoice)"| I
    C -->|"ProcessQuiz(Order)"| J
    C -->|"ProcessQuiz(TypeAnswer)"| K
    C -->|"HandleMedia"| L
    C -->|"UtilizeNames"| M
    C -->|"UtilizeGameIDs"| N
    G -->|"RealTimeResponses"| C

    %% Click Events
    click B "https://github.com/fuiz/game/blob/main/src/lib.rs"
    click C "https://github.com/fuiz/game/blob/main/src/game.rs"
    click D "https://github.com/fuiz/game/blob/main/src/session.rs"
    click E "https://github.com/fuiz/game/blob/main/src/teams.rs"
    click F "https://github.com/fuiz/game/blob/main/src/leaderboard.rs"
    click G "https://github.com/fuiz/game/blob/main/src/watcher.rs"
    click H "https://github.com/fuiz/game/blob/main/src/fuiz/config.rs"
    click I "https://github.com/fuiz/game/blob/main/src/fuiz/multiple_choice.rs"
    click J "https://github.com/fuiz/game/blob/main/src/fuiz/order.rs"
    click K "https://github.com/fuiz/game/blob/main/src/fuiz/type_answer.rs"
    click L "https://github.com/fuiz/game/blob/main/src/fuiz/media.rs"
    click M "https://github.com/fuiz/game/blob/main/src/names.rs"
    click N "https://github.com/fuiz/game/blob/main/src/game_id.rs"

    %% Style Definitions
    classDef client fill:#f8c471,stroke:#000,stroke-width:2px;
    classDef api fill:#85c1e9,stroke:#2e86c1,stroke-width:2px;
    classDef engine fill:#a9dfbf,stroke:#27ae60,stroke-width:2px;
    classDef module fill:#f9e79f,stroke:#d4ac0d,stroke-width:2px;
    classDef config fill:#fad7a0,stroke:#e67e22,stroke-width:2px;
    classDef util fill:#d2b4de,stroke:#8e44ad,stroke-width:2px;
```
