11#!/usr/bin/env python3
22
33import asyncio , itertools , os , sys
4- from argparse import ArgumentParser
4+ from argparse import ArgumentParser , REMAINDER
55from pathlib import Path
66from subprocess import run
77from time import time as t
@@ -52,13 +52,22 @@ def explore(out_dir):
5252 return ai_paths
5353
5454def display_name (bot , player ):
55+ if isinstance (player , str ):
56+ return player
57+
5558 user = bot .get_user (int (player .name ))
5659 assert user
5760 return user .display_name
5861
5962async def safe_game (game , semaphore , bot , log , progress : Progress , players , args ):
6063 async with semaphore :
61- players , winner , errors = await game .module .main (list (players ) + args , discord = True )
64+ try :
65+ players , winner , errors = await game .module .main (list (players ) + args , discord = True )
66+ except SystemExit as e :
67+ # Wrong arguments
68+ players = [bot .get_user (int (player .split ("_" )[1 ].split ('.' )[0 ])).name for player in players ]
69+ winner , errors = None , {player : "SystemExit" for player in players }
70+
6271 progress .log_results (bot , log , players , winner , errors )
6372 await progress .update_message ()
6473 return players , winner
@@ -67,7 +76,7 @@ def make(game, *args):
6776 args = * ("make" , "--directory" , str (game .game_path )), * args
6877 run (args , capture_output = True )
6978
70- async def tournament (ctx , game , rematches , nb_players , src_dir , args ):
79+ async def tournament (ctx , game , nb_players , src_dir , args , games_args = None ):
7180
7281 await ctx .channel .send (content = f"Starting **{ game .name } ** tournament" )
7382
@@ -93,11 +102,15 @@ async def tournament(ctx, game, rematches, nb_players, src_dir, args):
93102
94103 log_file = game .log_file .open ("w" )
95104 progress = Progress ()
105+ if not games_args :
106+ games_args = ["" ]
96107
97- for combinations in itertools .combinations (map (str , ai_files ), nb_players ):
98- for players in itertools .permutations (combinations ):
99- for _ in range (rematches ):
100- games .append (safe_game (game , semaphore , ctx .bot , log_file , progress , players , args ))
108+ for combinations in itertools .combinations (map (str , ai_files ), nb_players ): # All combinations of players
109+ for players in itertools .permutations (combinations ): # Each starting configuration
110+ for current_args in games_args :
111+ if isinstance (current_args , str ):
112+ current_args = current_args .split ()
113+ games .append (safe_game (game , semaphore , ctx .bot , log_file , progress , players , args + current_args ))
101114
102115 progress .nb_games = len (games )
103116 shuffle (games )
@@ -130,11 +143,14 @@ async def tournament(ctx, game, rematches, nb_players, src_dir, args):
130143async def main (ctx , game , raw_args = None ) -> list [tuple ]:
131144
132145 parser = ArgumentParser ()
133- parser .add_argument ("-r" , "--rematches" , type = int , default = 1 , metavar = "NB_REMATCHES" )
134146 parser .add_argument ("-p" , "--players" , type = int , default = 2 , metavar = "NB_PLAYERS" )
135- parser .add_argument ("-d" , "--directory" , default = AvailableGame .AI_DIR_NAME , metavar = "SRC_DIRECTORY" )
147+ parser .add_argument ("-d" , "--directory" , default = AvailableGame .RESULT_DIR_NAME , metavar = "SRC_DIRECTORY" )
148+ parser .add_argument ("-g" , "--games_args" , nargs = REMAINDER , metavar = "ARGS" , default = [])
136149
137- args , remaining_args = parser .parse_known_args (raw_args )
150+ try :
151+ args , remaining_args = parser .parse_known_args (raw_args )
152+ except SystemExit :
153+ return None
138154 src_dir = game .game_path / args .directory
139155
140- return await tournament (ctx , game , args .rematches , args . players , src_dir , remaining_args )
156+ return await tournament (ctx , game , args .players , src_dir , remaining_args , args . games_args )
0 commit comments