Skip to content

Commit e38cbd4

Browse files
committed
feat(bots): improve replay script with proper CLI
1 parent 58922f2 commit e38cbd4

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

bots/replay.py

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Simple bot that replays actions from a run save (JSONL file)."""
22

3+
import argparse
34
import json
45
import logging
56
import sys
7+
import time
68
from pathlib import Path
79

810
from balatrobot.client import BalatroClient
@@ -12,30 +14,66 @@
1214
logging.basicConfig(level=logging.INFO)
1315

1416

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>")
17+
def get_most_recent_jsonl() -> Path:
18+
"""Find the most recent JSONL file in the runs directory."""
19+
runs_dir = Path("runs")
20+
if not runs_dir.exists():
21+
logger.error("Runs directory not found")
22+
sys.exit(1)
23+
24+
jsonl_files = list(runs_dir.glob("*.jsonl"))
25+
if not jsonl_files:
26+
logger.error("No JSONL files found in runs directory")
1927
sys.exit(1)
2028

21-
jsonl_file = Path(sys.argv[1])
22-
if not jsonl_file.exists():
23-
logger.error(f"File not found: {jsonl_file}")
29+
# Sort by modification time, most recent first
30+
most_recent = max(jsonl_files, key=lambda f: f.stat().st_mtime)
31+
return most_recent
32+
33+
34+
def load_steps_from_jsonl(jsonl_path: Path) -> list[dict]:
35+
"""Load replay steps from JSONL file."""
36+
if not jsonl_path.exists():
37+
logger.error(f"File not found: {jsonl_path}")
2438
sys.exit(1)
2539

2640
try:
27-
with open(jsonl_file) as f:
41+
with open(jsonl_path) as f:
2842
steps = [json.loads(line) for line in f if line.strip()]
29-
logger.info(f"Loaded {len(steps)} steps from {jsonl_file}")
43+
logger.info(f"Loaded {len(steps)} steps from {jsonl_path}")
3044
return steps
3145
except json.JSONDecodeError as e:
32-
logger.error(f"Invalid JSON in file {jsonl_file}: {e}")
46+
logger.error(f"Invalid JSON in file {jsonl_path}: {e}")
3347
sys.exit(1)
3448

3549

3650
def main():
3751
"""Main replay function."""
38-
steps = load_steps_from_jsonl()
52+
parser = argparse.ArgumentParser(description="Replay actions from a JSONL run file")
53+
parser.add_argument(
54+
"--delay",
55+
"-d",
56+
type=float,
57+
default=0.0,
58+
help="Delay between played moves in seconds (default: 0.0)",
59+
)
60+
parser.add_argument(
61+
"--path",
62+
"-p",
63+
type=Path,
64+
help="Path to JSONL run file (default: most recent file in runs/)",
65+
)
66+
67+
args = parser.parse_args()
68+
69+
# Determine the path to use
70+
if args.path:
71+
jsonl_path = args.path
72+
else:
73+
jsonl_path = get_most_recent_jsonl()
74+
logger.info(f"Using most recent file: {jsonl_path}")
75+
76+
steps = load_steps_from_jsonl(jsonl_path)
3977

4078
try:
4179
with BalatroClient() as client:
@@ -46,6 +84,7 @@ def main():
4684
function_name = step["function"]["name"]
4785
arguments = step["function"]["arguments"]
4886
logger.info(f"Step {i + 1}/{len(steps)}: {function_name}({arguments})")
87+
time.sleep(args.delay)
4988

5089
try:
5190
response = client.send_message(function_name, arguments)

0 commit comments

Comments
 (0)