Skip to content

Commit 1c1a301

Browse files
committed
add gradle task to run the crossplay engine in a single command
1 parent ad1bbb7 commit 1c1a301

4 files changed

Lines changed: 41 additions & 8 deletions

File tree

build.gradle

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,19 @@ task headless(type: JavaExec, dependsOn: [':engine:build', ':example-bots:build'
9292
}
9393

9494
task crossPlayPy(type: Exec, dependsOn: [':engine:build']) {
95-
commandLine 'python', 'engine/src/crossplay_python/main.py', '--teamA', project.property('teamA'), '--teamB', project.property('teamB')
95+
commandLine 'python', 'engine/src/crossplay_python/main.py',
96+
'--teamA', (project.findProperty('languageA') == 'java' ? '/' : project.property('teamA')),
97+
'--teamB', (project.findProperty('languageB') == 'java' ? '/' : project.property('teamB')),
98+
'--new-process'
9699
}
97100

101+
task headlessPy(dependsOn: ['headless']) {}
102+
headlessPy.mustRunAfter crossPlayPy
103+
98104
// keep the client happy because it references this step
99105
task unpackClient() {}
100106

101-
task run(dependsOn: ['headless', 'unpackClient']) {}
107+
task run(dependsOn: ['crossPlayPy', 'headlessPy', 'unpackClient']) {}
102108

103109
task runClient {
104110
doLast {

engine/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ dependencies {
5555

5656
[group: 'net.sf.trove4j', name: 'trove4j', version: '2.1.0'],
5757

58-
[group: 'org.json', name: 'json', version: '20250517'],
58+
[group: 'org.json', name: 'json', version: '20251224'],
5959
)
6060
// implementation files('lib/jsi-1.0.jar')
6161

engine/src/crossplay_python/main.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33

44
import os
55
from argparse import ArgumentParser
6-
from collections import OrderedDict
6+
from subprocess import Popen
7+
DETACHED_PROCESS = 0x00000008
78

89
from crossplay_python.runner import RobotRunner
910
from crossplay_python.crossplay import BYTECODE_LIMIT, CrossPlayLiteral as lit, \
1011
CrossPlayMessage as mess, CrossPlayMethod as m, CrossPlayObjectType as ot, \
1112
wait, reset_files, clear_temp_files
1213
from crossplay_python.wrappers import _GAME_METHODS, Team
1314

14-
CROSSPLAY_PYTHON_DIR = "example-bots/src/crossplay_python"
15+
CROSSPLAY_PYTHON_DIR = "example-bots/src/crossplay_python" # TODO change for scaffold
1516

1617
TEAM_NAMES = {
1718
Team.A: "A",
@@ -43,6 +44,11 @@ def format_print(*args):
4344
return format_print
4445

4546
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+
4652
print(f"Starting cross-play Python runner with teams {team_a} and {team_b}")
4753

4854
code = {
@@ -79,11 +85,30 @@ def play(team_a=None, team_b=None, debug=False):
7985
finally:
8086
clear_temp_files()
8187

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+
8393
parser = ArgumentParser()
8494
parser.add_argument("--teamA", help="Path to team A code file")
8595
parser.add_argument("--teamB", help="Path to team B code file")
8696
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")
8798
args = parser.parse_args()
8899

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()

gradle.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# What are you trying to run?
22

3+
maps=DefaultSmall
34
teamA=examplefuncsplayer
45
teamB=examplefuncsplayer
5-
maps=DefaultSmall
6+
languageA=java
7+
languageB=java
68

79
# Some optional configurations you can adjust as you need
810

0 commit comments

Comments
 (0)