Skip to content

Commit 254439a

Browse files
committed
Battlesnake: Ignore games whose execution fails
1 parent 4eaa773 commit 254439a

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

codeclash/games/battlesnake/battlesnake.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from codeclash.agents.player import Player
99
from codeclash.constants import RESULT_TIE
1010
from codeclash.games.game import CodeGame, RoundStats
11-
from codeclash.utils.environment import assert_zero_exit_code
1211

1312

1413
class BattleSnakeGame(CodeGame):
@@ -47,14 +46,17 @@ def _wait_for_ports(self, requested_ports: list[int], timeout: float = 3.0) -> l
4746

4847
return list(available_ports)
4948

50-
def _run_single_simulation(self, cmd: str, idx: int) -> tuple[str, str]:
49+
def _run_single_simulation(self, cmd: str, idx: int) -> str:
5150
"""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",
5754
)
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"]
5860

5961
def execute_round(self, agents: list[Player]):
6062
self.logger.debug("Starting game servers")
@@ -108,11 +110,14 @@ def get_results(self, agents: list[Player], round_num: int, stats: RoundStats):
108110
if len(available_players) > 1:
109111
# We ran the game
110112
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")
116121
else:
117122
self.logger.warning(f"Only one player ({available_players[0]}) started, giving them the win")
118123
# We didn't run a game, so we just give the one player the win

0 commit comments

Comments
 (0)