# zengm-games/zengm architecture

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

- Interactive diagram: https://gitdiagram.com/zengm-games/zengm
- Repository: https://github.com/zengm-games/zengm

## Overview

##### Main System Components

a) Client-Side Architecture:
- UI Layer (React + Bootstrap)
- Worker Layer (Shared Worker/Web Worker)
- IndexedDB Storage Layer
- Cache Layer

b) Core Processing Units:
- Game Simulation Engine
- Player Management System
- Team Management System
- League Management System

##### Data Flow Architecture

Critical interactions:

a) Communication Channels:
- UI to Worker communication (`toUI` and `toWorker` functions)
- Worker to IndexedDB interactions
- Cache to IndexedDB synchronization

b) State Management:
- Global state handling (`self.bbgm`)
- Cache management system
- Data persistence flow

##### Key Subsystems

Major functional components:

a) Sport-Specific Modules:
- Basketball
- Football
- Baseball
- Hockey

b) Core Game Features:
- Player Generation
- Game Simulation
- Statistics Processing
- League Management

##### User Interface Layer
- React Components
- Bootstrap UI Elements
- View Controllers

##### Processing Layer
- Shared/Web Worker
- Game Logic
- Simulation Engine

##### Data Layer
- Cache System
- IndexedDB
- Local Storage

- Blue: UI components
- Green: Processing components
- Yellow: Data storage
- Red: Critical communication paths

##### Implementation Details

Technical aspects:

a) Client Architecture:
**Browser Tab 1 ----\**
**Browser Tab 2 ------> Shared Worker <---> IndexedDB**
**Browser Tab 3 ----/        |**
- v
- Cache Layer

b) Data Flow Patterns:
- Bi-directional communication between UI and Worker
- Cache update mechanisms
- Demonstrate database transaction flows

##### Integration Points

Connection points:

a) External Systems:
- Service Worker for offline support
- Build system integration
- Development tools connection

b) Development Tools:
- Testing framework integration
- Debugging interfaces
- Monitoring points

This design should effectively communicate both the high-level architecture and the important implementation details of this sophisticated browser-based sports simulation system.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles and classes
    classDef ui fill:#3498db,color:white
    classDef worker fill:#2ecc71,color:white
    classDef storage fill:#f1c40f,color:white
    classDef game fill:#e74c3c,color:white
    classDef communication fill:#9b59b6,color:white
    classDef management fill:#1abc9c,color:white

    %% UI Layer
    subgraph UI
        UI_Components["React Components"]:::ui
        Bootstrap["Bootstrap UI"]:::ui
        ViewControllers["View Controllers"]:::ui
    end
    click UI_Components "https://github.com/zengm-games/zengm/tree/master/src/ui/components/"
    click UI "https://github.com/zengm-games/zengm/tree/master/src/ui/"

    %% Worker Layer
    subgraph Worker
        SharedWorker["Shared Worker"]:::worker
        GameLogic["Game Logic"]:::worker
        WorkerAPI["Worker API"]:::worker
    end
    click SharedWorker "https://github.com/zengm-games/zengm/tree/master/src/worker/"
    click WorkerAPI "https://github.com/zengm-games/zengm/tree/master/src/worker/api/"

    %% Data Layer
    subgraph Storage
        IndexedDB[(IndexedDB)]:::storage
        Cache["Cache Layer"]:::storage
        LocalStorage[(Local Storage)]:::storage
    end
    click IndexedDB "https://github.com/zengm-games/zengm/blob/master/src/worker/db/connectIndexedDB.ts"
    click Cache "https://github.com/zengm-games/zengm/blob/master/src/worker/db/Cache.ts"

    %% Game Simulation Engines
    subgraph GameEngines
        Baseball["Baseball Engine"]:::game
        Basketball["Basketball Engine"]:::game
        Football["Football Engine"]:::game
        Hockey["Hockey Engine"]:::game
    end
    click Baseball "https://github.com/zengm-games/zengm/tree/master/src/worker/core/GameSim.baseball/"
    click Basketball "https://github.com/zengm-games/zengm/tree/master/src/worker/core/GameSim.basketball/"
    click Football "https://github.com/zengm-games/zengm/tree/master/src/worker/core/GameSim.football/"
    click Hockey "https://github.com/zengm-games/zengm/tree/master/src/worker/core/GameSim.hockey/"

    %% Management Systems
    subgraph Management
        PlayerMgmt["Player Management"]:::management
        TeamMgmt["Team Management"]:::management
        LeagueMgmt["League Management"]:::management
    end
    click PlayerMgmt "https://github.com/zengm-games/zengm/tree/master/src/worker/core/player/"
    click TeamMgmt "https://github.com/zengm-games/zengm/tree/master/src/worker/core/team/"
    click LeagueMgmt "https://github.com/zengm-games/zengm/tree/master/src/worker/core/league/"

    %% Communication Layer
    ToUI["toUI"]:::communication
    ToWorker["toWorker"]:::communication
    click ToUI "https://github.com/zengm-games/zengm/blob/master/src/worker/util/toUI.ts"
    click ToWorker "https://github.com/zengm-games/zengm/blob/master/src/ui/util/toWorker.ts"

    %% Sport Constants
    subgraph Constants
        BaseballConst["Baseball Constants"]
        BasketballConst["Basketball Constants"]
        FootballConst["Football Constants"]
        HockeyConst["Hockey Constants"]
    end
    click BaseballConst "https://github.com/zengm-games/zengm/blob/master/src/common/constants.baseball.ts"
    click BasketballConst "https://github.com/zengm-games/zengm/blob/master/src/common/constants.basketball.ts"
    click FootballConst "https://github.com/zengm-games/zengm/blob/master/src/common/constants.football.ts"
    click HockeyConst "https://github.com/zengm-games/zengm/blob/master/src/common/constants.hockey.ts"

    %% Relationships
    UI_Components --> ToWorker
    ToWorker --> SharedWorker
    SharedWorker --> ToUI
    ToUI --> UI_Components
    SharedWorker --> Cache
    Cache --> IndexedDB
    SharedWorker --> GameLogic
    GameLogic --> GameEngines
    GameLogic --> Management
    Management --> IndexedDB
    Constants --> GameEngines
    WorkerAPI --> GameLogic

    %% Legend
    subgraph Legend
        UI_L["UI Components"]:::ui
        Worker_L["Worker Components"]:::worker
        Storage_L["Storage Components"]:::storage
        Game_L["Game Engines"]:::game
        Comm_L["Communication"]:::communication
        Mgmt_L["Management Systems"]:::management
    end
```
