# stefan2200/twb architecture

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

- Interactive diagram: https://gitdiagram.com/stefan2200/twb
- Repository: https://github.com/stefan2200/twb

## Overview

##### Project type and purpose
- This is a Python-based “bot” for the browser game Tribal Wars. It provides both a CLI‐driven automation engine (twb.py/manager.py) and an optional Flask-based web dashboard (webmanager/). The core purpose is to scrape the game via HTTP (using your browser cookie for authentication), extract game state, make decisions (manage villages, troops, buildings, etc.), and post commands back to the game server automatically.

##### File structure
Top-level entrypoints:
- twb.py – main CLI bot
- manager.py – secondary CLI to adjust configuration
- installer.bat / start.bat – Windows helpers

Core packages:
- core/ – low-level services (HTTP requests, file I/O, templating, notifications, updates, reporting)
- game/ – domain logic (village, troops, map, attacks/defense, simulation, resource management)
- pages/ – page‐specific scraper definitions (overview.py, etc.)

Web UI:
- webmanager/ – Flask app (server.py), templates, static assets, helper code

Other:
- templates/ – text templates to generate game commands (building, offensive, troops)
- config.example.json & readme/configs.md – configuration for endpoints, villages, strategies
- requirements.txt – dependencies

##### README for additional insights
- Describes features (building, defense, troop, market, research management, etc.), usage flow (CLI startup, cookie string for auth, auto-reload config), and optional web dashboard on Flask at localhost:5000.

##### Elements
- UI components (e.g., blue)
- Core services (e.g., green)
- Domain logic (e.g., orange)
- Data stores & templates (e.g., gray)
- External systems (e.g., red)

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User Interfaces"
        CLI["CLI Bot"]:::ui
        ConfigCLI["CLI Config Manager"]:::ui
        WebDash["Web Dashboard"]:::ui
    end

    subgraph "Application Layer"
        subgraph "Core Services"
            Req["HTTP Client"]:::core
            FileMgr["File Manager"]:::core
            TemplSvc["Templating Service"]:::core
            Notif["Notification Service"]:::core
            Updater["Updater Service"]:::core
            Report["Reporting Service"]:::core
            Except["Exception Definitions"]:::core
            TWStats["TW-Stats Helper"]:::core
        end
        subgraph "Domain Services"
            Building["Building Manager"]:::domain
            Troop["Troop Manager"]:::domain
            Attack["Attack Service"]:::domain
            Defence["Defense Manager"]:::domain
            Hunter["Hunter Service"]:::domain
            Snob["Snobber Service"]:::domain
            MapMod["Map Module"]:::domain
            Sim["Simulation Service"]:::domain
            Resource["Resource Manager"]:::domain
            Village["Village Entity"]:::domain
            ReportParser["Report Parser"]:::domain
        end
        subgraph "Page Extractors"
            Overview["Overview Extractor"]:::core
        end
    end

    subgraph "Data Layer"
        ConfigFile["Config & State"]:::data
        Templates["Templates"]:::data
    end

    ExternalServer["Tribal Wars Servers"]:::external

    CLI -->|uses| Req
    CLI -->|reads/writes| ConfigFile
    ConfigCLI -->|manages| ConfigFile
    WebDash -->|reads| ConfigFile
    WebDash -->|uses| FileMgr
    WebDash -->|renders| Templates
    Req -->|GET/POST| ExternalServer
    ExternalServer -->|"HTML pages"| Overview
    Overview -->|"parsed data"| Building
    Overview -->|"parsed data"| Troop
    Overview -->|"parsed data"| Attack
    Overview -->|"parsed data"| Defence
    Overview -->|"parsed data"| Hunter
    Overview -->|"parsed data"| Snob
    Overview -->|"parsed data"| MapMod
    Overview -->|"parsed data"| Sim
    Overview -->|"parsed data"| Resource
    Overview -->|"parsed data"| Village
    Overview -->|"parsed data"| ReportParser
    Building -->|generate commands| TemplSvc
    Troop -->|generate commands| TemplSvc
    Attack -->|generate commands| TemplSvc
    Defence -->|generate commands| TemplSvc
    Hunter -->|generate commands| TemplSvc
    Snob -->|generate commands| TemplSvc
    Resource -->|generate commands| TemplSvc
    Sim -->|uses| MapMod
    ReportParser -->|logs| Report
    TemplSvc -->|templates| Templates
    TemplSvc -->|renders| Req
    Building -->|store state| FileMgr
    Troop -->|store state| FileMgr
    Report -->|writes| ConfigFile
    Updater -->|checks updates| ExternalServer
    Updater -->|updates| FileMgr

    click CLI "https://github.com/stefan2200/twb/blob/master/twb.py"
    click ConfigCLI "https://github.com/stefan2200/twb/blob/master/manager.py"
    click Installer "https://github.com/stefan2200/twb/blob/master/installer.bat"
    click Starter "https://github.com/stefan2200/twb/blob/master/start.bat"
    click ConfigFile "https://github.com/stefan2200/twb/blob/master/config.example.json"
    click Req "https://github.com/stefan2200/twb/blob/master/core/request.py"
    click FileMgr "https://github.com/stefan2200/twb/blob/master/core/filemanager.py"
    click TemplSvc "https://github.com/stefan2200/twb/blob/master/core/templates.py"
    click Notif "https://github.com/stefan2200/twb/blob/master/core/notification.py"
    click Report "https://github.com/stefan2200/twb/blob/master/core/reporter.py"
    click Updater "https://github.com/stefan2200/twb/blob/master/core/updater.py"
    click Except "https://github.com/stefan2200/twb/blob/master/core/exceptions.py"
    click TWStats "https://github.com/stefan2200/twb/blob/master/core/twstats.py"
    click Building "https://github.com/stefan2200/twb/blob/master/game/buildingmanager.py"
    click Troop "https://github.com/stefan2200/twb/blob/master/game/troopmanager.py"
    click Attack "https://github.com/stefan2200/twb/blob/master/game/attack.py"
    click Defence "https://github.com/stefan2200/twb/blob/master/game/defence_manager.py"
    click Hunter "https://github.com/stefan2200/twb/blob/master/game/hunter.py"
    click Snob "https://github.com/stefan2200/twb/blob/master/game/snobber.py"
    click MapMod "https://github.com/stefan2200/twb/blob/master/game/map.py"
    click Sim "https://github.com/stefan2200/twb/blob/master/game/simulator.py"
    click Resource "https://github.com/stefan2200/twb/blob/master/game/resources.py"
    click Village "https://github.com/stefan2200/twb/blob/master/game/village.py"
    click ReportParser "https://github.com/stefan2200/twb/blob/master/game/reports.py"
    click Overview "https://github.com/stefan2200/twb/blob/master/pages/overview.py"
    click Templates "https://github.com/stefan2200/twb/tree/master/templates/builder/"
    click TemplatesOff "https://github.com/stefan2200/twb/tree/master/templates/offensive/"
    click TemplatesTroop "https://github.com/stefan2200/twb/tree/master/templates/troops/"
    click WebDash "https://github.com/stefan2200/twb/blob/master/webmanager/server.py"
    click Utils "https://github.com/stefan2200/twb/blob/master/webmanager/utils.py"
    click HelpFile "https://github.com/stefan2200/twb/blob/master/webmanager/helpfile.py"
    click Static "https://github.com/stefan2200/twb/tree/master/webmanager/static/"
    click WebTemplates "https://github.com/stefan2200/twb/tree/master/webmanager/templates/"
    click Requirements "https://github.com/stefan2200/twb/blob/master/requirements.txt"
    click README "https://github.com/stefan2200/twb/blob/master/README.md"
    click ConfigDocs "https://github.com/stefan2200/twb/blob/master/readme/configs.md"

    classDef ui fill:#D0E8FF,stroke:#0366d6,stroke-width:1px
    classDef core fill:#E0F8E0,stroke:#28a745,stroke-width:1px
    classDef domain fill:#FFF5E5,stroke:#d58512,stroke-width:1px
    classDef data fill:#F0F0F0,stroke:#6a737d,stroke-width:1px
    classDef external fill:#FDE0E0,stroke:#d73a49,stroke-width:1px
```
