|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +This project requires that Balatro Game is running in the background. To run the game use the following command: |
| 6 | + |
| 7 | +```bash |
| 8 | +# Basic command (may hang on crash) |
| 9 | +./balatro.sh |
| 10 | + |
| 11 | +# Recommended: Run with logging and startup wait |
| 12 | +./balatro.sh > balatro_game.log 2>&1 & sleep 10 && echo "Balatro started and ready" |
| 13 | +``` |
| 14 | + |
| 15 | +### Game Monitoring Commands |
| 16 | + |
| 17 | +```bash |
| 18 | +# Check if game is running |
| 19 | +ps aux | grep run_lovely_macos |
| 20 | + |
| 21 | +# View game logs |
| 22 | +tail -f balatro_game.log |
| 23 | + |
| 24 | +# View monitor logs |
| 25 | +tail -f balatro_monitor.log |
| 26 | + |
| 27 | +# Kill game process |
| 28 | +pkill -f run_lovely_macos |
| 29 | +``` |
| 30 | + |
| 31 | +## Development Commands |
| 32 | + |
| 33 | +### Linting and Type Checking |
| 34 | + |
| 35 | +```bash |
| 36 | +# Run ruff linter and formatter |
| 37 | +ruff check . |
| 38 | +ruff format . |
| 39 | + |
| 40 | +# Run type checker |
| 41 | +basedpyright |
| 42 | +``` |
| 43 | + |
| 44 | +### Testing |
| 45 | + |
| 46 | +**IMPORTANT**: Tests require Balatro to be running in the background. Always start the game before running tests. |
| 47 | + |
| 48 | +```bash |
| 49 | +# Run all tests (requires Balatro to be running) |
| 50 | +pytest |
| 51 | + |
| 52 | +# Run specific test file |
| 53 | +pytest tests/test_api_functions.py |
| 54 | + |
| 55 | +# Run tests with verbose output |
| 56 | +pytest -v |
| 57 | +``` |
| 58 | + |
| 59 | +#### Test Prerequisites and Workflow |
| 60 | + |
| 61 | +1. **Always start Balatro first**: |
| 62 | + ```bash |
| 63 | + # Check if game is running |
| 64 | + ps aux | grep run_lovely_macos |
| 65 | + |
| 66 | +# Start if not running |
| 67 | + ./balatro.sh > balatro_game.log 2>&1 & sleep 10 && echo "Balatro started and ready" |
| 68 | + ``` |
| 69 | + |
| 70 | +2. **Monitor game startup**: |
| 71 | + ```bash |
| 72 | + # Check logs for successful mod loading |
| 73 | + tail -f balatro_game.log |
| 74 | + |
| 75 | + # Look for these success indicators: |
| 76 | + # - "BalatrobotAPI initialized" |
| 77 | + # - "BalatroBot loaded - version X.X.X" |
| 78 | + # - "UDP socket created on port 12346" |
| 79 | + ``` |
| 80 | + |
| 81 | +3. **Common startup issues and fixes**: |
| 82 | + - **Syntax errors in balatrobot.lua**: Check for corrupted text like `asldkfjalksdjlocal` that should be `local` |
| 83 | + - **Game crashes on mod load**: Review full log for Lua stack traces |
| 84 | + - **Steam connection warnings**: Can be ignored - game works without Steam in development |
| 85 | + - **JSON metadata errors**: Normal for development files (.vscode, .luarc.json) - can be ignored |
| 86 | + |
| 87 | +4. **Test execution**: |
| 88 | + - **Test suite**: 31 tests covering API functions and UDP communication |
| 89 | + - **Execution time**: ~56 seconds (includes game state transitions) |
| 90 | + - **Coverage**: API function calls, socket communication, error handling, edge cases |
| 91 | + |
| 92 | +5. **Troubleshooting test failures**: |
| 93 | + - **Connection timeouts**: Ensure UDP port 12346 is available |
| 94 | + - **Game state errors**: Check if game is responsive and not crashed |
| 95 | + - **Invalid responses**: Verify mod loaded correctly by checking logs |
| 96 | + |
| 97 | +### Documentation |
| 98 | + |
| 99 | +```bash |
| 100 | +# Serve documentation locally |
| 101 | +mkdocs serve |
| 102 | + |
| 103 | +# Build documentation |
| 104 | +mkdocs build |
| 105 | +``` |
| 106 | + |
| 107 | +### Bot Development |
| 108 | + |
| 109 | +```bash |
| 110 | +# Run the example bot |
| 111 | +python bots/example.py |
| 112 | + |
| 113 | +# Run bot with different log levels |
| 114 | +python bots/example.py --log DEBUG |
| 115 | +python bots/example.py --log INFO |
| 116 | +``` |
| 117 | + |
| 118 | +## Architecture Overview |
| 119 | + |
| 120 | +BalatroBot is a Python framework for developing automated bots to play the card game Balatro. The architecture consists of three main layers: |
| 121 | + |
| 122 | +### 1. Communication Layer (UDP Protocol) |
| 123 | + |
| 124 | +- **Lua API** (`src/lua/api.lua`): Game-side mod that handles socket communication |
| 125 | +- **UDP Socket Communication**: Real-time bidirectional communication between game and bot |
| 126 | +- **Protocol**: Bot sends "HELLO" → Game responds with JSON state → Bot sends action strings |
| 127 | + |
| 128 | +### 2. Python Framework Layer (`src/balatrobot/`) |
| 129 | + |
| 130 | +**NOTE**: This is the old implementation that is being heavily refactored without backwards compatibility. |
| 131 | +It will be drastically simplified in the future. For the moment I'm just focusing on the Lua API (`src/lua/api.lua`). |
| 132 | +I keep the old code around for reference. |
| 133 | + |
| 134 | +- **Bot Base Class** (`base.py`): Abstract base class defining the bot interface |
| 135 | +- **ActionSchema**: TypedDict defining structured action format with `action` (enum) and `args` (list) |
| 136 | +- **Enums** (`enums.py`): Game state enums (Actions, Decks, Stakes, State) |
| 137 | +- **Socket Management**: Automatic reconnection, timeout handling, JSON parsing |
| 138 | + |
| 139 | +## Development Standards |
| 140 | + |
| 141 | +### Python Code Style (from `.cursor/rules/`) |
| 142 | + |
| 143 | +- Use modern Python 3.12+ syntax with built-in collection types |
| 144 | +- Type annotations with pipe operator for unions: `str | int | None` |
| 145 | +- Use `type` statement for type aliases |
| 146 | +- Google-style docstrings without type information (since type annotations are present) |
| 147 | +- Modern generic class syntax: `class Container[T]:` |
| 148 | + |
| 149 | +## Project Structure Context |
| 150 | + |
| 151 | +- **Dual Implementation**: Both Python framework and Lua game mod |
| 152 | +- **UDP Communication**: Port 12346 for real-time game interaction |
| 153 | +- **MkDocs Documentation**: Comprehensive guides with Material theme |
| 154 | +- **Pytest Testing**: UDP socket testing with fixtures |
| 155 | +- **Development Tools**: Ruff, basedpyright, modern Python tooling |
| 156 | + |
| 157 | +### Testing Best Practices |
| 158 | + |
| 159 | +- **Always check that Balatro is running before running tests** |
| 160 | +- After starting Balatro, check the log to confirm successful startup |
0 commit comments