author | Xeli |
Tue, 09 Aug 2011 20:56:18 +0200 | |
branch | hedgeroid |
changeset 5512 | e4cbfa6c1a6d |
parent 5506 | 2b0c4fcde4c6 |
child 5532 | 3d7ac2b3b703 |
permissions | -rw-r--r-- |
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 | 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"; |
5506 | 22 |
// private static final Integer[] TEAM_COLORS = { |
23 |
// 0xffd12b42, /* red */ |
|
24 |
// 0xff4980c1, /* blue */ |
|
25 |
// 0xff6ab530, /* green */ |
|
26 |
// 0xffbc64c4, /* purple */ |
|
27 |
// 0xffe76d14, /* orange */ |
|
28 |
// 0xff3fb6e6, /* cyan */ |
|
29 |
// 0xffe3e90c, /* yellow */ |
|
30 |
// 0xff61d4ac, /* mint */ |
|
31 |
// 0xfff1c3e1, /* pink */ |
|
32 |
// /* add new colors here */ |
|
33 |
// }; |
|
34 |
||
35 |
private static final Integer[] TEAM_COLORS = { |
|
36 |
0xff0000, /* red */ |
|
37 |
0x00ff00, /* blue */ |
|
38 |
0x0000ff, /* green */ |
|
39 |
}; |
|
40 |
||
41 |
private static final int STATE_START = 0; |
|
42 |
private static final int STATE_ROOT = 1; |
|
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 | 57 |
|
58 |
public int hogCount = 4; |
|
59 |
public int color = TEAM_COLORS[0]; |
|
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 | 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 | 83 |
|
84 |
public void setRandomColor(int[] illegalcolors){ |
|
85 |
Integer[] colorsToPickFrom = TEAM_COLORS; |
|
86 |
if(illegalcolors != null){ |
|
87 |
ArrayList<Integer> colors = new ArrayList<Integer>(); |
|
88 |
for(int color : TEAM_COLORS){ |
|
89 |
boolean validColor = true; |
|
90 |
for(int illegal : illegalcolors){ |
|
91 |
if(color == illegal) validColor = false; |
|
92 |
} |
|
93 |
if(validColor) colors.add(color); |
|
94 |
} |
|
95 |
if(colors.size() != 0) colorsToPickFrom = colors.toArray(new Integer[1]); |
|
96 |
} |
|
97 |
int index = (int)Math.round(Math.random()*(colorsToPickFrom.length-1)); |
|
98 |
color = colorsToPickFrom[index]; |
|
99 |
} |
|
100 |
||
101 |
||
102 |
public void sendToEngine(EngineProtocolNetwork epn, int hogCount, int health) throws IOException{ |
|
103 |
epn.sendToEngine(String.format("eaddteam %s %d %s", hash, color, name)); |
|
104 |
epn.sendToEngine(String.format("egrave %s", grave)); |
|
105 |
epn.sendToEngine(String.format("efort %s", fort)); |
|
106 |
epn.sendToEngine(String.format("evoicepack %s", voice)); |
|
107 |
epn.sendToEngine(String.format("eflag %s", flag)); |
|
108 |
||
5463
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff
changeset
|
109 |
for(int i = 0; i < hogCount; i++){ |
5506 | 110 |
epn.sendToEngine(String.format("eaddhh %d %d %s", levels[i], health, hogNames[i])); |
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 | 114 |
|
115 |
/* |
|
116 |
* XML METHODS |
|
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 | 223 |
|
5463
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff
changeset
|
224 |
serializer.startTag(null, "team"); |
5506 | 225 |
serializer.startTag(null, "name"); |
226 |
serializer.text(name); |
|
227 |
serializer.endTag(null, "name"); |
|
228 |
serializer.startTag(null, "flag"); |
|
229 |
serializer.text(flag); |
|
230 |
serializer.endTag(null, "flag"); |
|
231 |
serializer.startTag(null, "fort"); |
|
232 |
serializer.text(fort); |
|
233 |
serializer.endTag(null, "fort"); |
|
234 |
serializer.startTag(null, "grave"); |
|
235 |
serializer.text(grave); |
|
236 |
serializer.endTag(null, "grave"); |
|
237 |
serializer.startTag(null, "voice"); |
|
238 |
serializer.text(voice); |
|
239 |
serializer.endTag(null, "voice"); |
|
240 |
serializer.startTag(null, "hash"); |
|
241 |
serializer.text(hash); |
|
242 |
serializer.endTag(null, "hash"); |
|
243 |
||
244 |
for(int i = 0; i < maxNumberOfHogs; i++){ |
|
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 | 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 | 249 |
serializer.startTag(null, "hat"); |
250 |
serializer.text(hats[i]); |
|
251 |
serializer.endTag(null, "hat"); |
|
252 |
serializer.startTag(null, "level"); |
|
253 |
serializer.text(String.valueOf(levels[i])); |
|
254 |
serializer.endTag(null, "level"); |
|
255 |
||
256 |
serializer.endTag(null, "hog"); |
|
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 | 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 | 270 |
/* |
271 |
* END XML METHODS |
|
272 |
*/ |
|
273 |
||
274 |
||
275 |
||
276 |
/* |
|
277 |
* PARCABLE METHODS |
|
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 | 294 |
dest.writeInt(color); |
295 |
dest.writeInt(hogCount); |
|
5463
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff
changeset
|
296 |
} |
5506 | 297 |
|
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 | 309 |
color = src.readInt(); |
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 | 320 |
|
5463
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff
changeset
|
321 |
}; |
5506 | 322 |
|
323 |
/* |
|
324 |
* END PARCABLE METHODS |
|
325 |
*/ |
|
326 |
||
5463
83c53a80f7ff
datastructures for the different aspects of a gameconfiguration
Xeli
parents:
diff
changeset
|
327 |
} |