# think-and-dev/cartesi-boardgame architecture

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

- Interactive diagram: https://gitdiagram.com/think-and-dev/cartesi-boardgame
- Repository: https://github.com/think-and-dev/cartesi-boardgame

## Overview

##### Main Components

a) Core Framework Layer:
- Game Engine (src/core/)
- State Management System
- Turn Order Management
- Flow Control

b) Client Layer:
- React Client (packages/react.ts)
- React Native Client (packages/react-native.ts)
- Debug Interface (src/client/debug/)
- Transport System (src/client/transport/)

c) Server Layer:
- Game Server (src/server/)
- Authentication System (src/server/auth.ts)
- Database Adapters (src/server/db/)
- Transport Handlers (src/server/transport/)

d) Blockchain Integration Layer:
- Cartesi Virtual Machine (CVM) Integration
- Cartesify Transport (src/client/transport/cartesify-transport.ts)
- Move Validation System

e) Plugin System:
- Events Plugin
- Random Number Generator
- Immer Integration
- Logging System

##### Component Relationships

**[Client Layer] ←→ [Transport Layer] ←→ [Server Layer]**
- ↕
- [Blockchain Integration Layer]
- ↕
- [Core Framework Layer]
- ↕
- [Plugin System]

##### Technical Details

- Socket.IO connections for real-time communication
- Database adapters (SQLite, In-memory, Flatfile)
- AI/Bot integration points
- Debug interface connections
- Authentication flow

##### Implementation Notes

- Clear labels for each component
- Version information and supported platforms

##### Key Workflows to Highlight

a) Game Move Flow:
**Client → Transport → CVM Validation → State Update → Broadcast**

b) State Synchronization:
**Server State → Transport Layer → Multiple Clients**

c) Plugin Integration:
**Core Framework ← Plugin System → Game Implementation**

Remember to include:
- Version compatibility notes
- Required dependencies
- Integration points with Cartesi
- Supported client platforms
- Database options
- Authentication methods

This will provide a clear understanding of how traditional board game logic interfaces with blockchain technology while maintaining the framework's ease of use.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles and classes
    classDef core fill:#2196F3,color:white
    classDef client fill:#4CAF50,color:white
    classDef server fill:#FFC107,color:black
    classDef blockchain fill:#9C27B0,color:white
    classDef plugin fill:#FF9800,color:white
    classDef external fill:#757575,color:white

    %% Core Framework Layer
    subgraph CoreFramework
        GameEngine["Game Engine"]:::core
        StateManagement["State Management"]:::core
        TurnOrder["Turn Order Management"]:::core
        FlowControl["Flow Control"]:::core
    end
    click GameEngine "https://github.com/Think-and-Dev/cartesi-boardgame/tree/develop/src/core/"
    click StateManagement "https://github.com/Think-and-Dev/cartesi-boardgame/blob/develop/src/core/reducer.ts"
    click TurnOrder "https://github.com/Think-and-Dev/cartesi-boardgame/blob/develop/src/core/turn-order.ts"

    %% Client Layer
    subgraph ClientLayer
        ReactClient["React Client"]:::client
        ReactNativeClient["React Native Client"]:::client
        DebugInterface["Debug Interface"]:::client
        ClientTransport["Transport System"]:::client
    end
    click ReactClient "https://github.com/Think-and-Dev/cartesi-boardgame/blob/develop/packages/react.ts"
    click ReactNativeClient "https://github.com/Think-and-Dev/cartesi-boardgame/blob/develop/packages/react-native.ts"
    click DebugInterface "https://github.com/Think-and-Dev/cartesi-boardgame/tree/develop/src/client/debug/"
    click ClientTransport "https://github.com/Think-and-Dev/cartesi-boardgame/tree/develop/src/client/transport/"

    %% Server Layer
    subgraph ServerLayer
        GameServer["Game Server"]:::server
        Authentication["Authentication System"]:::server
        DatabaseAdapters["Database Adapters"]:::server
        ServerTransport["Transport Handlers"]:::server
        Master["Master"]:::server
        LobbySystem["Lobby System"]:::server
    end
    click GameServer "https://github.com/Think-and-Dev/cartesi-boardgame/tree/develop/src/server/"
    click Authentication "https://github.com/Think-and-Dev/cartesi-boardgame/blob/develop/src/server/auth.ts"
    click DatabaseAdapters "https://github.com/Think-and-Dev/cartesi-boardgame/tree/develop/src/server/db/"
    click ServerTransport "https://github.com/Think-and-Dev/cartesi-boardgame/tree/develop/src/server/transport/"
    click Master "https://github.com/Think-and-Dev/cartesi-boardgame/blob/develop/src/master/master.ts"
    click LobbySystem "https://github.com/Think-and-Dev/cartesi-boardgame/tree/develop/src/lobby/"

    %% Blockchain Integration Layer
    subgraph BlockchainLayer
        CVM["Cartesi Virtual Machine"]:::blockchain
        CartesifyTransport["Cartesify Transport"]:::blockchain
        MoveValidation["Move Validation System"]:::blockchain
    end
    click CartesifyTransport "https://github.com/Think-and-Dev/cartesi-boardgame/blob/develop/src/client/transport/cartesify-transport.ts"

    %% Plugin System
    subgraph PluginSystem
        EventsPlugin["Events Plugin"]:::plugin
        RandomGenerator["Random Number Generator"]:::plugin
        ImmerIntegration["Immer Integration"]:::plugin
        LoggingSystem["Logging System"]:::plugin
    end
    click EventsPlugin "https://github.com/Think-and-Dev/cartesi-boardgame/blob/develop/src/plugins/events/events.ts"
    click RandomGenerator "https://github.com/Think-and-Dev/cartesi-boardgame/blob/develop/src/plugins/random/random.ts"
    click ImmerIntegration "https://github.com/Think-and-Dev/cartesi-boardgame/blob/develop/src/plugins/plugin-immer.ts"
    click LoggingSystem "https://github.com/Think-and-Dev/cartesi-boardgame/blob/develop/src/plugins/plugin-log.ts"

    %% Relationships
    ClientLayer --> ClientTransport
    ClientTransport --> ServerTransport
    ServerTransport --> BlockchainLayer
    BlockchainLayer --> CoreFramework
    CoreFramework --> PluginSystem
    ServerLayer --> BlockchainLayer
    
    %% Socket.IO specific connection
    ClientTransport -.-> SocketIO["Socket.IO Transport"]:::external
    SocketIO -.-> ServerTransport
    click SocketIO "https://github.com/Think-and-Dev/cartesi-boardgame/blob/develop/src/server/transport/socketio.ts"

    %% Legend
    subgraph Legend
        CoreLegend["Core Components"]:::core
        ClientLegend["Client Components"]:::client
        ServerLegend["Server Components"]:::server
        BlockchainLegend["Blockchain Components"]:::blockchain
        PluginLegend["Plugin Components"]:::plugin
        ExternalLegend["External Services"]:::external
    end
```
