Skip to content

Commit

Permalink
Brutal tester supporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Akarachudra committed May 17, 2020
1 parent 0cdb338 commit 762330b
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 1 deletion.
59 changes: 59 additions & 0 deletions pom.xml
Expand Up @@ -43,5 +43,64 @@
<artifactId>module-endscreen</artifactId>
<version>${gamengine.version}</version>
</dependency>

<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>

<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.codingame.gameengine.runner.CommandLineInterface</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.codingame.gameengine.runner.CommandLineInterface</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>

</project>
2 changes: 1 addition & 1 deletion src/main/java/com/codingame/game/Referee.java
Expand Up @@ -36,7 +36,7 @@ public void init() {
this.seed = gameManager.getSeed();

// Set configuration depending on game rules:
Config.setDefaultValueByLevel(LeagueRules.fromIndex(gameManager.getLeagueLevel()));
Config.setDefaultValueByLevel(LeagueRules.fromIndex(4));

// Override configuration with game parameters:
if (System.getProperty("allow.config.override") != null) {
Expand Down
111 changes: 111 additions & 0 deletions src/main/java/com/codingame/gameegine/runner/CommandLineInterface.java
@@ -0,0 +1,111 @@
package com.codingame.gameengine.runner;

import com.codingame.gameengine.runner.MultiplayerGameRunner;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;

import com.codingame.gameengine.runner.dto.GameResult;
import com.google.common.io.Files;

public class CommandLineInterface {

public static void main(String[] args) {
try {
Options options = new Options();

options.addOption("h", false, "Print the help")
.addOption("p1", true, "Required. Player 1 command line.")
.addOption("p2", true, "Required. Player 2 command line.")
.addOption("s", false, "Server mode")
.addOption("l", true, "File output for logs")
.addOption("d", true, "Referee initial data/seed");

CommandLine cmd = new DefaultParser().parse(options, args);

if (cmd.hasOption("h") || !cmd.hasOption("p1") || !cmd.hasOption("p2")) {
new HelpFormatter().printHelp(
"-p1 <player1 command line> -p2 <player2 command line> [-s -l <File output for logs> -d <seed>]",
options);
System.exit(0);
}

MultiplayerGameRunner runner = new MultiplayerGameRunner();

Field getGameResult = GameRunner.class.getDeclaredField("gameResult");
getGameResult.setAccessible(true);
GameResult result = (GameResult) getGameResult.get(runner);

if (cmd.hasOption("d")) {
runner.setSeed(Long.valueOf(cmd.getOptionValue("d").replaceAll("seed=","")));
}

int playerCount = 0;

for (int i = 1; i <= 2; ++i) {
if (cmd.hasOption("p" + i)) {
runner.addAgent(cmd.getOptionValue("p" + i), cmd.getOptionValue("p" + i));
playerCount += 1;
}
}


if (cmd.hasOption("s")) {
runner.start();
} else {
Method initialize = GameRunner.class.getDeclaredMethod("initialize", Properties.class);
initialize.setAccessible(true);
initialize.invoke(runner, new Properties());

Method runAgents = GameRunner.class.getDeclaredMethod("runAgents");
runAgents.setAccessible(true);
runAgents.invoke(runner);

if (cmd.hasOption("l")) {
Method getJSONResult = GameRunner.class.getDeclaredMethod("getJSONResult");
getJSONResult.setAccessible(true);

Files.asCharSink(Paths.get(cmd.getOptionValue("l")).toFile(), Charset.defaultCharset())
.write((String) getJSONResult.invoke(runner));
}

for (int i = 0; i < playerCount; ++i) {
System.out.println(result.scores.get(i));
}

for (String line : result.uinput) {
System.out.println(line);
}
}

// We have to clean players process properly
Field getPlayers = GameRunner.class.getDeclaredField("players");
getPlayers.setAccessible(true);
@SuppressWarnings("unchecked")
List<Agent> players = (List<Agent>) getPlayers.get(runner);

if (players != null) {
for (Agent player : players) {
Field getProcess = CommandLinePlayerAgent.class.getDeclaredField("process");
getProcess.setAccessible(true);
Process process = (Process) getProcess.get(player);

process.destroy();
}
}
} catch (Exception e) {
System.err.println(e);
e.printStackTrace(System.err);
System.exit(1);
}
}

}
11 changes: 11 additions & 0 deletions src/main/resources/log4j2.properties
@@ -0,0 +1,11 @@
# Configuration
name = PropertiesConfig
status = WARN

appender.console.type = Console
appender.console.name = CONSOLE
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{HH:mm:ss}] %-5p : %c{1} - %m%n

rootLogger.level = warn
rootLogger.appenderRef.console.ref = CONSOLE

0 comments on commit 762330b

Please sign in to comment.