33from pathlib import Path
44
55from codeclash .agents .abstract import Player
6+ from codeclash .constants import OUTPUTS_LOGS , OUTPUTS_RESULTS
67from codeclash .games .abstract import CodeGame
78from codeclash .utils .environment import assert_zero_exit_code
89
@@ -23,18 +24,20 @@ def __init__(self, config, *, tournament_id: str, local_output_dir: Path):
2324 self .run_cmd_round += f" --{ arg } { val } "
2425
2526 def determine_winner (
26- self , result_output : str , agents : list [Player ]
27+ self , result_outputs : list [ str ] , agents : list [Player ]
2728 ) -> dict [str , str ]:
28- self .logger .debug (f"Determining winner from result output: { result_output } " )
29- lines = result_output .strip ().split ("\n " )
30- # Get the last line which contains the game result
31- last_line = lines [- 1 ] if lines else ""
32- self .logger .debug (f"Last line: { last_line } " )
33- winner = json .loads (last_line )["winnerName" ]
34- self .logger .debug (f"Concluding winner: { winner } " )
29+ winners = []
30+ for ro in result_outputs :
31+ lines = ro .strip ().split ("\n " )
32+ # Get the last line which contains the game result
33+ last_line = lines [- 1 ] if lines else ""
34+ self .logger .debug (f"Last line: { last_line } " )
35+ winner = json .loads (last_line )["winnerName" ]
36+ winners .append (winner )
37+ winner = max (set (winners ), key = winners .count )
3538 return {"winner" : winner }
3639
37- def execute_round (self , agents : list [Player ]) -> dict [str , str ]:
40+ def execute_round (self , agents : list [Player ]) -> dict [str , list [ str ] ]:
3841 cmd = []
3942 for idx , agent in enumerate (agents ):
4043 port = 8001 + idx
@@ -46,27 +49,33 @@ def execute_round(self, agents: list[Player]) -> dict[str, str]:
4649
4750 time .sleep (3 ) # Give servers time to start
4851
49- # Create temporary output file for results
50- output_file = f"battlesnake_output_{ int (time .time ())} .json"
51- cmd_str = " " .join (cmd ) + f" -o { output_file } "
52- self .logger .info (f"Running command: { self .run_cmd_round } { cmd_str } " )
53-
5452 try :
55- response = assert_zero_exit_code (
56- self .environment .execute (
57- f"{ self .run_cmd_round } { cmd_str } " ,
58- cwd = f"{ self .environment .config .cwd } /game" ,
53+ log_outputs , result_outputs = [], []
54+ for idx in range (self .game_config ["sims_per_round" ]):
55+ # Create temporary output file for results
56+ output_file = f"battlesnake_output_{ idx } _{ int (time .time ())} .json"
57+ cmd_str = " " .join (cmd ) + f" -o { output_file } "
58+ self .logger .info (f"Running command: { self .run_cmd_round } { cmd_str } " )
59+
60+ response = assert_zero_exit_code (
61+ self .environment .execute (
62+ f"{ self .run_cmd_round } { cmd_str } " ,
63+ cwd = f"{ self .environment .config .cwd } /game" ,
64+ )
5965 )
60- )
6166
62- # Read the output file for result information
63- result_response = self .environment .execute (f"cat game/{ output_file } " )
64- result_output = result_response ["output" ]
67+ # Read the output file for result information
68+ result_response = self .environment .execute (f"cat game/{ output_file } " )
69+ result_output = result_response ["output" ]
70+ log_outputs .append (response ["output" ])
71+ result_outputs .append (result_output )
72+
73+ # Clean up the output file
74+ self .environment .execute (f"rm -f game/{ output_file } " )
6575
66- # Clean up the output file
67- self .environment .execute (f"rm -f game/{ output_file } " )
76+ time .sleep (0.1 )
6877
69- return {"log_output" : response [ "output" ], "result_output" : result_output }
78+ return {OUTPUTS_LOGS : log_outputs , OUTPUTS_RESULTS : result_outputs }
7079 finally :
7180 # Kill all python servers when done
7281 self .environment .execute ("pkill -f 'python main.py' || true" )
0 commit comments