The start of the IPC transers to the engine hedgeroid
authorXeli
Thu, 04 Aug 2011 17:37:40 +0200
branchhedgeroid
changeset 5475 06a87ff38ffb
parent 5473 a68f900c4e8c
child 5477 b420afbc20d4
The start of the IPC transers to the engine
project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/EngineProtocolNetwork.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/GameConfig.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/EngineProtocolNetwork.java	Thu Aug 04 17:37:40 2011 +0200
@@ -0,0 +1,138 @@
+package org.hedgewars.mobile.EngineProtocol;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+import android.util.Log;
+
+public class EngineProtocolNetwork implements Runnable{
+
+	public static final String GAMEMODE_LOCAL = "TL";
+	public static final String GAMEMODE_DEMO = "TD";
+	public static final String GAMEMODE_NET = "TN";
+	public static final String GAMEMODE_SAVE = "TS";
+	
+	public static final int BUFFER_SIZE = 255; //From iOS code which got it from the origional frontend
+	
+	public static final int MODE_GENLANDPREVIEW = 0;
+	public static final int MODE_GAME = 1;
+
+	private ServerSocket serverSocket;
+	private InputStream input;
+	private OutputStream output;
+	public int port;
+	private final GameConfig config;
+
+	public EngineProtocolNetwork(GameConfig _config){
+		config = _config;
+		try {
+			serverSocket = new ServerSocket(0);
+			port = serverSocket.getLocalPort();
+			Thread ipcThread = new Thread(this, "IPC - Thread");			
+			ipcThread.start();
+		} catch (UnknownHostException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	public void run(){
+		//if(mode == MODE_GENLANDPREVIEW) genLandPreviewIPC();
+		/*else if (mode == MODE_GAME)*/ gameIPC();
+	}
+	
+	private void genLandPreviewIPC(){
+		
+	}
+	
+	private void gameIPC(){
+		try{
+			Socket sock = serverSocket.accept();
+			input = sock.getInputStream();
+			output = sock.getOutputStream();
+			
+			boolean clientQuit = false;
+			int msgSize = 0;
+			byte[] buffer = new byte[BUFFER_SIZE];
+
+			while(!clientQuit){
+				msgSize = 0;
+
+				input.read(buffer, 0, 1);
+				msgSize = buffer[0];
+
+				input.read(buffer, 0, msgSize);
+
+				switch(buffer[0]){
+				case 'C'://game init
+					config.sendToEngine(this);
+					break;
+				case '?'://ping - pong
+					sendToEngine("!");
+					break;
+				case 'E'://error - quits game
+
+					break;
+				case 'e':
+
+					break;
+				case 'i'://game statistics
+					switch(buffer[1]){
+					case 'r'://winning team
+						break;
+					case 'D'://best shot
+						break;
+					case 'k'://best hedgehog
+						break;
+					case 'K'://# hogs killed
+						break;
+					case 'H'://team health graph
+						break;
+					case 'T':// local team stats
+						break;
+					case 'P'://teams ranking
+						break;
+					case 's'://self damage
+						break;
+					case 'S'://friendly fire
+						break;
+					case 'B'://turn skipped
+						break;
+					default:
+
+					}
+					break;
+				case 'q'://game ended remove save file
+
+					break;
+				case 'Q'://game ended but not finished
+
+					break;
+				}
+
+			}
+
+		}catch(IOException e){
+			e.printStackTrace();
+		}
+	}
+
+	public void sendToEngine(String s){
+		int length = s.length();
+		
+		try {
+			output.write(length);
+			output.write(s.getBytes(), 0, length);
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		
+		
+	}
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/GameConfig.java	Thu Aug 04 17:37:40 2011 +0200
@@ -0,0 +1,102 @@
+package org.hedgewars.mobile.EngineProtocol;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.UUID;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.util.Log;
+
+public class GameConfig implements Parcelable{
+	
+	public GameMode mode = GameMode.MODE_LOCAL;
+	public Map map = null;
+	public String theme = null;
+	public Scheme scheme = null;
+	public Weapon weapon = null;
+	
+	public String mission = null;
+	public String seed = null;
+	
+	public ArrayList<Team> teams = new ArrayList<Team>(8);
+	
+	public GameConfig(){
+		
+	}
+	
+	public GameConfig(Parcel in){
+		readFromParcel(in);	
+	}
+	
+
+	
+	public void sendToEngine(EngineProtocolNetwork epn) throws IOException{
+		Log.d("HW_Frontend", "Sending Gameconfig...");
+		int teamCount = 8;
+		epn.sendToEngine("TL"); //Write game mode
+		if(mission != null) epn.sendToEngine(mission);
+		
+		//seed info
+		epn.sendToEngine(String.format("eseed {%s}", UUID.randomUUID().toString()));
+		
+		map.sendToEngine(epn);
+		//dimensions of the map
+		//templatefilter_command
+		//mapgen_command
+		//mazesize_command
+		
+		//epn.sendToEngine(String.format("etheme %s", theme));
+		
+		//scheme.sendToEngine(epn);
+		
+		//weapon.sendToEngine(os, teamCount);
+		
+		for(Team t : teams){
+			//t.sendToEngine(os, teamCount, 50, 0);
+		}
+		
+		
+	}
+	
+	public int describeContents() {
+		return 0;
+	}
+
+	public void writeToParcel(Parcel dest, int flags) {
+		dest.writeString(mode.name());
+		dest.writeParcelable(map, flags);
+		dest.writeString(theme);
+		dest.writeParcelable(scheme, flags);
+		dest.writeParcelable(weapon, flags);
+		dest.writeString(mission);
+		dest.writeString(seed);
+		dest.writeParcelableArray((Team[])teams.toArray(new Team[1]), 0);
+	}
+	
+	private void readFromParcel(Parcel src){
+		mode = GameMode.valueOf(src.readString());
+		map = src.readParcelable(Map.class.getClassLoader());
+		theme = src.readString();
+		scheme = src.readParcelable(Scheme.class.getClassLoader());
+		weapon = src.readParcelable(Weapon.class.getClassLoader());
+		mission = src.readString();
+		seed = src.readString();
+		Parcelable[] parcelables = src.readParcelableArray(Team[].class.getClassLoader());
+		for(Parcelable team : parcelables){
+			teams.add((Team)team);
+		}
+		
+	}
+	
+	public static final Parcelable.Creator<GameConfig> CREATOR = new Parcelable.Creator<GameConfig>() {
+		public GameConfig createFromParcel(Parcel source) {
+			return new GameConfig(source);
+		}
+		public GameConfig[] newArray(int size) {
+			return new GameConfig[size];
+		}
+	};
+	
+}