How pyclashbot/py-clash-bot works
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: states.py State machine · base.py Emu base · image_rec.py Image rec · ui.py Interface
Components 21
App shell
Entry point and desktop control plane
- Main entry · Launches the desktop app and wires startup flow.
pyclashbot/__main__.py - Interface · User-facing configuration, setup, and start/stop controls.
pyclashbot/interface/ui.py - Widgets · Reusable interface widgets and layout building blocks.
pyclashbot/interface/widgets.py - Setup screens · Emulator selection, setup, and help guidance.
pyclashbot/interface/setup.py
Bot core
State machine and gameplay orchestration
- Worker · Long-running background loop that executes bot actions.
pyclashbot/bot/worker.py - State machine · Coordinates bot mode transitions and top-level decisions.
pyclashbot/bot/states.py - Fight logic · Executes battle behavior during matches.
pyclashbot/bot/fight.py - Navigation · Moves through game menus and screens.
pyclashbot/bot/nav.py - Deck ops · Deck selection, cycling, randomization, and utilities.
pyclashbot/bot/deck_cycle.py - Upgrades · Handles card upgrade progression states.
pyclashbot/bot/upgrade_state.py - Mastery · Collects card mastery rewards and related progression.
pyclashbot/bot/card_mastery_state.py - Recorder · Records actions or state for analysis and debugging.
pyclashbot/bot/recorder.py
Vision
Image recognition and reference assets
Emulators
Android emulator abstraction and vendor adapters
- Emu base · Common emulator behavior and lifecycle abstraction.
pyclashbot/emulators/base.py - ADB layer · Shared ADB helpers for input, screen, and process control.
pyclashbot/emulators/adb.py - BlueStacks · BlueStacks-specific startup and configuration handling.
pyclashbot/emulators/bluestacks.py - MEmu · MEmu-specific startup and configuration handling.
pyclashbot/emulators/memu.py - Google Play · Google Play Games-specific startup and configuration handling.
pyclashbot/emulators/google_play.py
Utils
Cross-cutting runtime helpers
- Utils · Logging, config, platform, subprocess, caching, and integration helpers.
pyclashbot/utils/__init__.py
Packaging
Build, release, and platform setup
- Packaging · Project packaging, platform setup, and release automation.
pyproject.toml
Connections 23
- 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.