# stefanrudi/darts-counter architecture

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

- Interactive diagram: https://gitdiagram.com/stefanrudi/darts-counter
- Repository: https://github.com/stefanrudi/darts-counter

## Overview

##### Main Components
- Client (Frontend): Contains React components (GameLobby, GameBoard, Dartboard, etc.), hooks (e.g., useWebSocket, useGame), utilities, and assets. This part is responsible for the user interface and communicating with the server.
- Server (Backend): Built on Node.js/Express. Contains controllers (e.g., wsController), models (Game, Player), services (GameService, WebSocketService), and utilities. This serves as the core gameplay logic handler, real-time communication manager, and state keeper.
- Shared: A module that provides TypeScript types (client, server, game types) ensuring both client and server use a consistent contract.
- WebSocket Layer: Acts as the real-time communication bridge between the client and server.

##### Relationships and Interactions
- Clients (web browsers running the React app) establish WebSocket connections to the server.
- Frontend components (e.g., GameLobby, GameBoard) translate user interactions into WebSocket messages. Use hooks like useWebSocket and useGame to manage state and communication.
- The Server’s WebSocketService manages connections and routes messages through the wsController.
- GameService on the server handles game-related logic such as score tracking, game session management, and processing throw history.
- Models (Game and Player) represent and persist the game state.
- Both client and server use type definitions from the shared module, ensuring strong typing and consistency.

##### Architectural Patterns and Design Principles
- Separation of Concerns: The client handles UI using React, while the server handles business logic and real-time communication.
- Event-driven Real-time Communication: WebSocket-based interactions push updates to clients as game events happen.
- Modularization: Different directories (components, hooks, controllers, models, services) indicate a modular approach to both the frontend and backend.
- Consistent Typing: Shared TypeScript types guarantee a unified interface between the frontend and backend.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend (Client)"
        RFE["React Frontend"]
        UI["UI Components (GameLobby, GameBoard, Dartboard)"]
        HK["Hooks (useWebSocket, useGame)"]
    end

    subgraph "Real-time Communication"
        WSok["WebSocket"]
    end

    subgraph "Backend (Server)"
        ES["Express Server"]
        WSC["WebSocket Controller"]
        GS["Game Service"]
        WSS["WebSocket Service"]
        DM["Domain Models (Game, Player)"]
    end

    ST["Shared TypeScript Types"]

    %% Client to WebSocket Communication
    RFE -->|"initiates"| WSok
    UI -->|"triggersEvents"| WSok
    HK -->|"handlesWS"| WSok
    WSok -->|"pushUpdates"| RFE

    %% WebSocket to Server Communication
    WSok -->|"connectsTo"| ES

    %% Server internal Flow
    ES -->|"routesTo"| WSC
    WSC -->|"calls"| GS
    WSC -->|"calls"| WSS
    GS -->|"updates"| DM

    %% Shared Types Usage
    RFE -->|"usesTypes"| ST
    ES -->|"usesTypes"| ST
    DM -->|"usesTypes"| ST

    %% Styling
    classDef frontend fill:#E8F8F5,stroke:#1ABC9C,stroke-width:2px;
    classDef backend fill:#FDEDEC,stroke:#E74C3C,stroke-width:2px;
    classDef websocket fill:#F9E79F,stroke:#F1C40F,stroke-width:2px;
    classDef shared fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;

    class RFE,UI,HK frontend;
    class ES,WSC,GS,WSS,DM backend;
    class WSok websocket;
    class ST shared;

    %% Click Events
    click RFE "https://github.com/stefanrudi/darts-counter/tree/main/client/src"
    click UI "https://github.com/stefanrudi/darts-counter/tree/main/client/src/components"
    click HK "https://github.com/stefanrudi/darts-counter/tree/main/client/src/hooks"
    click ES "https://github.com/stefanrudi/darts-counter/blob/main/server/src/server.ts"
    click WSC "https://github.com/stefanrudi/darts-counter/blob/main/server/src/controllers/wsController.ts"
    click GS "https://github.com/stefanrudi/darts-counter/blob/main/server/src/services/GameService.ts"
    click WSS "https://github.com/stefanrudi/darts-counter/blob/main/server/src/services/WebSocketService.ts"
    click DM "https://github.com/stefanrudi/darts-counter/tree/main/server/src/models"
    click ST "https://github.com/stefanrudi/darts-counter/tree/main/shared/types"
```
