project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/Team.java
author Xeli
Thu, 04 Aug 2011 17:27:05 +0200
branchhedgeroid
changeset 5463 83c53a80f7ff
child 5506 2b0c4fcde4c6
permissions -rw-r--r--
datastructures for the different aspects of a gameconfiguration
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
     1
package org.hedgewars.mobile.EngineProtocol;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
     2
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
     3
import java.io.BufferedReader;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
     4
import java.io.FileNotFoundException;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
     5
import java.io.FileReader;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
     6
import java.io.IOException;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
     7
import java.io.OutputStream;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
     8
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
     9
import org.xmlpull.v1.XmlPullParser;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    10
import org.xmlpull.v1.XmlPullParserException;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    11
import org.xmlpull.v1.XmlPullParserFactory;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    12
import org.xmlpull.v1.XmlSerializer;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    13
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    14
import android.os.Parcel;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    15
import android.os.Parcelable;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    16
import android.util.Xml;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    17
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    18
public class Team implements Parcelable{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    19
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    20
	public static final String DIRECTORY_TEAMS = "teams";
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    21
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    22
	public String name, grave, flag, voice, fort, hash;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    23
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    24
	public static int maxNumberOfHogs = 0;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    25
	public static int maxNumberOfTeams = 0;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    26
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    27
	static{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    28
		maxNumberOfHogs = PascalExports.HWgetMaxNumberOfHogs();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    29
		maxNumberOfTeams = PascalExports.HWgetMaxNumberOfTeams();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    30
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    31
	public String[] hats = new String[maxNumberOfHogs];
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    32
	public String[] hogNames = new String[maxNumberOfHogs];
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    33
	public int[] levels = new int[maxNumberOfHogs];
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    34
	
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    35
	public Team(){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    36
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    37
	
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    38
	public Team(Parcel in){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    39
		readFromParcel(in);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    40
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    41
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    42
	public boolean equals(Object o){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    43
		if(super.equals(o)) return true;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    44
		else if(o instanceof Team){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    45
			Team t = (Team)o;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    46
			boolean ret = name.equals(t.name);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    47
			ret &= grave.equals(t.grave);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    48
			ret &= flag.equals(t.flag);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    49
			ret &= voice.equals(t.voice);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    50
			ret &= fort.equals(t.fort);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    51
			ret &= hash.equals(t.hash);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    52
			return ret;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    53
		}else{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    54
			return false;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    55
		}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    56
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    57
	
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    58
	
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    59
	public void sendToEngine(OutputStream os, int hogCount, int health, int color) throws IOException{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    60
		os.write(String.format("eaddteam %s %d %s", hash, color, name).getBytes());
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    61
		os.write(String.format("egrave %s", grave).getBytes());
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    62
		os.write(String.format("efort %s", fort).getBytes());
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    63
		os.write(String.format("evoicepack %s", voice).getBytes());
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    64
		os.write(String.format("eflag %s", flag).getBytes());
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    65
		
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    66
		for(int i = 0; i < hogCount; i++){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    67
			os.write(String.format("eaddhh %d %d %s", levels[i], health, hogNames[i]).getBytes());
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    68
			os.write(String.format("ehat %s", hats[i]).getBytes());
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    69
		}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    70
		os.flush();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    71
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    72
	
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    73
	public static final int STATE_START = 0;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    74
	public static final int STATE_ROOT = 1;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    75
	public static final int STATE_HOG_ROOT = 2;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    76
	public static final int STATE_HOG_HAT = 3;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    77
	public static final int STATE_HOG_NAME = 4;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    78
	public static final int STATE_HOG_LEVEL = 5;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    79
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    80
	/**
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    81
	 * Read the xml file path and convert it to a Team object
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    82
	 * @param path absolute path to the xml file
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    83
	 * @return
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    84
	 */
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    85
	public static Team getTeamFromXml(String path){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    86
		try {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    87
			XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    88
			XmlPullParser xmlPuller = xmlPullFactory.newPullParser();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    89
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    90
			BufferedReader br = new BufferedReader(new FileReader(path), 1024);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    91
			xmlPuller.setInput(br);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    92
			Team team = new Team();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    93
			int hogCounter = 0;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    94
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    95
			int eventType = xmlPuller.getEventType();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    96
			int state = STATE_START;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    97
			while(eventType != XmlPullParser.END_DOCUMENT){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    98
				switch(state){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    99
				case STATE_START:
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   100
					if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().equals("team")) state = STATE_ROOT;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   101
					else if(eventType != XmlPullParser.START_DOCUMENT) throwException(path, eventType);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   102
					break;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   103
				case STATE_ROOT:
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   104
					if(eventType == XmlPullParser.START_TAG){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   105
						if(xmlPuller.getName().toLowerCase().equals("name")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   106
							team.name = getXmlText(xmlPuller, "name");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   107
						}else if(xmlPuller.getName().toLowerCase().equals("flag")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   108
							team.flag= getXmlText(xmlPuller, "flag");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   109
						}else if(xmlPuller.getName().toLowerCase().equals("voice")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   110
							team.voice = getXmlText(xmlPuller, "voice");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   111
						}else if(xmlPuller.getName().toLowerCase().equals("grave")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   112
							team.grave = getXmlText(xmlPuller, "grave");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   113
						}else if(xmlPuller.getName().toLowerCase().equals("fort")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   114
							team.fort = getXmlText(xmlPuller, "fort");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   115
						}else if(xmlPuller.getName().toLowerCase().equals("hash")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   116
							team.hash = getXmlText(xmlPuller, "hash");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   117
						}else if(xmlPuller.getName().toLowerCase().equals("hog")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   118
							state = STATE_HOG_ROOT;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   119
						}else throwException(xmlPuller.getName(), eventType);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   120
					}else if(eventType == XmlPullParser.END_TAG) state = STATE_START;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   121
					else throwException(xmlPuller.getText(), eventType);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   122
					break;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   123
				case STATE_HOG_ROOT:
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   124
					if(eventType == XmlPullParser.START_TAG){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   125
						if(xmlPuller.getName().toLowerCase().equals("name")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   126
							team.hogNames[hogCounter] = getXmlText(xmlPuller, "name");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   127
						}else if(xmlPuller.getName().toLowerCase().equals("hat")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   128
							team.hats[hogCounter] = getXmlText(xmlPuller, "hat");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   129
						}else if(xmlPuller.getName().toLowerCase().equals("level")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   130
							team.levels[hogCounter] = Integer.parseInt(getXmlText(xmlPuller, "level"));
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   131
						}else throwException(xmlPuller.getText(), eventType);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   132
					}else if(eventType == XmlPullParser.END_TAG){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   133
						hogCounter++;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   134
						state = STATE_ROOT;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   135
					}else throwException(xmlPuller.getText(), eventType);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   136
					break;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   137
				}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   138
				eventType = getEventType(xmlPuller);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   139
			}//end while(eventtype != END_DOCUMENT
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   140
			return team;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   141
		} catch (NumberFormatException e){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   142
			e.printStackTrace();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   143
		} catch (XmlPullParserException e) {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   144
			e.printStackTrace();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   145
		} catch (FileNotFoundException e) {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   146
			e.printStackTrace();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   147
		} catch (IOException e) {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   148
			e.printStackTrace();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   149
		}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   150
		return null;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   151
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   152
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   153
	private static String getXmlText(XmlPullParser xmlPuller, String parentTag)throws XmlPullParserException, IOException{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   154
		if(getEventType(xmlPuller) == XmlPullParser.TEXT){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   155
			String txt = xmlPuller.getText();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   156
			if(getEventType(xmlPuller) == XmlPullParser.END_TAG && xmlPuller.getName().toLowerCase().equals(parentTag)){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   157
				return txt;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   158
			}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   159
		}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   160
		throw new XmlPullParserException("malformed xml file on string read from tag: " + parentTag);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   161
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   162
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   163
	/**
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   164
	 * Skips whitespaces..
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   165
	 */
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   166
	private static int getEventType(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   167
		int eventType = xmlPuller.next();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   168
		while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   169
			eventType = xmlPuller.next();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   170
		}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   171
		return eventType;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   172
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   173
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   174
	private static void throwException(String file, int eventType){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   175
		throw new IllegalArgumentException(String.format("Xml file: %s malformed with error: %d.", file, eventType));
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   176
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   177
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   178
	public void writeToXml(OutputStream os){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   179
		XmlSerializer serializer = Xml.newSerializer();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   180
		try{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   181
			serializer.setOutput(os, "UTF-8");	
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   182
			serializer.startDocument("UTF-8", true);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   183
			serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   184
			
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   185
			serializer.startTag(null, "team");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   186
				serializer.startTag(null, "name");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   187
				serializer.text(name);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   188
				serializer.endTag(null, "name");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   189
				serializer.startTag(null, "flag");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   190
				serializer.text(flag);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   191
				serializer.endTag(null, "flag");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   192
				serializer.startTag(null, "fort");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   193
				serializer.text(fort);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   194
				serializer.endTag(null, "fort");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   195
				serializer.startTag(null, "grave");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   196
				serializer.text(grave);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   197
				serializer.endTag(null, "grave");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   198
				serializer.startTag(null, "voice");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   199
				serializer.text(voice);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   200
				serializer.endTag(null, "voice");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   201
				serializer.startTag(null, "hash");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   202
				serializer.text(hash);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   203
				serializer.endTag(null, "hash");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   204
				
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   205
				for(int i = 0; i < maxNumberOfHogs; i++){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   206
					serializer.startTag(null, "hog");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   207
					serializer.startTag(null, "name");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   208
					serializer.text(hogNames[i]);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   209
					serializer.endTag(null, "name");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   210
					serializer.startTag(null, "hat");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   211
					serializer.text(hats[i]);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   212
					serializer.endTag(null, "hat");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   213
					serializer.startTag(null, "level");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   214
					serializer.text(String.valueOf(levels[i]));
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   215
					serializer.endTag(null, "level");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   216
					
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   217
					serializer.endTag(null, "hog");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   218
				}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   219
			serializer.endTag(null, "team");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   220
			serializer.endDocument();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   221
			serializer.flush();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   222
			
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   223
		} catch (IOException e) {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   224
			e.printStackTrace();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   225
		}finally{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   226
			try {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   227
				os.close();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   228
			} catch (IOException e) {}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   229
		}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   230
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   231
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   232
	public int describeContents() {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   233
		return 0;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   234
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   235
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   236
	public void writeToParcel(Parcel dest, int flags) {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   237
		dest.writeString(name);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   238
		dest.writeString(grave);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   239
		dest.writeString(flag);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   240
		dest.writeString(voice);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   241
		dest.writeString(fort);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   242
		dest.writeString(hash);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   243
		dest.writeStringArray(hats);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   244
		dest.writeStringArray(hogNames);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   245
		dest.writeIntArray(levels);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   246
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   247
		
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   248
	
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   249
	public void readFromParcel(Parcel src){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   250
		name = src.readString();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   251
		grave = src.readString();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   252
		flag = src.readString();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   253
		voice = src.readString();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   254
		fort = src.readString();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   255
		hash = src.readString();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   256
		src.readStringArray(hats);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   257
		src.readStringArray(hogNames);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   258
		src.readIntArray(levels);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   259
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   260
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   261
	public static final Parcelable.Creator<Team> CREATOR = new Parcelable.Creator<Team>() {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   262
		public Team createFromParcel(Parcel source) {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   263
			return new Team(source);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   264
		}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   265
		public Team[] newArray(int size) {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   266
			return new Team[size];
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   267
		}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   268
		
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   269
	};
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   270
	
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   271
}