# zhblue/hustoj architecture

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

- Interactive diagram: https://gitdiagram.com/zhblue/hustoj
- Repository: https://github.com/zhblue/hustoj

## Overview

##### Main Components
a. Judge Engine Layer:
- The “trunk/core” directory shows C/C++ code for judging tasks. It includes “judged” (the central judging daemon), “judge_client” (the component that runs in the sandbox environment to compile and run user submissions), and the “sim” subdirectory (for similarity checking and possibly plagiarism detection).
b. Web Frontend Layer:
- The “trunk/web” directory is huge and contains PHP files, admin panels, various templates (syzoj, bs3, sweet, bshark, mdui, sidebar, etc.), and other assets. This is the web application that provides user interfaces for problem submission, contest management, discussion, and announcements.
c. Database Layer:
- Though not directly visible as a directory, installation scripts and documentation refer to MySQL/MariaDB and even SQLite3 support. The web code (in “db_info.inc.php”) clearly ties in with a backend database for storing problems, submissions, users, and contest data.
d. Container and Deployment Components:
- The “docker” and “kubernetes” directories contain Dockerfiles, entrypoint scripts, and YAML definitions. These components indicate that the system supports containerized deployment, allowing for scalable judge workers and web services.
e. Administration and Build/CI:
- The “.github”, “.gitlab-ci.yml” and various build scripts (like build.sh) imply that continuous integration and automated builds are in place. This ensures that components (especially the judge engine binaries) are compiled correctly across various platforms.

##### Relationships and Data Flow
- The Web Server (PHP application in trunk/web) interacts with the Database (MySQL/MariaDB) to retrieve/store contest, problem, user, and submission data.
- The Web Frontend submits judging jobs to the judge engine layer. This is typically achieved by enqueuing a submission request, which is then picked up by the “judged” daemon.
- The “judged” core process (written in C++) manages job distribution to one or more instances of “judge_client”, which likely run inside sandboxes (or even separate Docker containers) to compile and run submitted code.
- The “sim” directory holds the similarity or plagiarism detection components that may compare source codes after submissions and reports results back to the web interface.
- Deployment components (docker, kubernetes) serve to encapsulate these services into isolated containers that communicate over defined network ports and interfaces.

##### Architectural Patterns & Design Principles
- Modular separation: The system is clearly segregated into independent modules – a web frontend, a judging backend (with its own client–server architecture), and auxiliary modules (like similarity checking).
- Containerization: Use of Dockerfiles and Kubernetes YAML files indicates that independent services can be deployed as containers that communicate via APIs (or message queues), enhancing scalability and reliability.
- CI/CD Integration: Presence of GitHub workflows and GitLab pipelines suggests continuous integration practices, which helps in automated building, testing, and deployment.

##### Technologies, Frameworks, and Libraries
- Server-side: PHP (for front-end and admin modules) and underlying web servers such as Nginx (or Apache) as shown by configuration guidance.
- Judge engine: C/C++ code compiled for various architectures, with makefiles and platform-specific optimizations.
- Database: MySQL/MariaDB (with support hinted at for SQLite in certain modules).
- Containerization/orchestration: Docker and Kubernetes for containerized deployments.
- Frontend Libraries: Support for multiple templates/themes (syzoj, bs3, sweet, etc.) that rely on Bootstrap, ACE editor, KindEditor, and more.
- Additional utilities: Various open-source libraries like PHPMailer, codemirror, katex, and mdui are referenced via the README and file structure.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Web Frontend Layer
    subgraph "Web Frontend Layer"
        WA["Web Interface (PHP)"]:::web
        DBconf["DB Config (db_info.inc.php)"]:::config
    end

    %% Database Layer
    subgraph "Database Layer"
        DB["Database (MySQL/MariaDB)"]:::db
    end

    %% Judge Engine Layer
    subgraph "Judge Engine Layer"
        JD["Judge Daemon (judged)"]:::judge
        JC["Judge Client (sandbox)"]:::judge
        SIM["Similarity Checker (sim)"]:::judge
    end

    %% Container & Deployment
    subgraph "Container & Deployment"
        Docker["Docker Deployment"]:::deployment
        K8s["Kubernetes Deployment"]:::deployment
    end

    %% Administration & CI/CD
    subgraph "Administration & CI/CD"
        CICD["CI/CD & Build Scripts"]:::ci
    end

    %% Connections
    WA -->|"submit_job"| JD
    JD -->|"dispatch_to"| JC
    JD -->|"trigger_sim"| SIM
    WA <-->|"persist_data"| DB
    Docker --> JD
    Docker --> WA
    K8s --> JD
    K8s --> WA
    CICD --> JD
    CICD --> WA
    CICD --> Docker
    CICD --> K8s

    %% Class definitions for colors
    classDef web fill:#f1c40f,stroke:#000,stroke-width:2px;
    classDef config fill:#e67e22,stroke:#000,stroke-width:2px;
    classDef db fill:#2ecc71,stroke:#000,stroke-width:2px;
    classDef judge fill:#e74c3c,stroke:#000,stroke-width:2px;
    classDef deployment fill:#3498db,stroke:#000,stroke-width:2px;
    classDef ci fill:#9b59b6,stroke:#000,stroke-width:2px;

    %% Click Events
    click JD "https://github.com/zhblue/hustoj/blob/master/trunk/core/judged/judged.cc"
    click JC "https://github.com/zhblue/hustoj/blob/master/trunk/core/judge_client/judge_client.cc"
    click SIM "https://github.com/zhblue/hustoj/tree/master/trunk/core/sim"
    click WA "https://github.com/zhblue/hustoj/tree/master/trunk/web"
    click DBconf "https://github.com/zhblue/hustoj/blob/master/trunk/web/include/db_info.inc.php"
    click DB "https://github.com/zhblue/hustoj/blob/master/docs/Composition-Database.md"
    click Docker "https://github.com/zhblue/hustoj/tree/master/docker"
    click K8s "https://github.com/zhblue/hustoj/tree/master/kubernetes"
    click CICD "https://github.com/zhblue/hustoj/tree/master/.github/workflows"
    click CICD "https://github.com/zhblue/hustoj/blob/master/.gitlab-ci.yml"
    click CICD "https://github.com/zhblue/hustoj/blob/master/build.sh"
```
