# poooel/knucklebones architecture

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

- Interactive diagram: https://gitdiagram.com/poooel/knucklebones
- Repository: https://github.com/poooel/knucklebones

## Overview

##### Component Details

a. Client Layer (Top):
- Sub-components:
- Game UI Components (GameContext, PlayerBoard, Dice)
- State Management (GameContext)
- I18n System (translations)
- API Interface (utils/api.ts)

b. Edge Computing Layer (Middle):
- Endpoints:
- WebSocket Handler
- Game Management Endpoints
- Player Management Endpoints

c. State Management Layer (Bottom):
- GameStateDurableObject
- WebSocketDurableObject

##### Connection Flows

- Frontend → Cloudflare Workers (HTTP/WebSocket connections)
- Workers → Durable Objects (State persistence)
- WebSocket connections between clients (through Workers)

**[Frontend (React Application)]**
- Components Layer
- Game UI
- Player Boards
- Game Settings
- State Management
- GameContext
- API Interface

**[Cloudflare Workers]**
- Endpoint Handlers
- Game Management
- Player Management
- WebSocket Handler
- AI Implementation

**[Durable Objects]**
- GameStateDurableObject
- WebSocketDurableObject

**[Shared Package]**
- Common Code
- Game Logic
- Types
- Utilities

##### Real-time Communication
- WebSocket connections between clients
- State synchronization paths

##### State Management
- How game state is maintained in Durable Objects
- State propagation to connected clients

##### Shared Code
- How the common package is used by both frontend and backend

##### Deployment Flow
- Cloudflare Pages deployment for frontend
- Worker deployment path

This architecture is particularly interesting because it leverages Cloudflare's edge computing capabilities, so make sure to emphasize how the system takes advantage of this distributed nature for better performance and reliability.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Frontend
        FE["Frontend React Application"]:::frontend
        GC["Game Context"]:::frontend
        I18N["I18n System"]:::frontend
        API["API Interface"]:::frontend
        GUI["Game UI Components"]:::frontend
        GS["Game Settings"]:::frontend
        
        FE --> GC
        FE --> I18N
        FE --> API
        FE --> GUI
        FE --> GS
    end

    subgraph "Edge Computing Layer"
        WSH["WebSocket Handler"]:::worker
        GME["Game Management Endpoints"]:::worker
        PME["Player Management Endpoints"]:::worker
        AI["AI Implementation"]:::worker

        WSH --> GME
        WSH --> PME
        GME --> AI
    end

    subgraph "State Management Layer"
        GSDO["GameStateDurableObject"]:::durable
        WSDO["WebSocketDurableObject"]:::durable
    end

    subgraph "Shared Package"
        GL["Game Logic"]:::shared
        ST["Shared Types"]:::shared
        UT["Common Utilities"]:::shared
    end

    API --> WSH
    API --> GME
    API --> PME
    
    WSH --> WSDO
    GME --> GSDO
    PME --> GSDO

    GL --> FE
    GL --> GME
    ST --> FE
    ST --> WSH
    UT --> FE
    UT --> WSH

    classDef frontend fill:#2374ab
    classDef worker fill:#ff7f50
    classDef durable fill:#4caf50
    classDef shared fill:#9c27b0

    click FE "https://github.com/Poooel/knucklebones/tree/main/apps/front/src/components"
    click GC "https://github.com/Poooel/knucklebones/tree/main/apps/front/src/components/GameContext"
    click I18N "https://github.com/Poooel/knucklebones/tree/main/apps/front/src/translations"
    click API "https://github.com/Poooel/knucklebones/blob/main/apps/front/src/utils/api.ts"
    click GUI "https://github.com/Poooel/knucklebones/tree/main/apps/front/src/components/PlayerBoard"
    click WSH "https://github.com/Poooel/knucklebones/blob/main/apps/worker/src/endpoints/webSocket.ts"
    click GME "https://github.com/Poooel/knucklebones/blob/main/apps/worker/src/endpoints/play.ts"
    click PME "https://github.com/Poooel/knucklebones/blob/main/apps/worker/src/endpoints/displayName.ts"
    click GSDO "https://github.com/Poooel/knucklebones/blob/main/apps/worker/src/durable-objects/GameStateDurableObject.ts"
    click WSDO "https://github.com/Poooel/knucklebones/blob/main/apps/worker/src/durable-objects/WebSocketDurableObject.ts"
    click GL "https://github.com/Poooel/knucklebones/blob/main/packages/common/src/classes/GameState.ts"
    click ST "https://github.com/Poooel/knucklebones/tree/main/packages/common/src/types"
    click AI "https://github.com/Poooel/knucklebones/blob/main/apps/worker/src/classes/Ai.ts"
    click GS "https://github.com/Poooel/knucklebones/tree/main/apps/front/src/components/GameSettings"
    click UT "https://github.com/Poooel/knucklebones/tree/main/packages/common/src/utils"
```
