project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/Team.java
author Xeli
Wed, 10 Aug 2011 01:22:52 +0200
branchhedgeroid
changeset 5532 3d7ac2b3b703
parent 5506 2b0c4fcde4c6
child 5621 ea796c83ea47
permissions -rw-r--r--
Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
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;
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
     8
import java.util.ArrayList;
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
     9
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    10
import org.xmlpull.v1.XmlPullParser;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    11
import org.xmlpull.v1.XmlPullParserException;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    12
import org.xmlpull.v1.XmlPullParserFactory;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    13
import org.xmlpull.v1.XmlSerializer;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    14
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    15
import android.os.Parcel;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    16
import android.os.Parcelable;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    17
import android.util.Xml;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    18
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    19
public class Team implements Parcelable{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    20
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    21
	public static final String DIRECTORY_TEAMS = "teams";
5532
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    22
	private static final Integer[] TEAM_COLORS = {
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    23
		0xd12b42, /* red    */ 
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    24
		0x4980c1, /* blue   */ 
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    25
		0x6ab530, /* green  */ 
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    26
		0xbc64c4, /* purple */ 
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    27
		0xe76d14, /* orange */ 
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    28
		0x3fb6e6, /* cyan   */ 
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    29
		0xe3e90c, /* yellow */ 
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    30
		0x61d4ac, /* mint   */ 
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    31
		0xf1c3e1, /* pink   */ 
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    32
		/* add new colors here */
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    33
	};
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    34
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    35
//	private static final Integer[] TEAM_COLORS = {
5532
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    36
//		0xff0000, /* red    */ 
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    37
//		0x00ff00, /* blue   */ 
3d7ac2b3b703 Fixed team colors in the UI, delete and a nullpointer bug when no teams are created
Xeli
parents: 5506
diff changeset
    38
//		0x0000ff, /* green  */ 
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    39
//	};
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    40
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    41
	private static final int STATE_START = 0;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    42
	private static final int STATE_ROOT = 1;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    43
	private static final int STATE_HOG_ROOT = 2;
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    44
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    45
	public String name, grave, flag, voice, fort, hash;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    46
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    47
	public static int maxNumberOfHogs = 0;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    48
	public static int maxNumberOfTeams = 0;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    49
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    50
	static{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    51
		maxNumberOfHogs = PascalExports.HWgetMaxNumberOfHogs();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    52
		maxNumberOfTeams = PascalExports.HWgetMaxNumberOfTeams();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    53
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    54
	public String[] hats = new String[maxNumberOfHogs];
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    55
	public String[] hogNames = new String[maxNumberOfHogs];
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    56
	public int[] levels = new int[maxNumberOfHogs];
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    57
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    58
	public int hogCount = 4;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    59
	public int color = TEAM_COLORS[0];
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    60
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    61
	public Team(){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    62
	}
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    63
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    64
	public Team(Parcel in){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    65
		readFromParcel(in);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    66
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    67
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    68
	public boolean equals(Object o){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    69
		if(super.equals(o)) return true;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    70
		else if(o instanceof Team){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    71
			Team t = (Team)o;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    72
			boolean ret = name.equals(t.name);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    73
			ret &= grave.equals(t.grave);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    74
			ret &= flag.equals(t.flag);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    75
			ret &= voice.equals(t.voice);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    76
			ret &= fort.equals(t.fort);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    77
			ret &= hash.equals(t.hash);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    78
			return ret;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    79
		}else{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    80
			return false;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    81
		}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
    82
	}
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    83
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    84
	public void setRandomColor(int[] illegalcolors){
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    85
		Integer[] colorsToPickFrom = TEAM_COLORS;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    86
		if(illegalcolors != null){
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    87
			ArrayList<Integer> colors = new ArrayList<Integer>();
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    88
			for(int color : TEAM_COLORS){
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    89
				boolean validColor = true;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    90
				for(int illegal : illegalcolors){
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    91
					if(color == illegal) validColor = false;
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    92
				}
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    93
				if(validColor) colors.add(color);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    94
			}
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    95
			if(colors.size() != 0) colorsToPickFrom = colors.toArray(new Integer[1]);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    96
		}
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    97
		int index = (int)Math.round(Math.random()*(colorsToPickFrom.length-1));
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    98
		color = colorsToPickFrom[index];
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
    99
	}
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   100
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   101
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   102
	public void sendToEngine(EngineProtocolNetwork epn, int hogCount, int health) throws IOException{
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   103
		epn.sendToEngine(String.format("eaddteam %s %d %s", hash, color, name));
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   104
		epn.sendToEngine(String.format("egrave %s", grave));
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   105
		epn.sendToEngine(String.format("efort %s", fort));
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   106
		epn.sendToEngine(String.format("evoicepack %s", voice));
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   107
		epn.sendToEngine(String.format("eflag %s", flag));
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   108
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   109
		for(int i = 0; i < hogCount; i++){
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   110
			epn.sendToEngine(String.format("eaddhh %d %d %s", levels[i], health, hogNames[i]));
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   111
			epn.sendToEngine(String.format("ehat %s", hats[i]));
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   112
		}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   113
	}
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   114
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   115
	/*
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   116
	 * XML METHODS
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   117
	 */
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   118
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   119
	/**
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   120
	 * 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
   121
	 * @param path absolute path to the xml file
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   122
	 * @return
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   123
	 */
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   124
	public static Team getTeamFromXml(String path){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   125
		try {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   126
			XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   127
			XmlPullParser xmlPuller = xmlPullFactory.newPullParser();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   128
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   129
			BufferedReader br = new BufferedReader(new FileReader(path), 1024);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   130
			xmlPuller.setInput(br);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   131
			Team team = new Team();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   132
			int hogCounter = 0;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   133
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   134
			int eventType = xmlPuller.getEventType();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   135
			int state = STATE_START;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   136
			while(eventType != XmlPullParser.END_DOCUMENT){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   137
				switch(state){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   138
				case STATE_START:
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   139
					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
   140
					else if(eventType != XmlPullParser.START_DOCUMENT) throwException(path, eventType);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   141
					break;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   142
				case STATE_ROOT:
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   143
					if(eventType == XmlPullParser.START_TAG){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   144
						if(xmlPuller.getName().toLowerCase().equals("name")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   145
							team.name = getXmlText(xmlPuller, "name");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   146
						}else if(xmlPuller.getName().toLowerCase().equals("flag")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   147
							team.flag= getXmlText(xmlPuller, "flag");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   148
						}else if(xmlPuller.getName().toLowerCase().equals("voice")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   149
							team.voice = getXmlText(xmlPuller, "voice");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   150
						}else if(xmlPuller.getName().toLowerCase().equals("grave")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   151
							team.grave = getXmlText(xmlPuller, "grave");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   152
						}else if(xmlPuller.getName().toLowerCase().equals("fort")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   153
							team.fort = getXmlText(xmlPuller, "fort");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   154
						}else if(xmlPuller.getName().toLowerCase().equals("hash")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   155
							team.hash = getXmlText(xmlPuller, "hash");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   156
						}else if(xmlPuller.getName().toLowerCase().equals("hog")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   157
							state = STATE_HOG_ROOT;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   158
						}else throwException(xmlPuller.getName(), eventType);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   159
					}else if(eventType == XmlPullParser.END_TAG) state = STATE_START;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   160
					else throwException(xmlPuller.getText(), eventType);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   161
					break;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   162
				case STATE_HOG_ROOT:
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   163
					if(eventType == XmlPullParser.START_TAG){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   164
						if(xmlPuller.getName().toLowerCase().equals("name")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   165
							team.hogNames[hogCounter] = getXmlText(xmlPuller, "name");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   166
						}else if(xmlPuller.getName().toLowerCase().equals("hat")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   167
							team.hats[hogCounter] = getXmlText(xmlPuller, "hat");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   168
						}else if(xmlPuller.getName().toLowerCase().equals("level")){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   169
							team.levels[hogCounter] = Integer.parseInt(getXmlText(xmlPuller, "level"));
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   170
						}else throwException(xmlPuller.getText(), eventType);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   171
					}else if(eventType == XmlPullParser.END_TAG){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   172
						hogCounter++;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   173
						state = STATE_ROOT;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   174
					}else throwException(xmlPuller.getText(), eventType);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   175
					break;
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
				eventType = getEventType(xmlPuller);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   178
			}//end while(eventtype != END_DOCUMENT
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   179
			return team;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   180
		} catch (NumberFormatException e){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   181
			e.printStackTrace();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   182
		} catch (XmlPullParserException e) {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   183
			e.printStackTrace();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   184
		} catch (FileNotFoundException e) {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   185
			e.printStackTrace();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   186
		} catch (IOException e) {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   187
			e.printStackTrace();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   188
		}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   189
		return null;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   190
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   191
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   192
	private static String getXmlText(XmlPullParser xmlPuller, String parentTag)throws XmlPullParserException, IOException{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   193
		if(getEventType(xmlPuller) == XmlPullParser.TEXT){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   194
			String txt = xmlPuller.getText();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   195
			if(getEventType(xmlPuller) == XmlPullParser.END_TAG && xmlPuller.getName().toLowerCase().equals(parentTag)){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   196
				return txt;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   197
			}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   198
		}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   199
		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
   200
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   201
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   202
	/**
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   203
	 * Skips whitespaces..
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
	private static int getEventType(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   206
		int eventType = xmlPuller.next();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   207
		while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   208
			eventType = xmlPuller.next();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   209
		}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   210
		return eventType;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   211
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   212
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   213
	private static void throwException(String file, int eventType){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   214
		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
   215
	}
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
	public void writeToXml(OutputStream os){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   218
		XmlSerializer serializer = Xml.newSerializer();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   219
		try{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   220
			serializer.setOutput(os, "UTF-8");	
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   221
			serializer.startDocument("UTF-8", true);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   222
			serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   223
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   224
			serializer.startTag(null, "team");
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   225
			serializer.startTag(null, "name");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   226
			serializer.text(name);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   227
			serializer.endTag(null, "name");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   228
			serializer.startTag(null, "flag");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   229
			serializer.text(flag);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   230
			serializer.endTag(null, "flag");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   231
			serializer.startTag(null, "fort");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   232
			serializer.text(fort);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   233
			serializer.endTag(null, "fort");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   234
			serializer.startTag(null, "grave");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   235
			serializer.text(grave);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   236
			serializer.endTag(null, "grave");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   237
			serializer.startTag(null, "voice");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   238
			serializer.text(voice);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   239
			serializer.endTag(null, "voice");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   240
			serializer.startTag(null, "hash");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   241
			serializer.text(hash);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   242
			serializer.endTag(null, "hash");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   243
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   244
			for(int i = 0; i < maxNumberOfHogs; i++){
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   245
				serializer.startTag(null, "hog");
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   246
				serializer.startTag(null, "name");
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   247
				serializer.text(hogNames[i]);
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   248
				serializer.endTag(null, "name");
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   249
				serializer.startTag(null, "hat");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   250
				serializer.text(hats[i]);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   251
				serializer.endTag(null, "hat");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   252
				serializer.startTag(null, "level");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   253
				serializer.text(String.valueOf(levels[i]));
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   254
				serializer.endTag(null, "level");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   255
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   256
				serializer.endTag(null, "hog");
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   257
			}
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   258
			serializer.endTag(null, "team");
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   259
			serializer.endDocument();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   260
			serializer.flush();
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   261
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   262
		} catch (IOException e) {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   263
			e.printStackTrace();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   264
		}finally{
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   265
			try {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   266
				os.close();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   267
			} catch (IOException e) {}
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
	}
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   270
	/*
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   271
	 * END XML METHODS
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   272
	 */
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   273
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   274
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   275
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   276
	/*
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   277
	 * PARCABLE METHODS
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   278
	 */
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   279
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   280
	public int describeContents() {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   281
		return 0;
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   282
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   283
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   284
	public void writeToParcel(Parcel dest, int flags) {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   285
		dest.writeString(name);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   286
		dest.writeString(grave);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   287
		dest.writeString(flag);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   288
		dest.writeString(voice);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   289
		dest.writeString(fort);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   290
		dest.writeString(hash);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   291
		dest.writeStringArray(hats);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   292
		dest.writeStringArray(hogNames);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   293
		dest.writeIntArray(levels);
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   294
		dest.writeInt(color);
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   295
		dest.writeInt(hogCount);
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   296
	}
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   297
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   298
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   299
	public void readFromParcel(Parcel src){
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   300
		name = src.readString();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   301
		grave = src.readString();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   302
		flag = src.readString();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   303
		voice = src.readString();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   304
		fort = src.readString();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   305
		hash = src.readString();
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   306
		src.readStringArray(hats);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   307
		src.readStringArray(hogNames);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   308
		src.readIntArray(levels);
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   309
		color = src.readInt();
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   310
		hogCount = src.readInt();
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   311
	}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   312
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   313
	public static final Parcelable.Creator<Team> CREATOR = new Parcelable.Creator<Team>() {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   314
		public Team createFromParcel(Parcel source) {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   315
			return new Team(source);
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   316
		}
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   317
		public Team[] newArray(int size) {
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   318
			return new Team[size];
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   319
		}
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   320
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   321
	};
5506
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   322
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   323
	/*
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   324
	 * END PARCABLE METHODS
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   325
	 */
2b0c4fcde4c6 Added color and team hog count
Xeli
parents: 5463
diff changeset
   326
5463
83c53a80f7ff datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff changeset
   327
}