# moke-game/game architecture

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

- Interactive diagram: https://gitdiagram.com/moke-game/game
- Repository: https://github.com/moke-game/game

## Overview

##### Main Components
- Game Server Core: Represented by the service code in the cmd/{game-name}/service directory and the internal services (e.g., internal/services/game0) that encapsulate game logic.
- Client Component: Shown in cmd/{game-name}/client, which implements an interactive shell client for testing and interaction.
- API Layer: The api directory holds the proto files along with the generated code (in api/gen) that provide communication interfaces using gRPC (and HTTP gateway as seen in the “.pb.gw.go” files).
- Data Persistence (Database Interaction): Highlight the components under internal/services/game0/db_nosql which seem responsible for database interactions.
- External and Infrastructure Services: Indicate the Deployment directories (docker-compose and k8s) that support infrastructure setup.
- Build & CI/CD: Note the build directory containing the Dockerfile, as well as the .github/workflows for automated testing, code analysis, and deployment.
- Additional Utilities/Shared Libraries: Components under pkg (e.g., pkg/dfx and pkg/modules) that provide reusable helper functions or client functionality.
- Testing: The tests directory contains integration and load testing files, which simulate real-world usage scenarios.

##### Relationships and Data Flow
- Interaction between the Client Component (both interactive client and automated load tests) and the Game Server via HTTP/gRPC.
- That the internal services rely on the NoSQL database layer – showing the DAO and factory patterns implemented.
- Build and deployment artifacts (docker-compose and Kubernetes manifests) which govern how the server is deployed in different environments.

##### Architectural Patterns and Design Principles
- Layered Architecture: Show separation between API, business logic, and data access layers.
- Microservices / Modular Components: Although it is a template for a single game server, indicate modularity through distinct modules (e.g., game0 versus gm in the API sub-folders and pkg/modules).
- Client-Server Communication: Illustrate the clear separation between clients (interactive client and load testing scripts) and the server endpoints.

##### Relevant Technologies and Frameworks
- Go as the primary programming language.
- Protocol Buffers (buf tool and generated Go code for gRPC and HTTP gateways).
- Docker and Docker Compose for containerized deployment.
- Kubernetes (as seen in the deployment/k8s folder) for orchestration.
- Load testing using k6.
- CI/CD via GitHub Actions (workflows in .github/workflows).
- Possibly NoSQL databases (implied by db_nosql components).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        ClientMain["Client Main"]
        ClientUtilities["Client Utilities"]
    end

    %% API Layer
    subgraph "API Layer"
        ProtoFiles["Proto Files"]
        APIGenerated["Generated Code"]
    end

    %% Game Server Core
    subgraph "Game Server Core"
        ServiceMain["Service Main"]
        ServiceLogic["Service Logic"]
        GameLogicDomain["Game Logic Domain"]
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        DBAccess["Database Access"]
    end

    %% Build & CI/CD
    subgraph "Build & CI/CD"
        Dockerfile["Dockerfile"]
        Workflows["Workflows"]
    end

    %% Deployment
    subgraph "Deployment"
        DockerComposeConfig["Docker Compose - Config"]
        DockerComposeInfra["Docker Compose - Infra"]
        Kubernetes["Kubernetes"]
    end

    %% Utilities
    subgraph "Utilities"
        DFX["DFX"]
        GameModule["Game Module"]
    end

    %% Testing
    subgraph "Testing"
        TestingScripts["Testing Scripts"]
    end

    %% Data Flow Connections
    ClientMain -->|"sends request"| APIGenerated
    ClientUtilities -->|"sends request"| APIGenerated
    ProtoFiles -->|"defines api"| APIGenerated
    APIGenerated -->|"invokes"| ServiceMain
    APIGenerated -->|"invokes"| ServiceLogic
    ServiceMain -->|"delegates"| GameLogicDomain
    ServiceLogic -->|"delegates"| GameLogicDomain
    GameLogicDomain -->|"accesses"| DBAccess
    TestingScripts -->|"tests"| APIGenerated

    %% Utilities Connections
    DFX -->|"assists"| ClientUtilities
    DFX -->|"assists"| ServiceMain
    GameModule -->|"supports"| ServiceLogic
    GameModule -->|"supports"| ClientUtilities

    %% CI/CD and Deployment Flow
    Workflows -->|"triggers build"| Dockerfile
    Dockerfile -->|"builds image for"| DockerComposeConfig
    Dockerfile -->|"builds image for"| DockerComposeInfra
    Dockerfile -->|"builds image for"| Kubernetes
    DockerComposeConfig -->|"deploys to"| ServiceMain
    DockerComposeInfra -->|"deploys to"| ServiceMain
    Kubernetes -->|"deploys to"| ServiceMain

    %% Class Definitions
    classDef client fill:#A9DFBF,stroke:#229954,stroke-width:2px;
    classDef api fill:#D2B4DE,stroke:#8E44AD,stroke-width:2px;
    classDef runtime fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef db fill:#FCF3CF,stroke:#B7950B,stroke-width:2px;
    classDef cicd fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
    classDef infra fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef util fill:#FAD7A0,stroke:#D35400,stroke-width:2px;
    classDef test fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;

    %% Assign classes
    class ClientMain,ClientUtilities client;
    class ProtoFiles,APIGenerated api;
    class ServiceMain,ServiceLogic,GameLogicDomain runtime;
    class DBAccess db;
    class Dockerfile,Workflows cicd;
    class DockerComposeConfig,DockerComposeInfra,Kubernetes infra;
    class DFX,GameModule util;
    class TestingScripts test;

    %% Click Events for Component Mapping
    click ClientMain "https://github.com/moke-game/game/blob/main/cmd/game0/client/main.go"
    click ClientUtilities "https://github.com/moke-game/game/tree/main/internal/clients/game0"
    click ProtoFiles "https://github.com/moke-game/game/blob/main/api/game0/game0.proto"
    click APIGenerated "https://github.com/moke-game/game/blob/main/api/gen/game0/api/game0.pb.go"
    click ServiceMain "https://github.com/moke-game/game/blob/main/cmd/game0/service/main.go"
    click ServiceLogic "https://github.com/moke-game/game/blob/main/internal/services/game0/service.go"
    click GameLogicDomain "https://github.com/moke-game/game/blob/main/internal/services/game0/domain/domain.go"
    click DBAccess "https://github.com/moke-game/game/tree/main/internal/services/game0/db_nosql"
    click DockerComposeConfig "https://github.com/moke-game/game/blob/main/deployment/docker-compose/docker-compose.yaml"
    click DockerComposeInfra "https://github.com/moke-game/game/blob/main/deployment/docker-compose/infrastructure.yaml"
    click Kubernetes "https://github.com/moke-game/game/blob/main/deployment/k8s/readme.md"
    click Dockerfile "https://github.com/moke-game/game/tree/main/build/package/docker/Dockerfile"
    click Workflows "https://github.com/moke-game/game/tree/main/.github/workflows"
    click DFX "https://github.com/moke-game/game/tree/main/pkg/dfx"
    click GameModule "https://github.com/moke-game/game/blob/main/pkg/modules/game0_module.go"
    click TestingScripts "https://github.com/moke-game/game/blob/main/tests/game0/game0.js"
```
