Skip to content

Commit 69fa934

Browse files
committed
refactor(bot): migrate replay bot to use BalatroClient API
Replace direct TCP socket handling with BalatroClient for consistency with the rest of the codebase. Improves error handling and maintains the same JSONL replay functionality.
1 parent 9967b1f commit 69fa934

File tree

2 files changed

+71
-81
lines changed

2 files changed

+71
-81
lines changed

bots/replay-tcp.py

Lines changed: 0 additions & 81 deletions
This file was deleted.

bots/replay.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""Simple bot that replays actions from a run save (JSONL file)."""
2+
3+
import json
4+
import logging
5+
import sys
6+
from pathlib import Path
7+
8+
from src.balatrobot.client import BalatroClient
9+
from src.balatrobot.exceptions import BalatroError, ConnectionFailedError
10+
11+
logger = logging.getLogger(__name__)
12+
logging.basicConfig(level=logging.INFO)
13+
14+
15+
def load_steps_from_jsonl() -> list[dict]:
16+
"""Load replay steps from JSONL file."""
17+
if len(sys.argv) != 2:
18+
logger.error("Usage: python replay.py <jsonl_file>")
19+
sys.exit(1)
20+
21+
jsonl_file = Path(sys.argv[1])
22+
if not jsonl_file.exists():
23+
logger.error(f"File not found: {jsonl_file}")
24+
sys.exit(1)
25+
26+
try:
27+
with open(jsonl_file) as f:
28+
steps = [json.loads(line) for line in f if line.strip()]
29+
logger.info(f"Loaded {len(steps)} steps from {jsonl_file}")
30+
return steps
31+
except json.JSONDecodeError as e:
32+
logger.error(f"Invalid JSON in file {jsonl_file}: {e}")
33+
sys.exit(1)
34+
35+
36+
def main():
37+
"""Main replay function."""
38+
steps = load_steps_from_jsonl()
39+
40+
try:
41+
with BalatroClient() as client:
42+
logger.info("Connected to BalatroBot API")
43+
44+
# Replay each step
45+
for i, step in enumerate(steps):
46+
function_name = step["function"]["name"]
47+
arguments = step["function"]["arguments"]
48+
logger.info(f"Step {i + 1}/{len(steps)}: {function_name}({arguments})")
49+
50+
try:
51+
response = client.send_message(function_name, arguments)
52+
logger.debug(f"Response: {response}")
53+
except BalatroError as e:
54+
logger.error(f"API error in step {i + 1}: {e}")
55+
sys.exit(1)
56+
57+
logger.info("Replay completed successfully!")
58+
59+
except ConnectionFailedError as e:
60+
logger.error(f"Failed to connect to BalatroBot API: {e}")
61+
sys.exit(1)
62+
except KeyboardInterrupt:
63+
logger.info("Replay interrupted by user")
64+
sys.exit(0)
65+
except Exception as e:
66+
logger.error(f"Unexpected error during replay: {e}")
67+
sys.exit(1)
68+
69+
70+
if __name__ == "__main__":
71+
main()

0 commit comments

Comments
 (0)