Skip to content

Commit

Permalink
Merge pull request #54 from EntelectChallenge/feature/verbose-toggle
Browse files Browse the repository at this point in the history
Added verbose as a setting.
  • Loading branch information
ERFullard authored May 11, 2018
2 parents a041f9e + c1d9fdc commit 5b3d0ef
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 38 deletions.
1 change: 1 addition & 0 deletions game-runner/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"round-state-output-location": "./tower-defence-matches",
"game-config-file-location": "./game-config.properties",
"verbose-mode": true,
"max-runtime-ms": 2000,
"player-a": "../starter-bots/java",
"player-b": "../reference-bot/java"
Expand Down
15 changes: 15 additions & 0 deletions game-runner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -77,6 +87,11 @@
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
<repositories>
<repository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ public class Config {
@SerializedName("game-config-file-location")
public String gameConfigFileLocation;

@SerializedName("verbose-mode")
public boolean isVerbose;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;
import za.co.entelect.challenge.botrunners.BotRunner;
import za.co.entelect.challenge.botrunners.BotRunnerFactory;
import za.co.entelect.challenge.core.engine.TowerDefenseGameEngine;
import za.co.entelect.challenge.core.engine.TowerDefenseGameMapGenerator;
import za.co.entelect.challenge.core.engine.TowerDefenseRoundProcessor;
import za.co.entelect.challenge.engine.exceptions.InvalidRunnerState;
import za.co.entelect.challenge.engine.runner.GameEngineRunner;
import za.co.entelect.challenge.entities.BotMetaData;
import za.co.entelect.challenge.enums.BotLanguage;
import za.co.entelect.challenge.game.contracts.map.GameMap;
import za.co.entelect.challenge.game.contracts.player.Player;
import za.co.entelect.challenge.player.BotPlayer;
Expand All @@ -26,11 +28,13 @@
import java.util.function.Consumer;

public class GameBootstrapper {

private static final Logger log = LogManager.getLogger(GameBootstrapper.class);

private GameEngineRunner gameEngineRunner;
private static String gameName;

public static void main(String[] args) {

GameBootstrapper gameBootstrapper = new GameBootstrapper();

try {
Expand Down Expand Up @@ -87,6 +91,12 @@ private void prepareGame(Config config) throws Exception {

gameEngineRunner.preparePlayers(players);
gameEngineRunner.prepareGameMap();

if (config.isVerbose) {
Configurator.setRootLevel(Level.DEBUG);
} else {
Configurator.setRootLevel(Level.ERROR);
}
}

private void parsePlayer(String playerConfig, List<Player> players, String playerNumber, int maximumBotRuntimeMilliSeconds) throws Exception {
Expand Down Expand Up @@ -127,9 +137,9 @@ private Consumer<GameMap> getFirstPhaseHandler() {

private BiConsumer<GameMap, Integer> getRoundCompleteHandler() {
return (gameMap, round) -> {
System.out.println("=======================================");
System.out.println("Round ended " + round);
System.out.println("=======================================");
log.info("=======================================");
log.info("Round ended " + round);
log.info("=======================================");
};
}

Expand All @@ -147,13 +157,13 @@ private BiConsumer<GameMap, List<Player>> getGameCompleteHandler() {
}

if (winner == null) {
System.out.println("=======================================");
System.out.println("The game ended in a tie");
System.out.println("=======================================");
log.info("=======================================");
log.info("The game ended in a tie");
log.info("=======================================");
} else {
System.out.println("=======================================");
System.out.println("The winner is: " + winner.getName());
System.out.println("=======================================");
log.info("=======================================");
log.info("The winner is: " + winner.getName());
log.info("=======================================");
}

BufferedWriter bufferedWriter = null;
Expand All @@ -174,17 +184,17 @@ private BiConsumer<GameMap, List<Player>> getGameCompleteHandler() {

private BiConsumer<GameMap, Integer> getRoundStartingHandler() {
return (gameMap, round) -> {
System.out.println("=======================================");
System.out.println("Starting round " + round);
System.out.println("=======================================");
log.info("=======================================");
log.info("Starting round " + round);
log.info("=======================================");
};
}

private Consumer<GameMap> getGameStartedHandler() {
return gameMap -> {
System.out.println("=======================================");
System.out.println("Starting game");
System.out.println("=======================================");
log.info("=======================================");
log.info("Starting game");
log.info("=======================================");
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package za.co.entelect.challenge.engine.runner;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import za.co.entelect.challenge.core.renderers.TowerDefenseConsoleMapRenderer;
import za.co.entelect.challenge.engine.exceptions.InvalidRunnerState;
import za.co.entelect.challenge.game.contracts.command.RawCommand;
Expand All @@ -17,6 +19,8 @@

public class GameEngineRunner {

private static final Logger log = LogManager.getLogger(GameEngineRunner.class);

public Consumer<GameMap> firstPhaseHandler;
public Consumer<GameMap> gameStartedHandler;
public BiConsumer<GameMap, Integer> roundCompleteHandler;
Expand Down Expand Up @@ -95,9 +99,12 @@ private void runInitialPhase() throws Exception {
}
}


private void processRound() throws Exception {

TowerDefenseConsoleMapRenderer renderer = new TowerDefenseConsoleMapRenderer();
System.out.println(renderer.render(gameMap, players.get(0).getGamePlayer()));
//Only execute the render if the log mode is in INFO.
log.info(() -> renderer.render(gameMap, players.get(0).getGamePlayer()));

gameMap.setCurrentRound(gameMap.getCurrentRound() + 1);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package za.co.entelect.challenge.engine.runner;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import za.co.entelect.challenge.engine.exceptions.InvalidCommandException;
import za.co.entelect.challenge.engine.exceptions.InvalidOperationException;
import za.co.entelect.challenge.game.contracts.command.RawCommand;
Expand All @@ -13,6 +15,7 @@
import java.util.Hashtable;

public class RunnerRoundProcessor {
private static final Logger log = LogManager.getLogger(RunnerRoundProcessor.class);

private GameMap gameMap;
private GameRoundProcessor gameRoundProcessor;
Expand All @@ -35,7 +38,7 @@ boolean processRound() throws Exception {
boolean processed = gameRoundProcessor.processRound(gameMap, commandsToProcess);
ArrayList<String> errorList = gameRoundProcessor.getErrorList();
//TODO: Remove later
System.out.println("Error List: " + Arrays.toString(errorList.toArray()));
log.info("Error List: " + Arrays.toString(errorList.toArray()));
roundProcessed = true;

return processed;
Expand All @@ -48,7 +51,7 @@ void addPlayerCommand(Player player, RawCommand command) {

commandsToProcess.put(player.getGamePlayer(), command);
} catch (InvalidCommandException e) {
e.printStackTrace();
log.error(e.getStackTrace());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package za.co.entelect.challenge.player;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import za.co.entelect.challenge.botrunners.BotRunner;
import za.co.entelect.challenge.core.renderers.TowerDefenseConsoleMapRenderer;
import za.co.entelect.challenge.core.renderers.TowerDefenseJsonGameMapRenderer;
import za.co.entelect.challenge.core.renderers.TowerDefenseTextMapRenderer;
import za.co.entelect.challenge.engine.runner.GameEngineRunner;
import za.co.entelect.challenge.game.contracts.command.RawCommand;
import za.co.entelect.challenge.game.contracts.map.GameMap;
import za.co.entelect.challenge.game.contracts.player.Player;
Expand All @@ -24,6 +27,8 @@ public class BotPlayer extends Player {
private BotRunner botRunner;
private String saveStateLocation;

private static final Logger log = LogManager.getLogger(BotPlayer.class);

public BotPlayer(String name, BotRunner botRunner, String saveStateLocation) {
super(name);

Expand Down Expand Up @@ -66,7 +71,7 @@ public void newRoundStarted(GameMap gameMap) {
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println(String.format("File %s not found", botRunner.getBotDirectory() + "/" + BOT_COMMAND));
log.info(String.format("File %s not found", botRunner.getBotDirectory() + "/" + BOT_COMMAND));
}
try{
writeRoundStateData(playerSpecificJsonState, playerSpecificTextState,
Expand Down Expand Up @@ -121,9 +126,9 @@ private String runBot(String state, String textState) throws IOException {
try {
botConsoleOutput = botRunner.run();
}catch (IOException e){
System.out.println("Bot execution failed: " + e.getLocalizedMessage());
log.info("Bot execution failed: " + e.getLocalizedMessage());
}
System.out.println("BotRunner Started.");
log.info("BotRunner Started.");
return botConsoleOutput;
}

Expand All @@ -134,20 +139,20 @@ public void gameEnded(GameMap gameMap) {

@Override
public void playerKilled(GameMap gameMap) {
System.out.println(String.format("Player %s has been killed", getName()));
log.info(String.format("Player %s has been killed", getName()));
}

@Override
public void playerCommandFailed(GameMap gameMap, String reason) {
System.out.println(String.format("Could not process player command: %s", reason));
log.info(String.format("Could not process player command: %s", reason));
}

@Override
public void firstRoundFailed(GameMap gameMap, String reason) {
System.out.println(reason);
System.out.println("The first round has failed.");
System.out.println("The round will now restart and both players will have to try again");
System.out.println("Press any key to continue");
log.info(reason);
log.info("The first round has failed.");
log.info("The round will now restart and both players will have to try again");
log.info("Press any key to continue");

scanner.nextLine();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package za.co.entelect.challenge.player;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import za.co.entelect.challenge.core.renderers.TowerDefenseConsoleMapRenderer;
import za.co.entelect.challenge.engine.runner.GameEngineRunner;
import za.co.entelect.challenge.game.contracts.command.RawCommand;
import za.co.entelect.challenge.game.contracts.map.GameMap;
import za.co.entelect.challenge.game.contracts.player.Player;
Expand All @@ -10,6 +13,8 @@

public class ConsolePlayer extends Player {

private static final Logger log = LogManager.getLogger(ConsolePlayer.class);

private GameMapRenderer gameMapRenderer;
private Scanner scanner;

Expand All @@ -29,10 +34,10 @@ public void startGame(GameMap gameMap) {
public void newRoundStarted(GameMap gameMap) {

String output = gameMapRenderer.render(gameMap, getGamePlayer());
System.out.println(output);
log.info(output);

String inputPrompt = gameMapRenderer.commandPrompt(getGamePlayer());
System.out.println(inputPrompt);
log.info(inputPrompt);

String consoleInput = scanner.nextLine();

Expand All @@ -47,20 +52,20 @@ public void gameEnded(GameMap gameMap) {

@Override
public void playerKilled(GameMap gameMap) {
System.out.println(String.format("Player %s has been killed", getName()));
log.info(String.format("Player %s has been killed", getName()));
}

@Override
public void playerCommandFailed(GameMap gameMap, String reason) {
System.out.println(String.format("Could not process player command: %s", reason));
log.info(String.format("Could not process player command: %s", reason));
}

@Override
public void firstRoundFailed(GameMap gameMap, String reason) {
System.out.println(reason);
System.out.println("The first round has failed.");
System.out.println("The round will now restart and both players will have to try again");
System.out.println("Press any key to continue");
log.info(reason);
log.info("The first round has failed.");
log.info("The round will now restart and both players will have to try again");
log.info("Press any key to continue");

scanner.nextLine();
}
Expand Down
12 changes: 12 additions & 0 deletions game-runner/src/main/resources/log4j2.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name=PropertiesConfig
property.filename = logs
appenders = console

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %msg%n

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

0 comments on commit 5b3d0ef

Please sign in to comment.