|
8 | 8 | from codeclash.agents.player import Player |
9 | 9 | from codeclash.constants import RESULT_TIE |
10 | 10 | from codeclash.games.game import CodeGame, RoundStats |
11 | | -from codeclash.utils.environment import assert_zero_exit_code |
12 | 11 |
|
13 | 12 |
|
14 | 13 | class BattleSnakeGame(CodeGame): |
@@ -47,14 +46,17 @@ def _wait_for_ports(self, requested_ports: list[int], timeout: float = 3.0) -> l |
47 | 46 |
|
48 | 47 | return list(available_ports) |
49 | 48 |
|
50 | | - def _run_single_simulation(self, cmd: str, idx: int) -> tuple[str, str]: |
| 49 | + def _run_single_simulation(self, cmd: str, idx: int) -> str: |
51 | 50 | """Run a single battlesnake simulation and return log and result outputs.""" |
52 | | - assert_zero_exit_code( |
53 | | - self.environment.execute( |
54 | | - cmd + f" -o {self.log_env / f'sim_{idx}.jsonl'}", |
55 | | - cwd=f"{self.environment.config.cwd}/game", |
56 | | - ) |
| 51 | + output = self.environment.execute( |
| 52 | + cmd + f" -o {self.log_env / f'sim_{idx}.jsonl'}", |
| 53 | + cwd=f"{self.environment.config.cwd}/game", |
57 | 54 | ) |
| 55 | + if output["returncode"] != 0: |
| 56 | + self.logger.warning( |
| 57 | + f"Battlesnake simulation failed with exit code {output['returncode']}:\n{output['output']}" |
| 58 | + ) |
| 59 | + return output["output"] |
58 | 60 |
|
59 | 61 | def execute_round(self, agents: list[Player]): |
60 | 62 | self.logger.debug("Starting game servers") |
@@ -108,11 +110,14 @@ def get_results(self, agents: list[Player], round_num: int, stats: RoundStats): |
108 | 110 | if len(available_players) > 1: |
109 | 111 | # We ran the game |
110 | 112 | for idx in range(self.game_config["sims_per_round"]): |
111 | | - with open(self.log_round(round_num) / f"sim_{idx}.jsonl") as f: |
112 | | - lines = f.read().strip().split("\n") |
113 | | - results = json.loads(lines[-1]) # Get the last line which contains the game result |
114 | | - winner = RESULT_TIE if results["isDraw"] else results["winnerName"] |
115 | | - scores[winner] = scores.get(winner, 0) + 1 |
| 113 | + try: |
| 114 | + with open(self.log_round(round_num) / f"sim_{idx}.jsonl") as f: |
| 115 | + lines = f.read().strip().split("\n") |
| 116 | + results = json.loads(lines[-1]) # Get the last line which contains the game result |
| 117 | + winner = RESULT_TIE if results["isDraw"] else results["winnerName"] |
| 118 | + scores[winner] = scores.get(winner, 0) + 1 |
| 119 | + except FileNotFoundError: |
| 120 | + self.logger.warning(f"Simulation {idx} not found, skipping") |
116 | 121 | else: |
117 | 122 | self.logger.warning(f"Only one player ({available_players[0]}) started, giving them the win") |
118 | 123 | # We didn't run a game, so we just give the one player the win |
|
0 commit comments