Skip to content

Commit df00aa4

Browse files
committed
make python package use custom directories
1 parent 72b313f commit df00aa4

2 files changed

Lines changed: 29 additions & 13 deletions

File tree

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ task crossPlayPy(type: Exec, dependsOn: [':engine:build']) {
9595
commandLine 'python', 'engine/src/crossplay_python/main.py',
9696
'--teamA', (project.findProperty('languageA') == 'java' ? '/' : project.property('teamA')),
9797
'--teamB', (project.findProperty('languageB') == 'java' ? '/' : project.property('teamB')),
98+
'--dirA', 'example-bots/src/crossplay_python',
99+
'--dirB', 'example-bots/src/crossplay_python',
98100
'--new-process'
99101
}
100102

engine/src/crossplay_python/main.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@
2020
from battlecode26.wrappers import _GAME_METHODS, Team
2121

2222
DETACHED_PROCESS = 0x00000008
23-
CROSSPLAY_PYTHON_DIR = "example-bots/src/crossplay_python" # TODO change for scaffold
24-
2523
TEAM_NAMES = {Team.A: "A", Team.B: "B", Team.NEUTRAL: "N"}
2624

2725

28-
def get_code(bot_name):
26+
def get_code(bot_name, bot_dir):
2927
if bot_name is None:
3028
return None
3129

32-
path = f"{CROSSPLAY_PYTHON_DIR}/{bot_name}"
33-
30+
path = f"{bot_dir}/{bot_name}"
3431
# read all files in this directory into a dictionary
3532
code = {}
3633

@@ -57,13 +54,13 @@ def format_print(*args):
5754
return format_print
5855

5956

60-
def play(team_a=None, team_b=None, debug=False):
57+
def play(team_a=None, team_b=None, dir_a="src", dir_b="src", debug=False):
6158
if team_a == "/":
6259
team_a = None
6360
if team_b == "/":
6461
team_b = None
6562

66-
code = {Team.A: get_code(team_a), Team.B: get_code(team_b), Team.NEUTRAL: None}
63+
code = {Team.A: get_code(team_a, dir_a), Team.B: get_code(team_b, dir_b), Team.NEUTRAL: None}
6764
active_bots: dict[int, RobotRunner] = {}
6865
current_round = 0
6966
# _spawn_queue.clear()
@@ -244,29 +241,44 @@ def main(args=None):
244241
sys.exit(1)
245242

246243
parser = ArgumentParser()
247-
parser.add_argument("--teamA", help="Path to team A code file")
248-
parser.add_argument("--teamB", help="Path to team B code file")
244+
parser.add_argument("--teamA", help="Name of team A Python bot, or '/' for no bot", default="/")
245+
parser.add_argument("--teamB", help="Name of team B Python bot, or '/' for no bot", default="/")
246+
parser.add_argument("--dirA", help="Directory for team A code", default="src")
247+
parser.add_argument("--dirB", help="Directory for team B code", default="src")
249248
parser.add_argument("--debug", action="store_true", help="Enable debug mode")
250249
parser.add_argument(
251250
"--new-process",
252251
action="store_true",
253252
help="Start the Python runner in a new process",
254253
)
255254
parsed_args = parser.parse_args(args)
255+
team_a = parsed_args.teamA
256+
team_b = parsed_args.teamB
257+
dir_a = parsed_args.dirA
258+
dir_b = parsed_args.dirB
259+
debug = parsed_args.debug
256260

257261
if parsed_args.new_process:
258262
new_args = [
259263
sys.executable,
260264
__file__,
261265
"--teamA",
262-
parsed_args.teamA if parsed_args.teamA else "/",
266+
team_a,
263267
"--teamB",
264-
parsed_args.teamB if parsed_args.teamB else "/",
268+
team_b,
269+
"--dirA",
270+
dir_a,
271+
"--dirB",
272+
dir_b,
265273
]
266274

267-
if parsed_args.debug:
275+
if debug:
268276
new_args.append("--debug")
269277

278+
# make sure that the code is valid before starting a new process
279+
get_code(team_a, dir_a)
280+
get_code(team_b, dir_b)
281+
270282
Popen(
271283
new_args,
272284
shell=False,
@@ -277,7 +289,9 @@ def main(args=None):
277289
creationflags=DETACHED_PROCESS,
278290
)
279291
else:
280-
play(team_a=parsed_args.teamA, team_b=parsed_args.teamB, debug=parsed_args.debug)
292+
play(team_a=parsed_args.teamA, team_b=parsed_args.teamB,
293+
dir_a=parsed_args.dirA, dir_b=parsed_args.dirB,
294+
debug=parsed_args.debug)
281295

282296

283297
if __name__ == "__main__":

0 commit comments

Comments
 (0)