Main class files for the start-local-game screen hedgeroid
authorXeli
Thu, 14 Jul 2011 15:55:28 +0200
branchhedgeroid
changeset 5433 8f82045953c1
parent 5431 356b4fbd4c48
child 5435 d060bfb6f670
Main class files for the start-local-game screen
project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocolNetwork.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/FrontendDataUtil.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Scheme.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/StartGameActivity.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Weapon.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocolNetwork.java	Thu Jul 14 15:55:28 2011 +0200
@@ -0,0 +1,149 @@
+package org.hedgewars.mobile;
+
+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 int mode = -1;
+	private ServerSocket serverSocket;
+	private InputStream input;
+	private OutputStream output;
+	public int port;
+
+	public EngineProtocolNetwork(int _mode){
+		try {
+			mode = _mode;
+			
+			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 EngineProtocolNetwork(String uuid){
+		
+	}
+	
+	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];
+				Log.e("bla", "bla" + msgSize + " + " + buffer[0] + " + " + buffer[1]);
+
+				input.read(buffer, 0, msgSize);
+
+				switch(buffer[0]){
+				case 'C'://game init
+					Log.e("bla", "send init");
+					
+					sendToEngine(GAMEMODE_LOCAL);//Start localgame
+					
+					//seed info
+					
+					
+					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();
+		}
+	}
+
+	private 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/FrontendDataUtil.java	Thu Jul 14 15:55:28 2011 +0200
@@ -0,0 +1,64 @@
+package org.hedgewars.mobile;
+
+import java.io.File;
+import java.util.Arrays;
+
+import android.content.Context;
+
+public class FrontendDataUtil {
+
+	private static final String MISSION_PREFIX = "Mission: ";
+	
+	public static String[] getMaps(Context c){
+		File[] files = Utils.getFilesFromRelativeDir(c,"Maps");
+		String[] maps = new String[files.length];
+		String[] missions = new String[maps.length];
+		int mapsCounter = 0, missionsCounter = 0;
+		
+		for(File f : files){
+			if(Utils.hasFileWithSuffix(f, ".lua")){
+				missions[missionsCounter++] = MISSION_PREFIX + f.getName();
+			}else{
+				maps[mapsCounter++] = f.getName();
+			}
+		}
+		String[] ret = new String[maps.length];
+		System.arraycopy(missions, 0, ret, 0, missionsCounter);
+		System.arraycopy(maps, 0, ret, missionsCounter, mapsCounter);
+		Arrays.sort(ret, 0, missionsCounter);
+		Arrays.sort(ret, missionsCounter, ret.length);
+		return ret;
+	}
+	
+	public static String[] getGameplay(Context c){
+		String[] files = Utils.getFileNamesFromRelativeDir(c, "Scripts/Multiplayer");
+		int retCounter = 0;
+		
+		for(int i = 0; i < files.length; i++){
+			if(files[i].endsWith(".lua")){
+				files[i] = files[i].replace('_', ' ').substring(0, files[i].length()-4); //replace _ by a space and removed the last four characters (.lua)
+				retCounter++;
+			}else files[i] = null;
+		}
+		String[] ret = new String[retCounter];
+		retCounter = 0;
+		for(String s : files){
+			if(s != null) ret[retCounter++] = s;
+		}
+		Arrays.sort(ret);
+		
+		return ret;	
+	}
+	
+	public static String[] getThemes(Context c){
+		return Utils.getDirsWithFileSuffix(c, "Themes", "icon.png");
+	}
+	
+	public static Scheme[] getSchemes(Context c){
+		return Scheme.getSchemes(c);
+	}
+	
+	public static Weapon[] getWeapons(Context c){
+		return Weapon.getWeapons(c);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Scheme.java	Thu Jul 14 15:55:28 2011 +0200
@@ -0,0 +1,144 @@
+package org.hedgewars.mobile;
+
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+import org.xmlpull.v1.XmlPullParserFactory;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+
+public class Scheme {
+
+	public static final String DIRECTORY_SCHEME = "schemes";
+
+	
+	private String name;
+	private ArrayList<Integer> basic;
+	private ArrayList<Boolean> gamemod;
+
+	public Scheme(String _name, ArrayList<Integer> _basic, ArrayList<Boolean> _gamemod){
+		name = _name;
+		basic = _basic;
+		gamemod = _gamemod;
+	}
+
+	public String toString(){
+		return name;
+	}
+	
+	
+	public static final int STATE_START = 0;
+	public static final int STATE_ROOT = 1;
+	public static final int STATE_NAME = 2;
+	public static final int STATE_BASICFLAGS = 3;
+	public static final int STATE_GAMEMOD = 4;
+	public static final int STATE_BASICFLAG_INTEGER = 5;
+	public static final int STATE_GAMEMOD_TRUE = 6;
+	public static final int STATE_GAMEMOD_FALSE = 7;
+	
+	public static Scheme[] getSchemes(Context c) throws IllegalArgumentException{
+		String dir = c.getFilesDir().getAbsolutePath() + '/' + DIRECTORY_SCHEME + '/';
+		String[] files = new File(dir).list();
+		if(files == null) files = new String[]{};
+		Arrays.sort(files);
+		
+		ArrayList<Scheme> schemes = new ArrayList<Scheme>();
+
+		try {
+			XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance();
+			XmlPullParser xmlPuller = xmlPullFactory.newPullParser();
+			
+			for(String file : files){
+				BufferedReader br = new BufferedReader(new FileReader(dir + file), 1024);
+				xmlPuller.setInput(br);
+				String name = null;
+				ArrayList<Integer> basic = new ArrayList<Integer>();
+				ArrayList<Boolean> gamemod = new ArrayList<Boolean>();
+				int eventType = xmlPuller.getEventType();
+				int state = STATE_START;
+				while(eventType != XmlPullParser.END_DOCUMENT){
+					switch(state){
+					case STATE_START:
+						if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().equals("scheme")) state = STATE_ROOT;
+						else if(eventType != XmlPullParser.START_DOCUMENT) throwException(file, eventType);
+						break;
+					case STATE_ROOT:
+						if(eventType == XmlPullParser.START_TAG){
+							if(xmlPuller.getName().equals("basicflags")) state = STATE_BASICFLAGS;
+							else if(xmlPuller.getName().toLowerCase().equals("gamemod")) state = STATE_GAMEMOD;
+							else if(xmlPuller.getName().toLowerCase().equals("name")) state = STATE_NAME;
+							else throwException(file, eventType);
+						}else if(eventType == XmlPullParser.END_TAG) state = STATE_START;
+						else throwException(xmlPuller.getText(), eventType);
+						break;
+					case STATE_BASICFLAGS:
+						if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().toLowerCase().equals("integer")) state = STATE_BASICFLAG_INTEGER;
+						else if(eventType == XmlPullParser.END_TAG)	state = STATE_ROOT;
+						else throwException(file, eventType);
+						break;
+					case STATE_GAMEMOD:
+						if(eventType == XmlPullParser.START_TAG){
+							if(xmlPuller.getName().toLowerCase().equals("true")) state = STATE_GAMEMOD_TRUE;
+							else if(xmlPuller.getName().toLowerCase().equals("false")) state = STATE_GAMEMOD_FALSE;
+							else throwException(file, eventType);
+						}else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
+						else throwException(file, eventType);
+						break;
+					case STATE_NAME:
+						if(eventType == XmlPullParser.TEXT) name = xmlPuller.getText().trim();
+						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
+						else throwException(file, eventType);
+						break;
+					case STATE_BASICFLAG_INTEGER:
+						if(eventType == XmlPullParser.TEXT) basic.add(Integer.parseInt(xmlPuller.getText().trim()));
+						else if(eventType == XmlPullParser.END_TAG) state = STATE_BASICFLAGS;
+						else throwException(file, eventType);
+						break;
+					case STATE_GAMEMOD_FALSE:
+						if(eventType == XmlPullParser.TEXT) gamemod.add(false);
+						else if(eventType == XmlPullParser.END_TAG) state = STATE_GAMEMOD;
+						else throwException(file, eventType);
+						break;
+					case STATE_GAMEMOD_TRUE:
+						if(eventType == XmlPullParser.TEXT) gamemod.add(true);
+						else if(eventType == XmlPullParser.END_TAG) state = STATE_GAMEMOD;
+						else throwException(file, eventType);
+						break;
+					}
+					eventType = xmlPuller.next();
+					while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){//Skip whitespaces
+						eventType = xmlPuller.next();
+					}
+				}//end while(eventtype != END_DOCUMENT
+				schemes.add(new Scheme(name, basic, gamemod));
+			}//end for(string file : files
+			Scheme[] ret = new Scheme[schemes.size()];
+			schemes.toArray(ret);
+			return ret;
+		} catch (XmlPullParserException e) {
+			e.printStackTrace();
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return new Scheme[]{};//TODO handle correctly
+	}
+	
+	private static void throwException(String file, int eventType){
+		throw new IllegalArgumentException(String.format("Xml file: %s malformed with eventType: %d.", file, eventType));
+	}
+
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/StartGameActivity.java	Thu Jul 14 15:55:28 2011 +0200
@@ -0,0 +1,77 @@
+package org.hedgewars.mobile;
+
+import android.app.Activity;
+import android.content.SharedPreferences;
+import android.graphics.drawable.Drawable;
+import android.os.Bundle;
+import android.preference.PreferenceManager;
+import android.view.View;
+import android.widget.AdapterView;
+import android.widget.AdapterView.OnItemSelectedListener;
+import android.widget.ArrayAdapter;
+import android.widget.ImageView;
+import android.widget.Spinner;
+
+public class StartGameActivity extends Activity {
+
+	private Spinner maps, gameplay, gamescheme, weapons, themes;
+	private ImageView themeIcon;
+	
+	public void onCreate(Bundle savedInstanceState){
+		super.onCreate(savedInstanceState);
+		
+		setContentView(R.layout.starting_game);
+
+		maps = (Spinner) findViewById(R.id.spinMaps);
+		gameplay = (Spinner) findViewById(R.id.spinGameplay);
+		gamescheme = (Spinner) findViewById(R.id.spinGamescheme);
+		weapons = (Spinner) findViewById(R.id.spinweapons);
+		themes = (Spinner) findViewById(R.id.spinTheme);
+		
+		themeIcon = (ImageView) findViewById(R.id.imgTheme);
+		
+		ArrayAdapter<?> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, FrontendDataUtil.getMaps(this));
+		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+		maps.setAdapter(adapter);
+		
+		adapter = new ArrayAdapter<String>(this, R.layout.listview_item, FrontendDataUtil.getGameplay(this));
+		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+		gameplay.setAdapter(adapter);
+
+		adapter = new ArrayAdapter<Scheme>(this, R.layout.listview_item, FrontendDataUtil.getSchemes(this));
+		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+		gamescheme.setAdapter(adapter);
+
+		adapter = new ArrayAdapter<Weapon>(this, R.layout.listview_item, FrontendDataUtil.getWeapons(this));
+		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+		weapons.setAdapter(adapter);
+		
+		adapter = new ArrayAdapter<String>(this, R.layout.listview_item, FrontendDataUtil.getThemes(this));
+		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+		themes.setAdapter(adapter);
+		
+		
+		themes.setOnItemSelectedListener(themesClicker);
+		
+		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
+		
+		Utils.resRawToFilesDir(this,R.array.schemes, Scheme.DIRECTORY_SCHEME);
+		Utils.resRawToFilesDir(this, R.array.weapons, Weapon.DIRECTORY_WEAPON);
+	}
+	
+	private OnItemSelectedListener themesClicker = new OnItemSelectedListener(){
+
+		public void onItemSelected(AdapterView<?> arg0, View view, int position, long rowId) {
+			String themeName = (String) arg0.getAdapter().getItem(position);
+			Drawable themeIconDrawable = Drawable.createFromPath(Utils.getDownloadPath(StartGameActivity.this) + "/Data/Themes/" + themeName + "/icon@2X.png");
+			themeIcon.setImageDrawable(themeIconDrawable);
+		}
+
+		public void onNothingSelected(AdapterView<?> arg0) {
+			// TODO Auto-generated method stub
+			
+		}
+		
+	};
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Weapon.java	Thu Jul 14 15:55:28 2011 +0200
@@ -0,0 +1,138 @@
+package org.hedgewars.mobile;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+import org.xmlpull.v1.XmlPullParserFactory;
+
+import android.content.Context;
+
+public class Weapon {
+
+	public static final String DIRECTORY_WEAPON = "weapons";
+	
+	private String name;
+	private String QT;
+	private String prob;
+	private String delay;
+	private String crate;
+	
+	public Weapon(String _name, String _QT, String _prob, String _delay, String _crate){
+		name = _name;
+		QT = _QT;
+		prob = _prob;
+		delay = _delay;
+		crate = _crate;
+	}
+	
+	public String toString(){
+		return name;
+	}
+	
+	public static final int STATE_START = 0;
+	public static final int STATE_ROOT = 1;
+	public static final int STATE_NAME = 2;
+	public static final int STATE_QT = 3;
+	public static final int STATE_PROBABILITY = 4;
+	public static final int STATE_DELAY = 5;
+	public static final int STATE_CRATE = 6;
+	
+	public static Weapon[] getWeapons(Context c) throws IllegalArgumentException{
+		String dir = c.getFilesDir().getAbsolutePath() + '/' + DIRECTORY_WEAPON + '/';
+		String[] files = new File(dir).list();
+		if(files == null) files = new String[]{};
+		Arrays.sort(files);
+		
+		ArrayList<Weapon> weapons = new ArrayList<Weapon>();
+
+		try {
+			XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance();
+			XmlPullParser xmlPuller = xmlPullFactory.newPullParser();
+			
+			for(String file : files){
+				BufferedReader br = new BufferedReader(new FileReader(dir + file), 1024);
+				xmlPuller.setInput(br);
+				String name = null;
+				String qt = null;
+				String prob = null;
+				String delay = null;
+				String crate = null;
+				
+				int eventType = xmlPuller.getEventType();
+				int state = STATE_START;
+				while(eventType != XmlPullParser.END_DOCUMENT){
+					switch(state){
+					case STATE_START:
+						if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().equals("weapon")) state = STATE_ROOT;
+						else if(eventType != XmlPullParser.START_DOCUMENT) throwException(file, eventType);
+						break;
+					case STATE_ROOT:
+						if(eventType == XmlPullParser.START_TAG){
+							if(xmlPuller.getName().toLowerCase().equals("qt")) state = STATE_QT;
+							else if(xmlPuller.getName().toLowerCase().equals("name")) state = STATE_NAME;
+							else if(xmlPuller.getName().toLowerCase().equals("probability")) state = STATE_PROBABILITY;
+							else if(xmlPuller.getName().toLowerCase().equals("delay")) state = STATE_DELAY;
+							else if(xmlPuller.getName().toLowerCase().equals("crate")) state = STATE_CRATE;
+							else throwException(file, eventType);
+						}else if(eventType == XmlPullParser.END_TAG) state = STATE_START;
+						else throwException(xmlPuller.getText(), eventType);
+						break;
+					case STATE_NAME:
+						if(eventType == XmlPullParser.TEXT) name = xmlPuller.getText().trim();
+						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
+						else throwException(file, eventType);
+						break;
+					case STATE_QT:
+						if(eventType == XmlPullParser.TEXT) qt = xmlPuller.getText().trim();
+						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
+						else throwException(file, eventType);
+						break;
+					case STATE_PROBABILITY:
+						if(eventType == XmlPullParser.TEXT) prob = xmlPuller.getText().trim();
+						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
+						else throwException(file, eventType);
+						break;
+					case STATE_DELAY:
+						if(eventType == XmlPullParser.TEXT) delay = xmlPuller.getText().trim();
+						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
+						else throwException(file, eventType);
+						break;
+					case STATE_CRATE:
+						if(eventType == XmlPullParser.TEXT) crate = xmlPuller.getText().trim();
+						else if(eventType == XmlPullParser.END_TAG) state = STATE_ROOT;
+						else throwException(file, eventType);
+						break;
+					}
+					eventType = xmlPuller.next();
+					while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){//Skip whitespaces
+						eventType = xmlPuller.next();
+					}
+				}//end while(eventtype != END_DOCUMENT
+				weapons.add(new Weapon(name, qt, prob, delay, crate));
+			}//end for(string file : files
+			Weapon[] ret = new Weapon[weapons.size()];
+			weapons.toArray(ret);
+			return ret;
+			
+		} catch (XmlPullParserException e) {
+			e.printStackTrace();
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		return new Weapon[]{};//TODO handle correctly
+	}
+	
+	private static void throwException(String file, int eventType){
+		throw new IllegalArgumentException(String.format("Xml file: %s malformed with eventType: %d.", file, eventType));
+	}
+
+}