Skip to content

Commit 001087c

Browse files
Merge branch 'sdk-new-agent-constructor' into 'master'
[FIX][SDK CommandLineAgents can now be run with complex command lines See merge request codingame/game-engine!217
2 parents 0c50f57 + 8a5ca6b commit 001087c

File tree

2 files changed

+79
-18
lines changed

2 files changed

+79
-18
lines changed

runner/src/main/java/com/codingame/gameengine/runner/CommandLinePlayerAgent.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,29 @@ public class CommandLinePlayerAgent extends Agent {
1313
private OutputStream processStdin;
1414
private InputStream processStdout;
1515
private InputStream processStderr;
16-
private String commandLine;
16+
private String[] commandArray;
1717
private Process process;
1818

1919
/**
2020
* Creates an Agent for your game, will run the given commandLine at game start
2121
*
2222
* @param commandLine
23-
* the commandLine to run
23+
* the command line to run
2424
*/
2525
public CommandLinePlayerAgent(String commandLine) {
2626
super();
27-
try {
28-
this.commandLine = commandLine;
29-
} catch (Exception e) {
30-
}
27+
this.commandArray = commandLine.split(" ");
28+
}
29+
30+
/**
31+
* Creates an Agent for your game, will run the given commandLine at game start
32+
*
33+
* @param commandArray
34+
* the command array to run
35+
*/
36+
public CommandLinePlayerAgent(String[] commandArray) {
37+
super();
38+
this.commandArray = commandArray;
3139
}
3240

3341
@Override
@@ -47,11 +55,10 @@ protected InputStream getErrorStream() {
4755

4856
@Override
4957
public void initialize(Properties conf) {
50-
5158
try {
52-
this.process = Runtime.getRuntime().exec(commandLine);
59+
this.process = Runtime.getRuntime().exec(commandArray);
5360
} catch (IOException e) {
54-
throw new RuntimeException("Failed to launch " + commandLine, e);
61+
throw new RuntimeException("Failed to launch " + String.join(" ", commandArray));
5562
}
5663
processStdin = process.getOutputStream();
5764
processStdout = process.getInputStream();

runner/src/main/java/com/codingame/gameengine/runner/MultiplayerGameRunner.java

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@ public MultiplayerGameRunner() {
2020

2121
/**
2222
* Sets the league level to run. The first league is 1.
23-
* <p>The value can also be set by setting the environment variable <code>league.level</code>.</p>
24-
* @param leagueLevel the league level. 1 is the lowest level and default value.
23+
* <p>
24+
* The value can also be set by setting the environment variable <code>league.level</code>.
25+
* </p>
26+
*
27+
* @param leagueLevel
28+
* the league level. 1 is the lowest level and default value.
2529
*/
26-
30+
2731
public void setLeagueLevel(int leagueLevel) {
28-
if (leagueLevel < 1 || leagueLevel >= 20) {
29-
throw new IllegalArgumentException("League level must be higher than 0 and lesser than 20");
30-
}
31-
System.setProperty("league.level", String.valueOf(leagueLevel));
32+
if (leagueLevel < 1 || leagueLevel >= 20) {
33+
throw new IllegalArgumentException("League level must be higher than 0 and lesser than 20");
34+
}
35+
System.setProperty("league.level", String.valueOf(leagueLevel));
3236
}
3337

3438
/**
@@ -43,7 +47,8 @@ public void setLeagueLevel(int leagueLevel) {
4347
* If those parameters are present in the given input, the input values should override the generated values.
4448
* </p>
4549
*
46-
* @param seed this game's seed returned by the <code>GameManager</code> during execution
50+
* @param seed
51+
* this game's seed returned by the <code>GameManager</code> during execution
4752
*/
4853
public void setSeed(Long seed) {
4954
this.seed = seed;
@@ -119,7 +124,7 @@ public void addAgent(Class<?> playerClass, String nickname, String avatarUrl) {
119124
public void addAgent(String commandLine, String nickname, String avatarUrl) {
120125
addAgent(new CommandLinePlayerAgent(commandLine), nickname, avatarUrl);
121126
}
122-
127+
123128
/**
124129
* Adds an AI to the next game to run, with the specified nickname.
125130
*
@@ -157,4 +162,53 @@ protected void buildInitCommand(Command initCommand) {
157162
}
158163
}
159164
}
165+
166+
/**
167+
* Adds an AI to the next game to run.
168+
* <p>
169+
* The given command array will be executed with <code>Runtime.getRuntime().exec()</code>.
170+
* </p>
171+
* Example: <code>new String[]{"bash", "-c", "echo command1 && echo command2"}</code>
172+
*
173+
* @param commandArray
174+
* the system command array to run the AI.
175+
* @param nickname
176+
* the player's nickname
177+
* @param avatarUrl
178+
* the url of the player's avatar
179+
*/
180+
public void addAgent(String[] commandArray, String nickname, String avatarUrl) {
181+
addAgent(new CommandLinePlayerAgent(commandArray), nickname, avatarUrl);
182+
}
183+
184+
/**
185+
* Adds an AI to the next game to run.
186+
* <p>
187+
* The given command array will be executed with <code>Runtime.getRuntime().exec()</code>.
188+
* </p>
189+
* Example: <code>new String[]{"bash", "-c", "echo command1 && echo command2"}</code>
190+
*
191+
* @param commandArray
192+
* the system command array to run the AI.
193+
* @param nickname
194+
* the player's nickname
195+
*/
196+
public void addAgent(String[] commandArray, String nickname) {
197+
addAgent(commandArray, nickname, null);
198+
}
199+
200+
/**
201+
* Adds an AI to the next game to run.
202+
* <p>
203+
* The given command array will be executed with <code>Runtime.getRuntime().exec()</code>.
204+
* </p>
205+
* Example: <code>new String[]{"bash", "-c", "echo command1 && echo command2"}</code>
206+
*
207+
* @param commandArray
208+
* the system command array to run the AI.
209+
*/
210+
public void addAgent(String[] commandArray) {
211+
addAgent(commandArray, null, null);
212+
}
213+
160214
}

0 commit comments

Comments
 (0)