Browse Source

First working build.

Matthew Clark 7 years ago
commit
8c5dd97ce8

+ 8 - 0
.classpath

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="src" path="libs"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
+	<classpathentry kind="lib" path="libs/json-20140107.jar"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

+ 17 - 0
.project

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>Vindinium</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

+ 11 - 0
.settings/org.eclipse.jdt.core.prefs

@@ -0,0 +1,11 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.7
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.7

BIN
libs/json-20140107.jar


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

@@ -0,0 +1,53 @@
+package org.h4e.libs;
+
+import java.io.BufferedReader;
+import java.io.DataOutputStream;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.ArrayList;
+
+import org.h4e.objects.NameValuePair;
+
+public class HTTP {
+
+	public static String postRequest(String url, ArrayList<NameValuePair> params)
+			throws Exception {
+
+		URL obj = new URL(url);
+		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
+
+		con.setRequestMethod("POST");
+		con.setRequestProperty("User-Agent", "Java-Run/1.0");
+		con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
+
+		String urlParameters = "";
+		if (params != null) {
+
+			for (int x = 0; x < params.size(); x++) {
+				urlParameters += params.get(x).name + "=" + params.get(x).value
+						+ "&";
+			}
+		}
+
+		con.setDoOutput(true);
+		DataOutputStream wr = new DataOutputStream(con.getOutputStream());
+		wr.writeBytes(urlParameters);
+		wr.flush();
+		wr.close();
+
+		BufferedReader in = new BufferedReader(new InputStreamReader(
+				con.getInputStream()));
+		String inputLine;
+		StringBuffer response = new StringBuffer();
+
+		while ((inputLine = in.readLine()) != null) {
+			response.append(inputLine);
+		}
+		in.close();
+
+		return response.toString();
+
+	}
+
+}

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

@@ -0,0 +1,13 @@
+package org.h4e.objects;
+
+public class NameValuePair {
+
+	public String name = null;
+	public String value = null;
+
+	public NameValuePair(String name, String value) {
+		this.name = name;
+		this.value = value;
+	}
+
+}

+ 29 - 0
src/org/h4e/vindinium/bots/FirstPath.java

@@ -0,0 +1,29 @@
+package org.h4e.vindinium.bots;
+
+import org.h4e.vindinium.config.Bots;
+import org.h4e.vindinium.loaders.Game;
+import org.h4e.vindinium.loaders.Game.CharacterMovement;
+import org.h4e.vindinium.loaders.objects.GameState;
+
+public class FirstPath {
+
+	public static void main(String[] args) {
+
+		Game g = new Game(Bots.JAppTesting);
+		g.setTurnLimit(500);
+		g.setOnMapChangedListener(mOnMapChangedListener);
+		g.startTraining();
+
+	}
+
+	private static Game.OnMapChangedListener mOnMapChangedListener = new Game.OnMapChangedListener() {
+
+		@Override
+		public CharacterMovement onMapChanged(GameState g) {
+
+			return CharacterMovement.Stay;
+
+		}
+	};
+
+}

+ 49 - 0
src/org/h4e/vindinium/bots/RandomBot.java

@@ -0,0 +1,49 @@
+package org.h4e.vindinium.bots;
+
+import java.util.Random;
+
+import org.h4e.vindinium.config.Bots;
+import org.h4e.vindinium.loaders.Game;
+import org.h4e.vindinium.loaders.Game.CharacterMovement;
+import org.h4e.vindinium.loaders.objects.GameState;
+
+public class RandomBot {
+
+	public static void main(String[] args) {
+
+		Game g = new Game(Bots.JAppTesting);
+		g.setOnMapChangedListener(mOnMapChangedListener);
+
+		g.startTraining();
+
+	}
+
+	private static Game.OnMapChangedListener mOnMapChangedListener = new Game.OnMapChangedListener() {
+
+		Random mRandom = new Random();
+
+		@Override
+		public CharacterMovement onMapChanged(GameState g) {
+			
+			
+
+			switch (mRandom.nextInt(4)) {
+			case 0:
+				return CharacterMovement.Stay;
+			case 1:
+				return CharacterMovement.North;
+			case 2:
+				return CharacterMovement.South;
+			case 3:
+				return CharacterMovement.East;
+			case 4:
+				return CharacterMovement.West;
+
+			default:
+				return CharacterMovement.Stay;
+			}
+
+		}
+	};
+
+}

+ 8 - 0
src/org/h4e/vindinium/config/Bots.java

@@ -0,0 +1,8 @@
+package org.h4e.vindinium.config;
+
+public class Bots {
+	
+	public static final String JAppTesting = "ftld2vpf";
+	public static final String tinyClark = "rxf7q6c5";
+
+}

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

@@ -0,0 +1,7 @@
+package org.h4e.vindinium.config;
+
+public class Local {
+	
+	public static final int DEF_MOVECNT = 2000;
+
+}

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

@@ -0,0 +1,8 @@
+package org.h4e.vindinium.config;
+
+public class Servers {
+
+	public static final String TRAINING = "http://vindinium.org/api/training";
+	public static final String ARENA = "http://vindinium.org/api/arena";
+
+}

+ 211 - 0
src/org/h4e/vindinium/loaders/Game.java

@@ -0,0 +1,211 @@
+package org.h4e.vindinium.loaders;
+
+import java.util.ArrayList;
+
+import org.h4e.objects.NameValuePair;
+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.GameState;
+import org.h4e.vindinium.loaders.objects.Hero;
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+public class Game {
+
+	public interface OnMapChangedListener {
+		public CharacterMovement onMapChanged(GameState g);
+	}
+
+	public enum CharacterMovement {
+		Stay, North, South, East, West;
+	}
+
+	public String mBotID = null;
+
+	public Game(String botID) {
+		mBotID = botID;
+		
+		GameState mGameState = new GameState();
+	}
+
+	OnMapChangedListener mOnMapChangedListener = null;
+
+	public void setOnMapChangedListener(
+			OnMapChangedListener onMapChangedListener) {
+		mOnMapChangedListener = onMapChangedListener;
+	}
+	
+	private int mTurnLimit = Local.DEF_MOVECNT;
+
+	public void setTurnLimit(int turnLimit) {
+		mTurnLimit = turnLimit;
+
+	}
+
+	private ServerConnection mServerConnection = null;
+
+	public void startTraining() {
+
+		new Thread() {
+
+			public void run() {
+
+				ServerExtras extras = new ServerExtras();
+				extras.map = "";
+				extras.turns = Integer.toString(mTurnLimit);
+
+				mServerConnection = new ServerConnection(ServerType.Training,
+						mBotID, extras);
+
+				try {
+
+					parseJSON(mServerConnection.waitForResponse());
+
+					System.out.println("View URL: " + mGameState.viewUrl);
+
+					doLoop();
+
+				} catch (Exception ex) {
+					ex.printStackTrace();
+					System.out.println("Could not contact server.");
+					System.exit(0);
+				}
+			}
+		}.start();
+
+	}
+
+	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.createBoard(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 void doLoop() {
+
+		if (mOnMapChangedListener != null) {
+
+			try {
+
+				do {
+
+					CharacterMovement m = mOnMapChangedListener
+							.onMapChanged(mGameState);
+
+					String dir = "";
+					switch (m) {
+					case Stay:
+						dir = "Stay";
+						break;
+					case North:
+						dir = "North";
+						break;
+					case South:
+						dir = "South";
+						break;
+					case East:
+						dir = "East";
+						break;
+					case West:
+						dir = "West";
+						break;
+					}
+
+					ArrayList<NameValuePair> postParams = new ArrayList<NameValuePair>();
+
+					postParams.add(new NameValuePair("key", mBotID));
+					postParams.add(new NameValuePair("dir", dir));
+
+					mServerConnection.setQueryURL(mGameState.playUrl);
+					parseJSON(mServerConnection.waitForResponse(postParams));
+
+				} while (mGameState.finished != true);
+
+				System.out.println("Game finished.");
+
+			} catch (Exception e) {
+				e.printStackTrace();
+				System.out.println("Could not respond to server.");
+				System.exit(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();
+
+	}
+
+}

+ 100 - 0
src/org/h4e/vindinium/loaders/ServerConnection.java

@@ -0,0 +1,100 @@
+package org.h4e.vindinium.loaders;
+
+import java.util.ArrayList;
+
+import org.h4e.libs.HTTP;
+import org.h4e.objects.NameValuePair;
+import org.h4e.vindinium.config.Local;
+import org.h4e.vindinium.config.Servers;
+
+public class ServerConnection {
+
+	public enum ServerType {
+		Training, Arena;
+	}
+
+	public static class ServerExtras {
+
+		public ServerExtras() {
+		}
+
+		public String turns = "";
+		public String map = "";
+	}
+
+	public ServerConnection(ServerType serverType, String key) {
+
+		this(serverType, key, null);
+	}
+
+	private ArrayList<NameValuePair> mPostParams = null;
+	private ServerType mServerType = null;
+
+	public ServerConnection(ServerType serverType, String key,
+			ServerExtras extras) {
+
+		mServerType = serverType;
+		mPostParams = new ArrayList<NameValuePair>();
+
+		switch (serverType) {
+		case Training:
+
+			mPostParams.add(new NameValuePair("key", key));
+
+			if (extras != null) {
+				mPostParams.add(new NameValuePair("turns", extras.turns));
+				mPostParams.add(new NameValuePair("map", extras.map));
+			} else {
+				mPostParams.add(new NameValuePair("turns", Integer
+						.toString(Local.DEF_MOVECNT)));
+			}
+
+			break;
+
+		case Arena:
+
+			mPostParams.add(new NameValuePair("key", key));
+			break;
+		}
+
+	}
+
+	public String waitForResponse() throws Exception {
+		return waitForResponse(null);
+	}
+
+	public void setQueryURL(String url) {
+		mQueryURL = url;
+	}
+
+	private String mQueryURL = null;
+
+	public String waitForResponse(ArrayList<NameValuePair> postParams)
+			throws Exception {
+
+		if (postParams != null) {
+			mPostParams = postParams;
+		}
+
+		String response = null;
+
+		if (mQueryURL == null) {
+			switch (mServerType) {
+			case Arena:
+				mQueryURL = Servers.ARENA;
+				break;
+
+			case Training:
+				mQueryURL = Servers.TRAINING;
+				break;
+			}
+		}
+
+		response = HTTP.postRequest(mQueryURL, mPostParams);
+
+		mPostParams.clear();
+		return response;
+
+	}
+
+}

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

@@ -0,0 +1,45 @@
+package org.h4e.vindinium.loaders.objects;
+
+public class GameState {
+
+	public String id = null;
+	public int turn = -1;
+	public int maxTurns = -1;
+
+	public Hero heroes[] = new Hero[4];
+
+	public char[][] board = null;
+
+	public boolean finished = false;
+
+	public Hero hero = null;
+
+	public String token = null;
+
+	public String viewUrl = null;
+	public String playUrl = null;
+	
+	
+	public GameState(){
+		
+		for(int x = 0 ; x < heroes.length; x++){
+			heroes[x] = new Hero();
+		}
+		
+		hero = new Hero();
+	}
+	
+	public void createBoard(int size, String boardDat){
+		
+		board = new char[size][];
+		
+		for (int x = 0; x < size; x++) {
+			board[x] = new char[size];
+
+			for (int y = 0; y < size; y++) {
+				board[x][y] = boardDat.charAt(x * size + y);
+			}
+		}
+	}
+
+}

+ 19 - 0
src/org/h4e/vindinium/loaders/objects/Hero.java

@@ -0,0 +1,19 @@
+package org.h4e.vindinium.loaders.objects;
+
+import java.awt.Point;
+
+public class Hero {
+
+	public int id = -1;
+	public String name = null;
+	public String userId = null;
+	public int elo = -1;
+	public Point pos = new Point(-1, -1);
+	public int life = -1;
+	public String gold = null;
+	public String mineCount = null;
+	public Point spawnpos = new Point(-1, -1);
+	public boolean crashed = false;
+	public boolean self = false;
+
+}