# pyclashbot/py-clash-bot architecture

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

- Interactive diagram: https://gitdiagram.com/pyclashbot/py-clash-bot
- Repository: https://github.com/pyclashbot/py-clash-bot

## Overview

1. **Repository purpose and runtime shape**
   - This is a **desktop automation application** for Clash Royale, not a server or web app.
   - It runs on **Windows and macOS**, drives an **Android emulator**, and uses **image recognition** to operate the game.

2. **Primary entrypoints**
   - `pyclashbot/__main__.py` is the application entrypoint.
   - `pyclashbot/__init__.py` plus the packaging files (`pyproject.toml`, `uv.lock`) define the distributable Python app.
   - The app is launched as a local GUI-first tool, with automation running in the background.

3. **User interface boundary**
   - The `pyclashbot/interface` package is the user-facing control plane.
   - Key responsibilities:
     - emulator selection and setup
     - bot configuration
     - start/stop control
     - help and setup screens
     - widget/layout composition
   - This layer does not perform gameplay automation itself; it configures and triggers the bot runtime.

4. **Automation core**
   - The `pyclashbot/bot` package contains the gameplay logic and orchestration.
   - Major responsibilities are split across:
     - `worker.py`: long-running background execution loop
     - `states.py`: state machine for bot flow
     - `fight.py`: battle execution logic
     - `nav.py`: navigation between game screens
     - `upgrade_state.py`: card upgrade progression
     - `card_mastery_state.py`: mastery reward collection
     - `deck_*` modules: deck selection, cycling, randomization, utilities
     - `recorder.py`: likely captures/records actions or state for debugging/analysis
   - This is the main decision-making layer that coordinates perception and emulator control.

5. **Perception and screen understanding**
   - `pyclashbot/detection/image_rec.py` is the image-recognition subsystem.
   - `pyclashbot/detection/reference_images/` contains the template assets used to detect buttons, modes, and UI states.
   - The detection layer appears to identify things like:
     - battle mode selection
     - deck tabs
     - exit/post-battle buttons
     - selected game mode indicators
   - This forms the “vision” boundary between raw emulator screenshots and bot state transitions.

6. **Emulator abstraction layer**
   - `pyclashbot/emulators` abstracts the supported Android emulators:
     - **MEmu**
     - **BlueStacks**
     - **Google Play Games**
   - Common behavior is factored into base classes and ADB helpers:
     - `base.py`
     - `adb_base.py`
     - `adb.py`
   - Vendor-specific modules (`bluestacks.py`, `memu.py`, `google_play.py`) encapsulate startup/config differences.
   - `configs/memu_config.json` suggests emulator-specific launch or instance settings are part of the architecture.

7. **End-to-end data flow**
   - The user configures the bot in the GUI.
   - The interface selects an emulator and starts a worker.
   - The emulator layer launches or attaches to the Android instance.
   - Screenshots/UI state are read through the detection layer.
   - The bot state machine decides the next action.
   - Input is sent back through the emulator interface.
   - This loop repeats until stopped or a terminal condition is reached.

8. **Gameplay capabilities as separate concerns**
   - Battle automation and progression are not one monolith; they are split by task:
     - battles (`fight.py`)
     - deck behavior (`deck_cycle.py`, `deck_randomization.py`, `deck_utils.py`)
     - upgrades (`upgrade_state.py`)
     - mastery collection (`card_mastery_state.py`)
   - This separation matters for diagramming because the bot can be seen as a coordinator over multiple task-specific subflows.

9. **Support utilities**
   - `pyclashbot/utils` contains cross-cutting concerns used across the app:
     - logging
     - cancellation/token handling
     - caching
     - subprocess/platform helpers
     - version and machine info
     - image handling
     - Discord RPC integration
     - config loading
   - These are infrastructural helpers rather than primary business logic, but they affect how the app runs and integrates with the desktop environment.

10. **Assets and static resources**
   - `assets/` contains marketing/demo media used by the README and likely the project site/docs.
   - `pyclashbot/interface/assets` and `pyclashbot/detection/reference_images` are operational assets:
     - UI assets for the desktop app
     - template images for recognition
   - The detection templates are a key runtime dependency, not just documentation.

11. **OS-specific setup and packaging**
   - `scripts/setup_macos.py` and `scripts/setup_msi.py` indicate platform-specific installer/build support.
   - The `.github/workflows` files show CI for testing, release, CodeQL, and pre-commit checks.
   - This is a packaged desktop product with automated release support, not a library-only repo.

12. **External dependencies and boundaries**
   - The app depends on:
     - a locally installed emulator
     - the Clash Royale app inside that emulator
     - optionally Discord-related services for presence/webhook workflows
   - There is no central backend service in the repository; the main “external system” is the emulator/game runtime on the user’s machine.

Main entry points: [`pyclashbot/bot/states.py`](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/states.py) (State machine), [`pyclashbot/emulators/base.py`](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/emulators/base.py) (Emu base), [`pyclashbot/detection/image_rec.py`](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/detection/image_rec.py) (Image rec), [`pyclashbot/interface/ui.py`](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/interface/ui.py) (Interface)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_app["App shell"]
  node_node_main["Main entry<br/>python entrypoint<br/>[__main__.py]"]
  node_node_interface["Interface<br/>gui control plane<br/>[ui.py]"]
  node_node_widgets["Widgets<br/>ui composition<br/>[widgets.py]"]
  node_node_setup["Setup screens<br/>onboarding flows<br/>[setup.py]"]
end

subgraph group_grp_bot["Bot core"]
  node_node_worker["Worker<br/>runtime loop<br/>[worker.py]"]
  node_node_states["State machine<br/>flow control<br/>[states.py]"]
  node_node_fight["Fight logic<br/>battle automation<br/>[fight.py]"]
  node_node_nav["Navigation<br/>screen flow<br/>[nav.py]"]
  node_node_deck["Deck ops<br/>deck strategy<br/>[deck_cycle.py]"]
  node_node_upgrade["Upgrades<br/>progression task<br/>[upgrade_state.py]"]
  node_node_mastery["Mastery<br/>reward task"]
  node_node_recorder["Recorder<br/>debug capture<br/>[recorder.py]"]
end

subgraph group_grp_vision["Vision"]
  node_node_image_rec["Image rec<br/>vision engine<br/>[image_rec.py]"]
  node_node_ref_images["Templates<br/>image assets"]
end

subgraph group_grp_emu["Emulators"]
  node_node_emu_base["Emu base<br/>emulator abstraction<br/>[base.py]"]
  node_node_adb["ADB layer<br/>device control<br/>[adb.py]"]
  node_node_bluestacks["BlueStacks<br/>vendor adapter<br/>[bluestacks.py]"]
  node_node_memu["MEmu<br/>vendor adapter<br/>[memu.py]"]
  node_node_google_play["Google Play<br/>vendor adapter<br/>[google_play.py]"]
end

subgraph group_grp_utils["Utils"]
  node_node_utils["Utils<br/>runtime helpers<br/>[__init__.py]"]
end

subgraph group_grp_pkg["Packaging"]
  node_node_packaging["Packaging<br/>build and release<br/>[pyproject.toml]"]
end

node_node_main -->|"launches"| node_node_interface
node_node_interface -->|"configures"| node_node_setup
node_node_interface -->|"starts"| node_node_worker
node_node_interface -->|"reads config"| node_node_utils
node_node_worker -->|"drives"| node_node_states
node_node_states -->|"enters"| node_node_fight
node_node_states -->|"routes"| node_node_nav
node_node_states -->|"selects deck"| node_node_deck
node_node_states -->|"handles"| node_node_upgrade
node_node_states -->|"handles"| node_node_mastery
node_node_states -->|"records"| node_node_recorder
node_node_fight -->|"observes"| node_node_image_rec
node_node_nav -->|"detects"| node_node_image_rec
node_node_deck -->|"matches"| node_node_image_rec
node_node_image_rec -->|"uses templates"| node_node_ref_images
node_node_worker -->|"controls"| node_node_emu_base
node_node_emu_base -->|"delegates"| node_node_adb
node_node_emu_base -->|"adapts"| node_node_bluestacks
node_node_emu_base -->|"adapts"| node_node_memu
node_node_emu_base -->|"adapts"| node_node_google_play
node_node_emu_base -->|"depends on"| node_node_utils
node_node_image_rec -->|"depends on"| node_node_utils
node_node_packaging -->|"supports"| node_node_utils

click node_node_main "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/__main__.py"
click node_node_interface "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/interface/ui.py"
click node_node_widgets "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/interface/widgets.py"
click node_node_setup "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/interface/setup.py"
click node_node_worker "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/worker.py"
click node_node_states "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/states.py"
click node_node_fight "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/fight.py"
click node_node_nav "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/nav.py"
click node_node_deck "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/deck_cycle.py"
click node_node_upgrade "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/upgrade_state.py"
click node_node_mastery "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/card_mastery_state.py"
click node_node_recorder "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/recorder.py"
click node_node_image_rec "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/detection/image_rec.py"
click node_node_ref_images "https://github.com/pyclashbot/py-clash-bot/tree/master/pyclashbot/detection/reference_images"
click node_node_emu_base "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/emulators/base.py"
click node_node_adb "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/emulators/adb.py"
click node_node_bluestacks "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/emulators/bluestacks.py"
click node_node_memu "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/emulators/memu.py"
click node_node_google_play "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/emulators/google_play.py"
click node_node_utils "https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/utils/__init__.py"
click node_node_packaging "https://github.com/pyclashbot/py-clash-bot/blob/master/pyproject.toml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_main,node_node_interface,node_node_widgets,node_node_setup toneBlue
class node_node_worker,node_node_states,node_node_fight,node_node_nav,node_node_deck,node_node_upgrade,node_node_mastery,node_node_recorder toneAmber
class node_node_image_rec,node_node_ref_images toneMint
class node_node_emu_base,node_node_adb,node_node_bluestacks,node_node_memu,node_node_google_play toneRose
class node_node_utils toneIndigo
class node_node_packaging toneTeal
```

## Components

### App shell

Entry point and desktop control plane

- [Main entry](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/__main__.py): python entrypoint · Launches the desktop app and wires startup flow. · `pyclashbot/__main__.py`
- [Interface](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/interface/ui.py): gui control plane · User-facing configuration, setup, and start/stop controls. · `pyclashbot/interface/ui.py`
- [Widgets](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/interface/widgets.py): ui composition · Reusable interface widgets and layout building blocks. · `pyclashbot/interface/widgets.py`
- [Setup screens](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/interface/setup.py): onboarding flows · Emulator selection, setup, and help guidance. · `pyclashbot/interface/setup.py`

### Bot core

State machine and gameplay orchestration

- [Worker](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/worker.py): runtime loop · Long-running background loop that executes bot actions. · `pyclashbot/bot/worker.py`
- [State machine](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/states.py): flow control · Coordinates bot mode transitions and top-level decisions. · `pyclashbot/bot/states.py`
- [Fight logic](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/fight.py): battle automation · Executes battle behavior during matches. · `pyclashbot/bot/fight.py`
- [Navigation](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/nav.py): screen flow · Moves through game menus and screens. · `pyclashbot/bot/nav.py`
- [Deck ops](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/deck_cycle.py): deck strategy · Deck selection, cycling, randomization, and utilities. · `pyclashbot/bot/deck_cycle.py`
- [Upgrades](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/upgrade_state.py): progression task · Handles card upgrade progression states. · `pyclashbot/bot/upgrade_state.py`
- [Mastery](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/card_mastery_state.py): reward task · Collects card mastery rewards and related progression. · `pyclashbot/bot/card_mastery_state.py`
- [Recorder](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/bot/recorder.py): debug capture · Records actions or state for analysis and debugging. · `pyclashbot/bot/recorder.py`

### Vision

Image recognition and reference assets

- [Image rec](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/detection/image_rec.py): vision engine · Matches screenshots against reference images to detect UI state. · `pyclashbot/detection/image_rec.py`
- [Templates](https://github.com/pyclashbot/py-clash-bot/tree/master/pyclashbot/detection/reference_images): image assets · Reference images used for button, mode, and screen detection. · `pyclashbot/detection/reference_images`

### Emulators

Android emulator abstraction and vendor adapters

- [Emu base](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/emulators/base.py): emulator abstraction · Common emulator behavior and lifecycle abstraction. · `pyclashbot/emulators/base.py`
- [ADB layer](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/emulators/adb.py): device control · Shared ADB helpers for input, screen, and process control. · `pyclashbot/emulators/adb.py`
- [BlueStacks](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/emulators/bluestacks.py): vendor adapter · BlueStacks-specific startup and configuration handling. · `pyclashbot/emulators/bluestacks.py`
- [MEmu](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/emulators/memu.py): vendor adapter · MEmu-specific startup and configuration handling. · `pyclashbot/emulators/memu.py`
- [Google Play](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/emulators/google_play.py): vendor adapter · Google Play Games-specific startup and configuration handling. · `pyclashbot/emulators/google_play.py`

### Utils

Cross-cutting runtime helpers

- [Utils](https://github.com/pyclashbot/py-clash-bot/blob/master/pyclashbot/utils/__init__.py): runtime helpers · Logging, config, platform, subprocess, caching, and integration helpers. · `pyclashbot/utils/__init__.py`

### Packaging

Build, release, and platform setup

- [Packaging](https://github.com/pyclashbot/py-clash-bot/blob/master/pyproject.toml): build and release · Project packaging, platform setup, and release automation. · `pyproject.toml`

## Connections

- Main entry → Interface: launches · Application entrypoint opens the GUI control plane.
- Interface → Setup screens: configures · User setup and emulator selection happen in the interface.
- Interface → Worker: starts · GUI triggers the background automation worker.
- Interface → Utils: reads config · Interface relies on shared runtime helpers.
- Worker → State machine: drives · Worker runs the high-level bot state machine.
- State machine → Fight logic: enters · Battle states delegate to fight automation.
- State machine → Navigation: routes · Navigation states move through game screens.
- State machine → Deck ops: selects deck · State machine coordinates deck behavior.
- State machine → Upgrades: handles · Progression states manage upgrades.
- State machine → Mastery: handles · Progression states collect mastery rewards.
- State machine → Recorder: records · State transitions and actions can be captured for analysis.
- Fight logic → Image rec: observes · Battle logic consumes visual recognition results.
- Navigation → Image rec: detects · Screen navigation depends on image-based UI detection.
- Deck ops → Image rec: matches · Deck flow uses templates to identify tabs and modes.
- Image rec → Templates: uses templates · Vision engine compares screenshots with reference assets.
- Worker → Emu base: controls · Worker sends actions through the emulator abstraction.
- Emu base → ADB layer: delegates · Base emulator behavior uses shared ADB control paths.
- Emu base → BlueStacks: adapts · One vendor adapter implements BlueStacks specifics.
- Emu base → MEmu: adapts · One vendor adapter implements MEmu specifics.
- Emu base → Google Play: adapts · One vendor adapter implements Google Play Games specifics.
- Emu base → Utils: depends on · Emulator control uses shared helpers for subprocess and platform behavior.
- Image rec → Utils: depends on · Vision utilities support image handling and logging.
- Packaging → Utils: supports · Build and setup scripts rely on shared helpers.
