Browse Source

Working for Arean and Training. Bad cross threading with concurent games.

Matthew Clark 7 years ago
parent
commit
a0ce334c6a

+ 9 - 0
src/org/h4e/libs/HTTP.java

@@ -11,6 +11,15 @@ import org.h4e.objects.NameValuePair;
 
 public class HTTP {
 
+	/**
+	 * 
+	 * Submit an HTTP POST Request with the specified parameters. Returns the String response.
+	 * 
+	 * @param url URL to query
+	 * @param params ArrayList of POST parameters
+	 * @return String result
+	 * @throws Exception Could not fetch result
+	 */
 	public static String postRequest(String url, ArrayList<NameValuePair> params)
 			throws Exception {
 

+ 7 - 0
src/org/h4e/objects/NameValuePair.java

@@ -5,6 +5,13 @@ public class NameValuePair {
 	public String name = null;
 	public String value = null;
 
+	/**
+	 * 
+	 * Basic String Name Value Pair
+	 * 
+	 * @param name
+	 * @param value
+	 */
 	public NameValuePair(String name, String value) {
 		this.name = name;
 		this.value = value;

+ 14 - 1
src/org/h4e/vindinium/bots/Bot.java

@@ -1,10 +1,23 @@
 package org.h4e.vindinium.bots;
 
-import org.h4e.vindinium.loaders.Game.Movement;
 import org.h4e.vindinium.loaders.objects.GameState;
+import org.h4e.vindinium.loaders.objects.Movement;
 
+/**
+ * Bot template
+ * 
+ * @author Matthew
+ * 
+ */
 public abstract class Bot {
 
+	/**
+	 * This function must be overridden to get the move from the users bot
+	 * 
+	 * @param gameState
+	 *            Current state of the game
+	 * @return Movement direction
+	 */
 	public abstract Movement onMoveRequest(GameState gameState);
 
 }

+ 3 - 0
src/org/h4e/vindinium/config/Local.java

@@ -2,7 +2,10 @@ package org.h4e.vindinium.config;
 
 public class Local {
 	
+	// Default move count
 	public static final int DEF_MOVECNT = 2000;
+	
+	// Default fail count
 	public static final int DEF_FAILCNT = 3;
 
 }

+ 3 - 0
src/org/h4e/vindinium/config/Servers.java

@@ -2,7 +2,10 @@ package org.h4e.vindinium.config;
 
 public class Servers {
 
+	// Default Training server URL
 	public static final String TRAINING = "http://vindinium.org/api/training";
+	
+	// Default Arena server URL
 	public static final String ARENA = "http://vindinium.org/api/arena";
 
 }

+ 0 - 5
src/org/h4e/vindinium/gametype/Arena.java

@@ -1,5 +0,0 @@
-package org.h4e.vindinium.gametype;
-
-public class Arena {
-
-}

+ 53 - 0
src/org/h4e/vindinium/gametype/ArenaGame.java

@@ -0,0 +1,53 @@
+package org.h4e.vindinium.gametype;
+
+import org.h4e.vindinium.bots.Bot;
+import org.h4e.vindinium.loaders.Game;
+import org.h4e.vindinium.loaders.ServerConnection;
+import org.h4e.vindinium.loaders.ServerConnection.ServerType;
+
+public class ArenaGame extends Game {
+
+	public ArenaGame(String botID, Bot bot) {
+		super(botID, bot);
+
+	}
+
+	public boolean startArena() {
+
+		return startArena(1, 1);
+	}
+
+	public boolean startArena(int sequential) {
+
+		return startArena(1, sequential);
+	}
+
+	public boolean startArena(int concurent, int sequential) {
+
+		if (concurent > 10) {
+			concurent = 10;
+			System.out.println("Concurent limit set to max of 10.");
+		}
+
+		System.out.println("Running [ " + concurent
+				+ " ] concurent bots for [ " + sequential
+				+ " ] arena games each.");
+
+		for (int x = 0; x < concurent; x++) {
+			new Thread() {
+
+				public void run() {
+
+					ArenaGame.this.setServerConnection(new ServerConnection(
+							ServerType.Arena, mBotKey));
+
+					ArenaGame.this.play();
+
+				}
+			}.start();
+		}
+		
+		return true;
+	}
+
+}

+ 0 - 5
src/org/h4e/vindinium/gametype/Training.java

@@ -1,5 +0,0 @@
-package org.h4e.vindinium.gametype;
-
-public class Training {
-
-}

+ 76 - 0
src/org/h4e/vindinium/gametype/TrainingGame.java

@@ -0,0 +1,76 @@
+package org.h4e.vindinium.gametype;
+
+import org.h4e.vindinium.bots.Bot;
+import org.h4e.vindinium.config.Local;
+import org.h4e.vindinium.loaders.Game;
+import org.h4e.vindinium.loaders.ServerConnection;
+import org.h4e.vindinium.loaders.ServerConnection.ServerExtras;
+import org.h4e.vindinium.loaders.ServerConnection.ServerType;
+
+public class TrainingGame extends Game {
+
+	private int mTurnLimit = Local.DEF_MOVECNT;
+
+	public TrainingGame(String botID, Bot bot) {
+		super(botID, bot);
+
+	}
+
+	public boolean startTraining() {
+
+		return startTraining(1, 1);
+	}
+
+	public boolean startTraining(int sequential) {
+
+		return startTraining(1, sequential);
+	}
+
+	public boolean startTraining(int concurent, final int sequential) {
+
+		if (concurent > 10) {
+			concurent = 10;
+			System.out.println("Concurent limit set to max of 10.");
+		}
+
+		System.out.println("Running [ " + concurent
+				+ " ] concurent bots for [ " + sequential
+				+ " ] training games each.");
+
+		for (int x = 0; x < concurent; x++) {
+			new Thread() {
+
+				public void run() {
+
+					for (int y = 0; y < sequential; y++) {
+
+						ServerExtras extras = new ServerExtras();
+						extras.map = "";
+						extras.turns = Integer.toString(TrainingGame.this
+								.getTurnLimit());
+
+						TrainingGame.this
+								.setServerConnection(new ServerConnection(
+										ServerType.Training, mBotKey, extras));
+
+						TrainingGame.this.play();
+
+					}
+				}
+			}.start();
+		}
+
+		return true;
+
+	}
+	
+	public void setTurnLimit(int turnLimit) {
+		mTurnLimit = turnLimit;
+
+	}
+
+	public int getTurnLimit() {
+		return mTurnLimit;
+	}
+
+}

+ 45 - 182
src/org/h4e/vindinium/loaders/Game.java

@@ -5,117 +5,66 @@ import java.util.ArrayList;
 import org.h4e.objects.NameValuePair;
 import org.h4e.vindinium.bots.Bot;
 import org.h4e.vindinium.config.Local;
-import org.h4e.vindinium.loaders.ServerConnection.ServerExtras;
-import org.h4e.vindinium.loaders.ServerConnection.ServerType;
-import org.h4e.vindinium.loaders.objects.Board;
 import org.h4e.vindinium.loaders.objects.GameState;
-import org.h4e.vindinium.loaders.objects.Hero;
-import org.json.JSONArray;
-import org.json.JSONObject;
+import org.h4e.vindinium.loaders.objects.Movement;
 
 public class Game {
 
-	public enum Movement {
-		Stay, North, South, East, West;
-	}
-
-	public String mBotID = null;
+	public String mBotKey = null;
+	private Bot mBot = null;
+	private GameState mGameState = null;
 
-	public Game(String botID) {
-		mBotID = botID;
+	private ServerConnection mServerConnection = null;
+	private boolean mRunning = false;
+	private int mFailCount = 0;
+	private int mMaxFailCount = Local.DEF_FAILCNT;
 
-		GameState mGameState = new GameState();
-	}
+	/**
+	 * Create a new game by passing in the API key, and the bot to be used
+	 * 
+	 * @param botKey
+	 * @param bot
+	 */
+	public Game(String botKey, Bot bot) {
 
-	public GameState getGameState() {
-		return mGameState;
+		mBotKey = botKey;
+		mBot = bot;
 	}
 
-	private int mTurnLimit = Local.DEF_MOVECNT;
-
-	public void setTurnLimit(int turnLimit) {
-		mTurnLimit = turnLimit;
-
+	public void setServerConnection(ServerConnection con) {
+		mServerConnection = con;
 	}
 
-	private ServerConnection mServerConnection = null;
-	private boolean mRunning = false;
-
-	public boolean startTraining() {
-		return startTraining(1, 1);
+	public ServerConnection getServerConnection() {
+		return mServerConnection;
 	}
 
-	public boolean startTraining(int sequential) {
-		return startTraining(1, sequential);
+	public GameState getGameState() {
+		return mGameState;
 	}
 
-	private Bot mBot = null;
-
 	public void setBot(Bot bot) {
 		mBot = bot;
 	}
 
-	public boolean startTraining(int concurent, final int sequential) {
-
-		if (mRunning) {
-			System.out.println("Already running.");
-			return false;
-		} else {
-			mRunning = true;
-		}
-
-		if (mBot == null) {
-			System.out.println("Set the bot before starting the simulation.");
-			System.exit(0);
-		}
-
-		if (concurent > 10) {
-			concurent = 10;
-			System.out.println("Concurent limit set to max of 10.");
-		}
-
-		System.out.println("Running [ " + concurent
-				+ " ] concurent bots for [ " + sequential + " ] games each.");
-
-		for (int x = 0; x < concurent; x++) {
-			new Thread() {
-
-				public void run() {
-
-					for (int y = 0; y < sequential; y++) {
-
-						mRunning = true;
-
-						ServerExtras extras = new ServerExtras();
-						extras.map = "";
-						extras.turns = Integer.toString(mTurnLimit);
-
-						mServerConnection = new ServerConnection(
-								ServerType.Training, mBotID, extras);
-
-						playGame();
-
-					}
-				}
-			}.start();
-		}
-
-		return true;
-
-	}
-
-	private boolean playGame() {
+	public boolean play() {
 
 		try {
 
-			parseJSON(mServerConnection.waitForResponse());
+			System.out.println("Waiting for game to begin.");
+
+			GameState g = mServerConnection.waitForGameState();
 
-			System.out.println("View URL: " + mGameState.viewUrl);
+			System.out.println("Starting game. ");
+			System.out.println("View URL: " + g.viewUrl);
 
-			return doLoop();
+			return doLoop(g);
 
 		} catch (Exception e) {
 
+			e.printStackTrace();
+			System.out.println("Aborting game [ " + " ].");
+
 			return false;
 		}
 	}
@@ -124,67 +73,7 @@ public class Game {
 		mMaxFailCount = max;
 	}
 
-	private int mMaxFailCount = Local.DEF_FAILCNT;
-
-	private void parseJSON(String json) {
-
-		JSONObject response = new JSONObject(json);
-
-		JSONObject game = response.getJSONObject("game");
-
-		mGameState = new GameState();
-
-		mGameState.id = game.getString("id");
-		mGameState.turn = game.getInt("turn");
-		mGameState.maxTurns = game.getInt("maxTurns");
-
-		JSONArray heroes = game.getJSONArray("heroes");
-		for (int x = 0; x < heroes.length(); x++) {
-			mGameState.heroes[x] = new Hero();
-
-			mGameState.heroes[x].id = heroes.getJSONObject(x).getInt("id");
-			mGameState.heroes[x].name = heroes.getJSONObject(x).getString(
-					"name");
-
-			try {
-				mGameState.heroes[x].userId = heroes.getJSONObject(x)
-						.getString("userId");
-			} catch (Exception e) {
-			}
-
-			try {
-				mGameState.heroes[x].elo = heroes.getJSONObject(x)
-						.getInt("elo");
-			} catch (Exception e) {
-			}
-
-			mGameState.heroes[x].pos.x = heroes.getJSONObject(x)
-					.getJSONObject("pos").getInt("x");
-			mGameState.heroes[x].pos.y = heroes.getJSONObject(x)
-					.getJSONObject("pos").getInt("y");
-			mGameState.heroes[x].life = heroes.getJSONObject(x).getInt("life");
-		}
-
-		JSONObject board = game.getJSONObject("board");
-
-		mGameState.board = Board.GenerateBoard(board.getInt("size"),
-				board.getString("tiles"));
-
-		mGameState.finished = game.getBoolean("finished");
-
-		JSONObject hero = response.getJSONObject("hero");
-		int heroId = hero.getInt("id");
-		mGameState.heroes[heroId].self = true;
-
-		mGameState.token = response.getString("token");
-		mGameState.viewUrl = response.getString("viewUrl");
-		mGameState.playUrl = response.getString("playUrl");
-
-	}
-
-	private GameState mGameState = null;
-
-	public boolean doLoop() {
+	private boolean doLoop(GameState g) {
 
 		try {
 
@@ -213,59 +102,33 @@ public class Game {
 
 				ArrayList<NameValuePair> postParams = new ArrayList<NameValuePair>();
 
-				postParams.add(new NameValuePair("key", mBotID));
+				postParams.add(new NameValuePair("key", mBotKey));
 				postParams.add(new NameValuePair("dir", dir));
 
-				mServerConnection.setQueryURL(mGameState.playUrl);
-				parseJSON(mServerConnection.waitForResponse(postParams));
+				mServerConnection.setQueryURL(g.playUrl);
 
-			} while (mGameState.finished != true);
+				g = mServerConnection.waitForGameState(postParams);
+
+			} while (!g.finished);
+
+			System.out.println("Game [ " + g.id + " ] finished.");
 
-			System.out.println("Game finished.");
 			return true;
 
 		} catch (Exception e) {
 
-			System.out.println("Could not respond to server. Aboting game.");
+			System.out.println("Could not respond to server. Aboting game [ "
+					+ g.id + " ].");
 			mFailCount++;
 
 			if (mFailCount < mMaxFailCount) {
-				return playGame();
+				return play();
 			} else {
 				System.out.println("Reached max fail count. Aboting thread.");
+				mFailCount = 0;
 				return false;
 			}
 		}
 
 	}
-
-	private int mFailCount = 0;
-
-	public void startArena() {
-
-		new Thread() {
-
-			public void run() {
-
-				mServerConnection = new ServerConnection(ServerType.Arena,
-						mBotID);
-
-				try {
-
-					parseJSON(mServerConnection.waitForResponse());
-
-					System.out.println("View URL: " + mGameState.viewUrl);
-
-					doLoop();
-
-				} catch (Exception ex) {
-
-					System.out.println("Could not contact server.");
-					System.exit(0);
-				}
-			}
-		}.start();
-
-	}
-
 }

+ 5 - 4
src/org/h4e/vindinium/loaders/ServerConnection.java

@@ -6,6 +6,7 @@ import org.h4e.libs.HTTP;
 import org.h4e.objects.NameValuePair;
 import org.h4e.vindinium.config.Local;
 import org.h4e.vindinium.config.Servers;
+import org.h4e.vindinium.loaders.objects.GameState;
 
 public class ServerConnection {
 
@@ -59,8 +60,8 @@ public class ServerConnection {
 
 	}
 
-	public String waitForResponse() throws Exception {
-		return waitForResponse(null);
+	public GameState waitForGameState() throws Exception {
+		return waitForGameState(null);
 	}
 
 	public void setQueryURL(String url) {
@@ -69,7 +70,7 @@ public class ServerConnection {
 
 	private String mQueryURL = null;
 
-	public String waitForResponse(ArrayList<NameValuePair> postParams)
+	public GameState waitForGameState(ArrayList<NameValuePair> postParams)
 			throws Exception {
 
 		if (postParams != null) {
@@ -93,7 +94,7 @@ public class ServerConnection {
 		response = HTTP.postRequest(mQueryURL, mPostParams);
 
 		mPostParams.clear();
-		return response;
+		return GameState.ParseJSON(response);
 
 	}
 

+ 61 - 0
src/org/h4e/vindinium/loaders/objects/GameState.java

@@ -1,6 +1,8 @@
 package org.h4e.vindinium.loaders.objects;
 
 import org.h4e.vindinium.loaders.objects.Board.Tile;
+import org.json.JSONArray;
+import org.json.JSONObject;
 
 public class GameState {
 
@@ -30,4 +32,63 @@ public class GameState {
 		hero = new Hero();
 	}
 
+	public static GameState ParseJSON(String json) {
+
+		GameState gameState = new GameState();
+
+		JSONObject response = new JSONObject(json);
+
+		JSONObject game = response.getJSONObject("game");
+
+		gameState = new GameState();
+
+		gameState.id = game.getString("id");
+		gameState.turn = game.getInt("turn");
+		gameState.maxTurns = game.getInt("maxTurns");
+
+		JSONArray heroes = game.getJSONArray("heroes");
+		for (int x = 0; x < heroes.length(); x++) {
+			gameState.heroes[x] = new Hero();
+
+			gameState.heroes[x].id = heroes.getJSONObject(x).getInt("id");
+			gameState.heroes[x].name = heroes.getJSONObject(x)
+					.getString("name");
+
+			try {
+				gameState.heroes[x].userId = heroes.getJSONObject(x).getString(
+						"userId");
+			} catch (Exception e) {
+			}
+
+			try {
+				gameState.heroes[x].elo = heroes.getJSONObject(x).getInt("elo");
+			} catch (Exception e) {
+			}
+
+			gameState.heroes[x].pos.x = heroes.getJSONObject(x)
+					.getJSONObject("pos").getInt("x");
+			gameState.heroes[x].pos.y = heroes.getJSONObject(x)
+					.getJSONObject("pos").getInt("y");
+			gameState.heroes[x].life = heroes.getJSONObject(x).getInt("life");
+		}
+
+		JSONObject board = game.getJSONObject("board");
+
+		gameState.board = Board.GenerateBoard(board.getInt("size"),
+				board.getString("tiles"));
+
+		gameState.finished = game.getBoolean("finished");
+
+		JSONObject hero = response.getJSONObject("hero");
+		int heroId = hero.getInt("id");
+		gameState.heroes[heroId-1].self = true;
+
+		gameState.token = response.getString("token");
+		gameState.viewUrl = response.getString("viewUrl");
+		gameState.playUrl = response.getString("playUrl");
+
+		return gameState;
+
+	}
+
 }

+ 5 - 0
src/org/h4e/vindinium/loaders/objects/Movement.java

@@ -0,0 +1,5 @@
+package org.h4e.vindinium.loaders.objects;
+
+public enum Movement {
+	Stay, North, South, East, West;
+}