# melledy/lunarcore architecture

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

- Interactive diagram: https://gitdiagram.com/melledy/lunarcore
- Repository: https://github.com/melledy/lunarcore

## Overview

##### Main Components
a. Network & Communication Layer:
- The “server/packet” packages (both recv and send) and the “server/game” packages show the implementation of network message parsing, routing, and KCP listener networking.
- The Fiddler integration (documented in the README) indicates that there’s an HTTP tunable part for redirecting client connections locally.
b. Game Logic & Command Processing:
- The “game” package covers many subsystems such as account management, battle and challenge logic, chat, inventory and player management.
- The “command” package (with commands like /account, /avatar, /heal, etc.) implements an in-game command system, likely using a command pattern.
c. Data & Resource Management:
- The “data” package (including both generated proto files and custom/excel data readers under “data/excel”) manages configuration, game resources, and runtime game data.
- The “data/config” and “data/custom” directories indicate a rich configuration backbone.
d. Persistence & Database:
- The “database” package contains classes for connecting to an internal MongoDB instance as well as managing data storage separately.
e. HTTP Server Interface:
- The “server/http” package contains HTTP handlers that support various endpoints (e.g., token login, status checking, etc.).
f. Plugin Architecture:
- The “plugin” package offers an extensibility point so new game features or modifications can be introduced without affecting the core logic.
g. Utilities:
- The “util” packages support ancillary actions such as file handling, cryptography, and logging integration.

##### Relationships and Data Flows
- Client interactions (both the game client and HTTP-based calls) are initially handled by the Network Listener (KCP Listener and HTTP Server).
- Incoming packets are processed by the Packet Dispatcher (based on “server/packet/recv”) which extracts commands/requests.
- Commands are channeled into the Command Manager and further dispatched to the Game Logic modules.
- Game Logic components (spanning account, battle, inventory, gacha, chat, etc.) coordinate with the Data & Resource Management modules to load and update game state.
- Persistence is abstracted via database interfaces (in the “database” package) to read/write persistent player data.
- External configuration files (downloaded and placed in the “resources” folder) are loaded at startup via resource loaders.
- Internal dependencies like Utility functions and Plugin management overlay support common mechanisms across tiers.

##### Architectural Patterns and Design Principles
- Modularity is evident: separate packages for network, game logic, data loading, and utilities suggest good separation of concerns.
- Command pattern in the Command packages supports loosely coupled game commands.
- A layered architecture is in use: Presentation (network interface) → Application/Command processing → Domain (game logic) → Data/Persistence.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Client["Client"]

    %% Network Layer Subgraph
    subgraph "Network Layer"
        PacketHandler["KCP Listener & Packet Handler"]
        HttpHandler["HTTP Server & Handler"]
    end

    %% Protocol / Generated Classes Subgraph
    subgraph "Protocol / Generated Classes"
        ProtoClasses["Proto Classes"]
    end

    %% Command & Game Logic Subgraph
    subgraph "Command & Game Logic"
        CommandManager["Command Manager"]
        GameEngine["Game Engine (Account, Battle, etc.)"]
    end

    %% Data & Persistence Subgraph
    subgraph "Data & Persistence"
        DataLoader["Data & Resource Loader"]
        DatabaseManager["Database Manager"]
        ResourceFiles["Resource Files"]
    end

    %% Plugin Architecture and Utilities (Separate Components)
    PluginManager["Plugin Manager"]
    Utilities["Utilities"]

    %% Data Flow
    Client -->|"message"| PacketHandler
    Client -->|"HTTP request"| HttpHandler

    PacketHandler -->|"protocol parsing"| ProtoClasses
    PacketHandler -->|"dispatch"| CommandManager
    HttpHandler -->|"dispatch"| CommandManager

    CommandManager -->|"process command"| GameEngine

    GameEngine -->|"load/update state"| DataLoader
    DataLoader -->|"read config"| ResourceFiles
    GameEngine -->|"persist data"| DatabaseManager
    GameEngine -->|"extend features"| PluginManager
    GameEngine -->|"support"| Utilities

    %% Click Events
    click PacketHandler "https://github.com/melledy/lunarcore/tree/development/src/main/java/emu/lunarcore/server/packet"
    click HttpHandler "https://github.com/melledy/lunarcore/tree/development/src/main/java/emu/lunarcore/server/http"
    click CommandManager "https://github.com/melledy/lunarcore/tree/development/src/main/java/emu/lunarcore/command"
    click GameEngine "https://github.com/melledy/lunarcore/tree/development/src/main/java/emu/lunarcore/game"
    click DataLoader "https://github.com/melledy/lunarcore/tree/development/src/main/java/emu/lunarcore/data"
    click ResourceFiles "https://github.com/melledy/lunarcore/tree/development/data"
    click DatabaseManager "https://github.com/melledy/lunarcore/tree/development/src/main/java/emu/lunarcore/database"
    click PluginManager "https://github.com/melledy/lunarcore/tree/development/src/main/java/emu/lunarcore/plugin"
    click Utilities "https://github.com/melledy/lunarcore/tree/development/src/main/java/emu/lunarcore/util"
    click ProtoClasses "https://github.com/melledy/lunarcore/tree/development/src/generated/main/emu/lunarcore/proto"

    %% Styles
    classDef network fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef protocol fill:#FAD7A0,stroke:#B9770E,stroke-width:2px;
    classDef game fill:#A9DFBF,stroke:#145A32,stroke-width:2px;
    classDef data fill:#F9E79F,stroke:#7D6608,stroke-width:2px;
    classDef persistence fill:#F5B7B1,stroke:#922B21,stroke-width:2px;
    classDef plugin fill:#D2B4DE,stroke:#5B2C6F,stroke-width:2px;
    classDef utilities fill:#D5F5E3,stroke:#1E8449,stroke-width:2px;

    class PacketHandler,HttpHandler network;
    class ProtoClasses protocol;
    class CommandManager,GameEngine game;
    class DataLoader,ResourceFiles data;
    class DatabaseManager persistence;
    class PluginManager plugin;
    class Utilities utilities;
```
