Skip to content

Commit 0a5ed05

Browse files
author
Julien
committed
Added the possibility to run CommandLineAgents with full arguments
1 parent bcca611 commit 0a5ed05

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ 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
/**
@@ -24,10 +24,12 @@ public class CommandLinePlayerAgent extends Agent {
2424
*/
2525
public CommandLinePlayerAgent(String commandLine) {
2626
super();
27-
try {
28-
this.commandLine = commandLine;
29-
} catch (Exception e) {
30-
}
27+
this.commandArray = new String[] {commandLine};
28+
}
29+
30+
public CommandLinePlayerAgent(String[] commandArray) {
31+
super();
32+
this.commandArray = commandArray;
3133
}
3234

3335
@Override
@@ -47,11 +49,10 @@ protected InputStream getErrorStream() {
4749

4850
@Override
4951
public void initialize(Properties conf) {
50-
5152
try {
52-
this.process = Runtime.getRuntime().exec(commandLine);
53+
this.process = Runtime.getRuntime().exec(commandArray);
5354
} catch (IOException e) {
54-
throw new RuntimeException("Failed to launch " + commandLine, e);
55+
throw new RuntimeException("Failed to launch " + String.join(" ", commandArray));
5556
}
5657
processStdin = process.getOutputStream();
5758
processStdout = process.getInputStream();

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,53 @@ protected void buildInitCommand(Command initCommand) {
162162
}
163163
}
164164
}
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[]{"echo", "test command"}</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[]{"echo", "test command"}</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[]{"echo", "test command"}</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+
165214
}

0 commit comments

Comments
 (0)