# mvolfik/roborally architecture

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

- Interactive diagram: https://gitdiagram.com/mvolfik/roborally
- Repository: https://github.com/mvolfik/roborally

## Overview

##### Main Components

a) Client-Side Components:
- Frontend SPA (Svelte-based)
- WebAssembly Module (roborally-frontend-wasm)
- Shared Structures (roborally-structs with 'client' feature)

b) Server-Side Components:
- Game Server (roborally-server)
- Shared Structures (roborally-structs with 'server' feature)
- Storage
- Game State Management
- Card Script Engine (Rhai)

**[Frontend Layer] <-> [Communication Layer] <-> [Backend Layer]**

##### Key Relationships to Highlight

a) Frontend Layer:
- Svelte components hierarchy
- WebAssembly bridge
- Demonstrate shared structure usage

b) Communication Layer:
- WebSocket connection
- MessagePack serialization
- Enum-based message types (ServerMessage/ClientMessage)

c) Backend Layer:
- Game state management
- Card script execution
- Handling
- Player management

##### Specific Implementation Details

- WebSocket communication paths
- State updates
- Game command flow
- Animation state propagation

b) Component Interactions:
- How WebAssembly interfaces with JavaScript
- Game state locking mechanism
- Card script execution flow

##### Detailed Component Breakdown

Frontend:
**[Svelte Components]**
- App.svelte (Main container)
- Game.svelte (Game state manager)
- Supporting Components
- Map.svelte
- Programmer.svelte
- Others

**[WebAssembly Bridge]**
- State Deserialization
- Type Conversions
- CSS Transformations

Backend:
**[Game Server]**
- Game State Manager
- Player Connections
- Card Script Engine
- Map Handler

**[Shared Structures]**
- Game State
- Messages
- Types

##### Implementation Notes

Callouts for:
- Rust's type system usage
- WebAssembly limitations and workarounds
- State management approach
- Real-time game synchronization

Remember to emphasize the unique aspects of this implementation:
- The shared structure approach between client and server
- The WebAssembly bridge for efficient state handling
- The separation of game logic and presentation
- The real-time nature of the game state updates

This system design should clearly communicate how the application maintains type safety across boundaries while delivering a complex board game experience in a web browser.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef backend fill:#e67e22,stroke:#d35400,color:white
    classDef shared fill:#9b59b6,stroke:#8e44ad,color:white
    classDef communication fill:#2ecc71,stroke:#27ae60,color:white
    classDef external fill:#f1c40f,stroke:#f39c12,color:black

    %% Frontend Layer
    subgraph Frontend
        SvelteApp["App.svelte"]
        GameComponent["Game.svelte"]
        MapComponent["Map.svelte"]
        ProgrammerComponent["Programmer.svelte"]
        
        SvelteApp --> GameComponent
        GameComponent --> MapComponent
        GameComponent --> ProgrammerComponent
    end

    %% WebAssembly Bridge
    subgraph WasmBridge
        WasmModule["WebAssembly Module"]
        AssetHandler["Asset Handler"]
        
        WasmModule --> AssetHandler
    end

    %% Communication Layer
    subgraph Communication
        WebSocket["WebSocket Connection"]
        MessagePack["MessagePack Serialization"]
        MessageTypes["Message Types"]
    end

    %% Backend Layer
    subgraph Backend
        GameServer["Game Server"]
        GameLogic["Game Logic"]
        PlayerMgmt["Player Management"]
        GameStateMgmt["Game State Management"]
        ConnHandler["Connection Handler"]
        CardEngine["Card Script Engine"]
        MapStorage["Map Storage"]
    end

    %% Shared Structures
    subgraph SharedStructures
        GameState["Game State"]
        Animations["Animations"]
        Cards["Cards"]
        GameMap["Game Map"]
        Transport["Transport Layer"]
        Position["Position Handler"]
        Tiles["Tile Management"]
        
        subgraph GameStateComponents
            AnimatedState["Animated State"]
            GamePhases["Game Phases"]
            PlayerState["Player State"]
        end
    end

    %% Relationships
    Frontend <--> WasmBridge
    WasmBridge <--> Communication
    Communication <--> Backend
    Backend <--> SharedStructures
    GameServer --> GameLogic
    GameServer --> PlayerMgmt
    GameServer --> GameStateMgmt
    GameServer --> ConnHandler
    GameServer --> CardEngine
    GameServer --> MapStorage

    %% Click Events
    click SvelteApp "https://github.com/mvolfik/roborally/blob/master/roborally-frontend/src/App.svelte"
    click GameComponent "https://github.com/mvolfik/roborally/blob/master/roborally-frontend/src/Game.svelte"
    click MapComponent "https://github.com/mvolfik/roborally/blob/master/roborally-frontend/src/Map.svelte"
    click ProgrammerComponent "https://github.com/mvolfik/roborally/blob/master/roborally-frontend/src/Programmer.svelte"
    click WasmModule "https://github.com/mvolfik/roborally/blob/master/backend/roborally-frontend-wasm/src/lib.rs"
    click AssetHandler "https://github.com/mvolfik/roborally/blob/master/backend/roborally-frontend-wasm/src/asset.rs"
    click GameServer "https://github.com/mvolfik/roborally/blob/master/backend/roborally-server/src/main.rs"
    click GameLogic "https://github.com/mvolfik/roborally/blob/master/backend/roborally-server/src/game.rs"
    click PlayerMgmt "https://github.com/mvolfik/roborally/blob/master/backend/roborally-server/src/player.rs"
    click GameStateMgmt "https://github.com/mvolfik/roborally/blob/master/backend/roborally-server/src/game_state.rs"
    click ConnHandler "https://github.com/mvolfik/roborally/blob/master/backend/roborally-server/src/game_connection.rs"
    click CardEngine "https://github.com/mvolfik/roborally/blob/master/backend/roborally-server/src/rhai_api.rs"
    click MapStorage "https://github.com/mvolfik/roborally/tree/master/backend/maps/"
    click GameState "https://github.com/mvolfik/roborally/tree/master/backend/roborally-structs/src/game_state/"
    click Animations "https://github.com/mvolfik/roborally/blob/master/backend/roborally-structs/src/animations.rs"
    click Cards "https://github.com/mvolfik/roborally/blob/master/backend/roborally-structs/src/card.rs"
    click GameMap "https://github.com/mvolfik/roborally/blob/master/backend/roborally-structs/src/game_map.rs"
    click Transport "https://github.com/mvolfik/roborally/blob/master/backend/roborally-structs/src/transport.rs"
    click Position "https://github.com/mvolfik/roborally/blob/master/backend/roborally-structs/src/position.rs"
    click Tiles "https://github.com/mvolfik/roborally/blob/master/backend/roborally-structs/src/tile.rs"
    click AnimatedState "https://github.com/mvolfik/roborally/blob/master/backend/roborally-structs/src/game_state/animated_state.rs"
    click GamePhases "https://github.com/mvolfik/roborally/blob/master/backend/roborally-structs/src/game_state/phase.rs"
    click PlayerState "https://github.com/mvolfik/roborally/blob/master/backend/roborally-structs/src/game_state/player_public_state.rs"

    %% Style Applications
    class SvelteApp,GameComponent,MapComponent,ProgrammerComponent frontend
    class WasmModule,AssetHandler frontend
    class WebSocket,MessagePack,MessageTypes communication
    class GameServer,GameLogic,PlayerMgmt,GameStateMgmt,ConnHandler,CardEngine,MapStorage backend
    class GameState,Animations,Cards,GameMap,Transport,Position,Tiles,AnimatedState,GamePhases,PlayerState shared
```
