|
3 | 3 |
|
4 | 4 | import os |
5 | 5 | from argparse import ArgumentParser |
6 | | -from collections import OrderedDict |
| 6 | +from subprocess import Popen |
| 7 | +DETACHED_PROCESS = 0x00000008 |
7 | 8 |
|
8 | 9 | from crossplay_python.runner import RobotRunner |
9 | 10 | from crossplay_python.crossplay import BYTECODE_LIMIT, CrossPlayLiteral as lit, \ |
10 | 11 | CrossPlayMessage as mess, CrossPlayMethod as m, CrossPlayObjectType as ot, \ |
11 | 12 | wait, reset_files, clear_temp_files |
12 | 13 | from crossplay_python.wrappers import _GAME_METHODS, Team |
13 | 14 |
|
14 | | -CROSSPLAY_PYTHON_DIR = "example-bots/src/crossplay_python" |
| 15 | +CROSSPLAY_PYTHON_DIR = "example-bots/src/crossplay_python" # TODO change for scaffold |
15 | 16 |
|
16 | 17 | TEAM_NAMES = { |
17 | 18 | Team.A: "A", |
@@ -43,6 +44,11 @@ def format_print(*args): |
43 | 44 | return format_print |
44 | 45 |
|
45 | 46 | def play(team_a=None, team_b=None, debug=False): |
| 47 | + if team_a == "/": |
| 48 | + team_a = None |
| 49 | + if team_b == "/": |
| 50 | + team_b = None |
| 51 | + |
46 | 52 | print(f"Starting cross-play Python runner with teams {team_a} and {team_b}") |
47 | 53 |
|
48 | 54 | code = { |
@@ -79,11 +85,30 @@ def play(team_a=None, team_b=None, debug=False): |
79 | 85 | finally: |
80 | 86 | clear_temp_files() |
81 | 87 |
|
82 | | -if __name__ == "__main__": |
| 88 | +def main(): |
| 89 | + if sys.version_info.major != 3 or sys.version_info.minor != 12: |
| 90 | + print(f"Error: The Battlecode Python runner requires Python 3.12. Found version {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}.") |
| 91 | + sys.exit(1) |
| 92 | + |
83 | 93 | parser = ArgumentParser() |
84 | 94 | parser.add_argument("--teamA", help="Path to team A code file") |
85 | 95 | parser.add_argument("--teamB", help="Path to team B code file") |
86 | 96 | parser.add_argument("--debug", action="store_true", help="Enable debug mode") |
| 97 | + parser.add_argument("--new-process", action="store_true", help="Start the Python runner in a new process") |
87 | 98 | args = parser.parse_args() |
88 | 99 |
|
89 | | - play(team_a=args.teamA, team_b=args.teamB, debug=args.debug) |
| 100 | + if args.new_process: |
| 101 | + new_args = [sys.executable, __file__, |
| 102 | + "--teamA", args.teamA if args.teamA else "/", |
| 103 | + "--teamB", args.teamB if args.teamB else "/"] |
| 104 | + |
| 105 | + if args.debug: |
| 106 | + new_args.append("--debug") |
| 107 | + |
| 108 | + Popen(new_args, shell=False, stdin=None, stdout=None, stderr=None, |
| 109 | + close_fds=True, creationflags=DETACHED_PROCESS) |
| 110 | + else: |
| 111 | + play(team_a=args.teamA, team_b=args.teamB, debug=args.debug) |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + main() |
0 commit comments