# ledinhtuyen/ailatrieuphu architecture

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

- Interactive diagram: https://gitdiagram.com/ledinhtuyen/ailatrieuphu
- Repository: https://github.com/ledinhtuyen/ailatrieuphu

## Overview

##### Project type and purpose
- This is a full-stack, real-time networked trivia game (“Ai là triệu phú”) with a Qt/QML C++ client and a C server backed by MySQL.
- The client handles UI, game flow, asset playback, while the server manages authentication, question distribution, PvP matchmaking, scoring, and persistence.

##### File structure
- Top-level:
- client/ – Qt application
– qml/ – all QML views (LoginPage.qml, GameScreen.qml, popups, etc.)
– src/ – C++ glue (BackEnd.cpp, Utils.c, main.cpp)
– headers/ – BackEnd.h, Utils.h
– assets/ – audio clips, sprites, fonts
- server/ – C network server
– server.c/.h – main logic (socket listening, client threads, game rules)
– config – config file for DB/port
– database.sql – schema & seed data
- Build scripts: Makefile (server, client), qmake project (.pro).
- Runtime dependencies: Qt 5.12 framework, libmysqlclient, OpenGL library (libgl1), MySQL server.

##### README
- Confirms tech stack: C/C++ for backend logic, QML/Qt for UI, MySQL for storage.
- Describes features: register/login, solo/PvP modes.
- Explains setup: compile server, import database, compile client, run with server IP/port.

a. Main components:
- Client Application
– QML UI Layer (Views & Popups)
– C++ Backend Layer (BackEnd.cpp & Utils.c)
– Asset Manager (audio, sprites, fonts)
– Network Module (QtNetwork or POSIX sockets)
- Server Application
– Network Listener (accepts client TCP connections)
– Game Engine (authentication, question dispatcher, matchmaking)
– DB Access Layer (MySQL client library)
- MySQL Database Server
– Users table, Questions table, Sessions/logs
- External Dependencies
– Qt Framework, OpenGL, libmysqlclient

b. Relationships & interactions:
1. Client UI triggers actions (login, answer, request question) → Backend Layer via Qt signals
2. Backend Layer serializes requests → Network Module → TCP → Server
3. Server Network Listener → hands off to Game Engine (per-client threads or event loop)
4. Game Engine issues queries via DB Access Layer → MySQL Server → returns data
5. Server composes response (authentication result, question payload, opponent’s answer) → back to Client
6. Client Backend updates QML UI; assets played accordingly

c. Architectural patterns & principles:
- Client: Model-View separation (QML views vs. C++ backend logic)
- Event-driven communication using Qt signals/slots or callbacks
- Server: request-response cycle + concurrent client handling (thread or non-blocking I/O)
- Thin server: business logic + DB persistence
- Layered design: UI → application logic → transport → persistence

d. Key technologies & libraries:
- Qt/QML for client UI
- C++ (BackEnd.cpp) & C (Utils.c) in client
- POSIX sockets or QtNetwork for client-server comms
- C network server with libmysqlclient-dev
- MySQL 5.x/8.x for data storage

##### Diagramming recommendations
- Rounded rectangles for UI components (QML pages)
- Rectangles for code modules (BackEnd, Utils, Game Engine)
- Clouds or hexagons for external frameworks (Qt, OpenGL)
- from QML → C++ backend → network → server → DB and back
- between server’s matchmaking module and multiple client modules for PvP

##### Detail
- Server’s sub-modules: Network Listener, Authentication Service, Question Service, Matchmaking Service, Persistence Service
- Config file as a parameter source for server startup

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Application
    subgraph "Client Application"
        direction TB
        QMLUI["QML UI Layer"]:::ui
        LoginPage["LoginPage.qml"]:::ui
        GameScreen["GameScreen.qml"]:::ui
        BackendLogic["C++ Backend Layer"]:::logic
        MainCPP["main.cpp"]:::logic
        BackEndCPP["BackEnd.cpp"]:::logic
        UtilsC["Utils.c"]:::logic
        AssetMgr["Asset Manager"]:::assets
        AudioAssets["Audio Assets"]:::assets
        FontAssets["Font Assets"]:::assets
        SpriteAssets["Sprite Assets"]:::assets
        ClientNet["Network Module"]:::network
    end

    %% Server Application
    subgraph "Server Application"
        direction TB
        NetListener["Network Listener"]:::network
        GameEngine["Game Engine"]:::logic
        AuthService["Authentication Service"]:::logic
        QuestionService["Question Service"]:::logic
        MatchService["Matchmaking Service"]:::logic
        PersistService["Persistence Service"]:::logic
        DBAccess["DB Access Layer"]:::data
        ServerConfig["Config File"]:::infra
        ServerMake["Server Makefile"]:::infra
        DBSQL["Database Schema"]:::db
    end

    %% Database
    MySQLDB["MySQL Database"]:::db

    %% External Dependencies
    subgraph "External Dependencies"
        direction TB
        QtFramework["Qt Framework"]:::infra
        OpenGL["OpenGL Library"]:::infra
        MySQLClient["libmysqlclient"]:::infra
    end

    %% README
    README["README.md"]:::infra

    %% Connections Client
    QMLUI -->|uses| BackendLogic
    LoginPage -->|view| QMLUI
    GameScreen -->|view| QMLUI
    BackendLogic -->|loads assets| AssetMgr
    AssetMgr --> AudioAssets
    AssetMgr --> FontAssets
    AssetMgr --> SpriteAssets
    BackendLogic -->|sends requests| ClientNet

    %% Connections to Server
    ClientNet -->|TCP / LOGIN_REQUEST, GET_QUESTION| NetListener
    NetListener -->|dispatch| GameEngine
    GameEngine --> AuthService
    GameEngine --> QuestionService
    GameEngine --> MatchService
    GameEngine --> PersistService
    AuthService --> DBAccess
    QuestionService --> DBAccess
    MatchService --> DBAccess
    PersistService --> DBAccess
    DBAccess --> MySQLDB

    %% Server Config & Build
    ServerConfig --> NetListener
    ServerMake --> NetListener
    DBSQL --> MySQLDB

    %% External Dependencies links
    BackendLogic --> QtFramework
    ClientNet --> QtFramework
    AssetMgr --> QtFramework
    MySQLClient --> DBAccess
    OpenGL --> AssetMgr

    %% README link
    README --> QtFramework

    %% Click Events
    click QMLUI "https://github.com/ledinhtuyen/ailatrieuphu/tree/main/client/qml/"
    click LoginPage "https://github.com/ledinhtuyen/ailatrieuphu/blob/main/client/qml/LoginPage.qml"
    click GameScreen "https://github.com/ledinhtuyen/ailatrieuphu/blob/main/client/qml/GameScreen.qml"
    click MainCPP "https://github.com/ledinhtuyen/ailatrieuphu/blob/main/client/src/main.cpp"
    click BackEndCPP "https://github.com/ledinhtuyen/ailatrieuphu/blob/main/client/src/BackEnd.cpp"
    click UtilsC "https://github.com/ledinhtuyen/ailatrieuphu/blob/main/client/src/Utils.c"
    click AssetMgr "https://github.com/ledinhtuyen/ailatrieuphu/tree/main/client/assets/"
    click AudioAssets "https://github.com/ledinhtuyen/ailatrieuphu/tree/main/client/assets/AudioClip/"
    click FontAssets "https://github.com/ledinhtuyen/ailatrieuphu/tree/main/client/assets/Font/"
    click SpriteAssets "https://github.com/ledinhtuyen/ailatrieuphu/tree/main/client/assets/Sprite/"
    click ClientNet "https://github.com/ledinhtuyen/ailatrieuphu/blob/main/client/src/BackEnd.cpp"
    click NetListener "https://github.com/ledinhtuyen/ailatrieuphu/blob/main/server/server.c"
    click GameEngine "https://github.com/ledinhtuyen/ailatrieuphu/blob/main/server/server.c"
    click DBAccess "https://github.com/ledinhtuyen/ailatrieuphu/blob/main/server/server.c"
    click ServerConfig "https://github.com/ledinhtuyen/ailatrieuphu/tree/main/server/config"
    click ServerMake "https://github.com/ledinhtuyen/ailatrieuphu/tree/main/server/Makefile"
    click DBSQL "https://github.com/ledinhtuyen/ailatrieuphu/blob/main/server/database.sql"
    click README "https://github.com/ledinhtuyen/ailatrieuphu/blob/main/README.md"

    %% Styles
    classDef ui fill:#D6EAF8,stroke:#2980B9;
    classDef logic fill:#FADBD8,stroke:#C0392B;
    classDef network fill:#D5F5E3,stroke:#27AE60;
    classDef assets fill:#FDEBD0,stroke:#D35400;
    classDef data fill:#FCF3CF,stroke:#F1C40F;
    classDef db fill:#E8DAEF,stroke:#8E44AD;
    classDef infra fill:#F2F3F4,stroke:#7F8C8D;
```
