16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
17 */ |
17 */ |
18 |
18 |
19 package org.hedgewars.hedgeroid.Datastructures; |
19 package org.hedgewars.hedgeroid.Datastructures; |
20 |
20 |
21 import java.io.BufferedReader; |
|
22 import java.io.File; |
21 import java.io.File; |
23 import java.io.FileNotFoundException; |
|
24 import java.io.FileReader; |
|
25 import java.io.IOException; |
22 import java.io.IOException; |
26 import java.io.OutputStream; |
|
27 import java.util.ArrayList; |
23 import java.util.ArrayList; |
|
24 import java.util.Collections; |
|
25 import java.util.List; |
28 |
26 |
29 import org.hedgewars.hedgeroid.EngineProtocol.EngineProtocolNetwork; |
|
30 import org.hedgewars.hedgeroid.EngineProtocol.PascalExports; |
27 import org.hedgewars.hedgeroid.EngineProtocol.PascalExports; |
31 import org.xmlpull.v1.XmlPullParser; |
28 import org.hedgewars.hedgeroid.frontlib.Flib; |
32 import org.xmlpull.v1.XmlPullParserException; |
29 import org.hedgewars.hedgeroid.frontlib.Frontlib.TeamPtr; |
33 import org.xmlpull.v1.XmlPullParserFactory; |
|
34 import org.xmlpull.v1.XmlSerializer; |
|
35 |
30 |
36 import android.content.Context; |
31 import android.util.Log; |
37 import android.os.Parcel; |
|
38 import android.os.Parcelable; |
|
39 import android.util.Xml; |
|
40 |
32 |
41 public class Team implements Parcelable{ |
33 public final class Team { |
|
34 public static final String DIRECTORY_TEAMS = "teams"; |
42 |
35 |
43 public static final String DIRECTORY_TEAMS = "teams"; |
36 public static final int maxNumberOfHogs = PascalExports.HWgetMaxNumberOfHogs(); |
44 private static final Integer[] TEAM_COLORS = { |
37 public static final int maxNumberOfTeams = PascalExports.HWgetMaxNumberOfTeams(); |
45 0xd12b42, /* red */ |
|
46 0x4980c1, /* blue */ |
|
47 0x6ab530, /* green */ |
|
48 0xbc64c4, /* purple */ |
|
49 0xe76d14, /* orange */ |
|
50 0x3fb6e6, /* cyan */ |
|
51 0xe3e90c, /* yellow */ |
|
52 0x61d4ac, /* mint */ |
|
53 0xf1c3e1, /* pink */ |
|
54 /* add new colors here */ |
|
55 }; |
|
56 |
38 |
57 // private static final Integer[] TEAM_COLORS = { |
39 public final String name, grave, flag, voice, fort; |
58 // 0xff0000, /* red */ |
40 public final List<Hog> hogs; |
59 // 0x00ff00, /* blue */ |
|
60 // 0x0000ff, /* green */ |
|
61 // }; |
|
62 |
41 |
63 private static final int STATE_START = 0; |
42 public Team(String name, String grave, String flag, String voice, String fort, List<Hog> hogs) { |
64 private static final int STATE_ROOT = 1; |
43 if(hogs.size() != maxNumberOfHogs) { |
65 private static final int STATE_HOG_ROOT = 2; |
44 throw new IllegalArgumentException("A team must consist of "+maxNumberOfHogs+" hogs."); |
66 |
45 } |
67 public String name, grave, flag, voice, fort, hash; |
46 this.name = name; |
68 public String file = null; |
47 this.grave = grave; |
69 |
48 this.flag = flag; |
70 public static int maxNumberOfHogs = 0; |
49 this.voice = voice; |
71 public static int maxNumberOfTeams = 0; |
50 this.fort = fort; |
72 |
51 this.hogs = Collections.unmodifiableList(new ArrayList<Hog>(hogs)); |
73 static{ |
|
74 maxNumberOfHogs = PascalExports.HWgetMaxNumberOfHogs(); |
|
75 maxNumberOfTeams = PascalExports.HWgetMaxNumberOfTeams(); |
|
76 } |
|
77 public String[] hats = new String[maxNumberOfHogs]; |
|
78 public String[] hogNames = new String[maxNumberOfHogs]; |
|
79 public int[] levels = new int[maxNumberOfHogs]; |
|
80 |
|
81 public int hogCount = 4; |
|
82 public int color = TEAM_COLORS[0]; |
|
83 |
|
84 public Team(){ |
|
85 } |
52 } |
86 |
53 |
87 public Team(Parcel in){ |
54 public void save(File f) throws IOException { |
88 readFromParcel(in); |
55 Log.d("Team", "saving to "+f.getAbsolutePath()); |
|
56 TeamPtr teamPtr = TeamPtr.createJavaOwned(this); |
|
57 if(Flib.INSTANCE.flib_team_to_ini(f.getAbsolutePath(), teamPtr) != 0) { |
|
58 throw new IOException("Error saving team "+name); |
|
59 } |
|
60 } |
|
61 |
|
62 public static Team load(File f) { |
|
63 TeamPtr teamPtr = Flib.INSTANCE.flib_team_from_ini(f.getAbsolutePath()); |
|
64 if(teamPtr != null) { |
|
65 Team team = teamPtr.deref().team; |
|
66 Flib.INSTANCE.flib_team_destroy(teamPtr); |
|
67 return team; |
|
68 } else { |
|
69 return null; |
|
70 } |
89 } |
71 } |
90 |
72 |
91 @Override |
73 @Override |
92 public boolean equals(Object o){ |
74 public String toString() { |
93 if(super.equals(o)) return true; |
75 return "Team [name=" + name + ", grave=" + grave + ", flag=" + flag |
94 else if(o instanceof Team){ |
76 + ", voice=" + voice + ", fort=" + fort + ", hogs=" + hogs |
95 Team t = (Team)o; |
77 + "]"; |
96 boolean ret = name.equals(t.name); |
|
97 ret &= grave.equals(t.grave); |
|
98 ret &= flag.equals(t.flag); |
|
99 ret &= voice.equals(t.voice); |
|
100 ret &= fort.equals(t.fort); |
|
101 ret &= hash.equals(t.hash); |
|
102 return ret; |
|
103 }else{ |
|
104 return false; |
|
105 } |
|
106 } |
78 } |
107 |
|
108 @Override |
|
109 public int hashCode() { |
|
110 final int prime = 31; |
|
111 int result = 1; |
|
112 result = prime * result + ((name == null) ? 0 : name.hashCode()); |
|
113 result = prime * result + ((grave == null) ? 0 : grave.hashCode()); |
|
114 result = prime * result + ((flag == null) ? 0 : flag.hashCode()); |
|
115 result = prime * result + ((voice == null) ? 0 : voice.hashCode()); |
|
116 result = prime * result + ((fort == null) ? 0 : fort.hashCode()); |
|
117 result = prime * result + ((hash == null) ? 0 : hash.hashCode()); |
|
118 return result; |
|
119 } |
|
120 |
|
121 public void setRandomColor(int[] illegalcolors){ |
|
122 Integer[] colorsToPickFrom = TEAM_COLORS; |
|
123 if(illegalcolors != null){ |
|
124 ArrayList<Integer> colors = new ArrayList<Integer>(); |
|
125 for(int color : TEAM_COLORS){ |
|
126 boolean validColor = true; |
|
127 for(int illegal : illegalcolors){ |
|
128 if(color == illegal) validColor = false; |
|
129 } |
|
130 if(validColor) colors.add(color); |
|
131 } |
|
132 if(colors.size() != 0) colorsToPickFrom = colors.toArray(new Integer[1]); |
|
133 } |
|
134 int index = (int)Math.round(Math.random()*(colorsToPickFrom.length-1)); |
|
135 color = colorsToPickFrom[index]; |
|
136 } |
|
137 |
|
138 |
|
139 public void sendToEngine(EngineProtocolNetwork epn, int hogCount, int health) throws IOException{ |
|
140 epn.sendToEngine(String.format("eaddteam %s %d %s", hash, color, name)); |
|
141 epn.sendToEngine(String.format("egrave %s", grave)); |
|
142 epn.sendToEngine(String.format("efort %s", fort)); |
|
143 epn.sendToEngine(String.format("evoicepack %s", voice)); |
|
144 epn.sendToEngine(String.format("eflag %s", flag)); |
|
145 |
|
146 for(int i = 0; i < hogCount; i++){ |
|
147 epn.sendToEngine(String.format("eaddhh %d %d %s", levels[i], health, hogNames[i])); |
|
148 epn.sendToEngine(String.format("ehat %s", hats[i])); |
|
149 } |
|
150 } |
|
151 |
|
152 public void setFileName(Context c){ |
|
153 if(file == null){ |
|
154 file = validFileName(c, name); |
|
155 } |
|
156 } |
|
157 private String validFileName(Context c, String fileName){ |
|
158 String absolutePath = String.format("%s/%s", c.getFilesDir(), fileName); |
|
159 File f = new File(absolutePath); |
|
160 if(f.exists()){ |
|
161 String newFileName = fileName + (int)(Math.random()*10); |
|
162 return validFileName(c, newFileName); |
|
163 }else{ |
|
164 return fileName; |
|
165 } |
|
166 } |
|
167 |
|
168 /* |
|
169 * XML METHODS |
|
170 */ |
|
171 |
|
172 /** |
|
173 * Read the xml file path and convert it to a Team object |
|
174 * @param path absolute path to the xml file |
|
175 * @return |
|
176 */ |
|
177 public static Team getTeamFromXml(String path){ |
|
178 try { |
|
179 XmlPullParserFactory xmlPullFactory = XmlPullParserFactory.newInstance(); |
|
180 XmlPullParser xmlPuller = xmlPullFactory.newPullParser(); |
|
181 |
|
182 BufferedReader br = new BufferedReader(new FileReader(path), 1024); |
|
183 xmlPuller.setInput(br); |
|
184 Team team = new Team(); |
|
185 int hogCounter = 0; |
|
186 |
|
187 int eventType = xmlPuller.getEventType(); |
|
188 int state = STATE_START; |
|
189 while(eventType != XmlPullParser.END_DOCUMENT){ |
|
190 switch(state){ |
|
191 case STATE_START: |
|
192 if(eventType == XmlPullParser.START_TAG && xmlPuller.getName().equals("team")) state = STATE_ROOT; |
|
193 else if(eventType != XmlPullParser.START_DOCUMENT) throwException(path, eventType); |
|
194 break; |
|
195 case STATE_ROOT: |
|
196 if(eventType == XmlPullParser.START_TAG){ |
|
197 if(xmlPuller.getName().toLowerCase().equals("name")){ |
|
198 team.name = getXmlText(xmlPuller, "name"); |
|
199 }else if(xmlPuller.getName().toLowerCase().equals("flag")){ |
|
200 team.flag= getXmlText(xmlPuller, "flag"); |
|
201 }else if(xmlPuller.getName().toLowerCase().equals("voice")){ |
|
202 team.voice = getXmlText(xmlPuller, "voice"); |
|
203 }else if(xmlPuller.getName().toLowerCase().equals("grave")){ |
|
204 team.grave = getXmlText(xmlPuller, "grave"); |
|
205 }else if(xmlPuller.getName().toLowerCase().equals("fort")){ |
|
206 team.fort = getXmlText(xmlPuller, "fort"); |
|
207 }else if(xmlPuller.getName().toLowerCase().equals("hash")){ |
|
208 team.hash = getXmlText(xmlPuller, "hash"); |
|
209 }else if(xmlPuller.getName().toLowerCase().equals("hog")){ |
|
210 state = STATE_HOG_ROOT; |
|
211 }else throwException(xmlPuller.getName(), eventType); |
|
212 }else if(eventType == XmlPullParser.END_TAG) state = STATE_START; |
|
213 else throwException(xmlPuller.getText(), eventType); |
|
214 break; |
|
215 case STATE_HOG_ROOT: |
|
216 if(eventType == XmlPullParser.START_TAG){ |
|
217 if(xmlPuller.getName().toLowerCase().equals("name")){ |
|
218 team.hogNames[hogCounter] = getXmlText(xmlPuller, "name"); |
|
219 }else if(xmlPuller.getName().toLowerCase().equals("hat")){ |
|
220 team.hats[hogCounter] = getXmlText(xmlPuller, "hat"); |
|
221 }else if(xmlPuller.getName().toLowerCase().equals("level")){ |
|
222 team.levels[hogCounter] = Integer.parseInt(getXmlText(xmlPuller, "level")); |
|
223 }else throwException(xmlPuller.getText(), eventType); |
|
224 }else if(eventType == XmlPullParser.END_TAG){ |
|
225 hogCounter++; |
|
226 state = STATE_ROOT; |
|
227 }else throwException(xmlPuller.getText(), eventType); |
|
228 break; |
|
229 } |
|
230 eventType = getEventType(xmlPuller); |
|
231 }//end while(eventtype != END_DOCUMENT |
|
232 return team; |
|
233 } catch (NumberFormatException e){ |
|
234 e.printStackTrace(); |
|
235 } catch (XmlPullParserException e) { |
|
236 e.printStackTrace(); |
|
237 } catch (FileNotFoundException e) { |
|
238 e.printStackTrace(); |
|
239 } catch (IOException e) { |
|
240 e.printStackTrace(); |
|
241 } |
|
242 return null; |
|
243 } |
|
244 |
|
245 private static String getXmlText(XmlPullParser xmlPuller, String parentTag)throws XmlPullParserException, IOException{ |
|
246 if(getEventType(xmlPuller) == XmlPullParser.TEXT){ |
|
247 String txt = xmlPuller.getText(); |
|
248 if(getEventType(xmlPuller) == XmlPullParser.END_TAG && xmlPuller.getName().toLowerCase().equals(parentTag)){ |
|
249 return txt; |
|
250 } |
|
251 } |
|
252 throw new XmlPullParserException("malformed xml file on string read from tag: " + parentTag); |
|
253 } |
|
254 |
|
255 /** |
|
256 * Skips whitespaces.. |
|
257 */ |
|
258 private static int getEventType(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{ |
|
259 int eventType = xmlPuller.next(); |
|
260 while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){ |
|
261 eventType = xmlPuller.next(); |
|
262 } |
|
263 return eventType; |
|
264 } |
|
265 |
|
266 private static void throwException(String file, int eventType){ |
|
267 throw new IllegalArgumentException(String.format("Xml file: %s malformed with error: %d.", file, eventType)); |
|
268 } |
|
269 |
|
270 public void writeToXml(OutputStream os){ |
|
271 XmlSerializer serializer = Xml.newSerializer(); |
|
272 try{ |
|
273 serializer.setOutput(os, "UTF-8"); |
|
274 serializer.startDocument("UTF-8", true); |
|
275 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
|
276 |
|
277 serializer.startTag(null, "team"); |
|
278 serializer.startTag(null, "name"); |
|
279 serializer.text(name); |
|
280 serializer.endTag(null, "name"); |
|
281 serializer.startTag(null, "flag"); |
|
282 serializer.text(flag); |
|
283 serializer.endTag(null, "flag"); |
|
284 serializer.startTag(null, "fort"); |
|
285 serializer.text(fort); |
|
286 serializer.endTag(null, "fort"); |
|
287 serializer.startTag(null, "grave"); |
|
288 serializer.text(grave); |
|
289 serializer.endTag(null, "grave"); |
|
290 serializer.startTag(null, "voice"); |
|
291 serializer.text(voice); |
|
292 serializer.endTag(null, "voice"); |
|
293 serializer.startTag(null, "hash"); |
|
294 serializer.text(hash); |
|
295 serializer.endTag(null, "hash"); |
|
296 |
|
297 for(int i = 0; i < maxNumberOfHogs; i++){ |
|
298 serializer.startTag(null, "hog"); |
|
299 serializer.startTag(null, "name"); |
|
300 serializer.text(hogNames[i]); |
|
301 serializer.endTag(null, "name"); |
|
302 serializer.startTag(null, "hat"); |
|
303 serializer.text(hats[i]); |
|
304 serializer.endTag(null, "hat"); |
|
305 serializer.startTag(null, "level"); |
|
306 serializer.text(String.valueOf(levels[i])); |
|
307 serializer.endTag(null, "level"); |
|
308 |
|
309 serializer.endTag(null, "hog"); |
|
310 } |
|
311 serializer.endTag(null, "team"); |
|
312 serializer.endDocument(); |
|
313 serializer.flush(); |
|
314 |
|
315 } catch (IOException e) { |
|
316 e.printStackTrace(); |
|
317 }finally{ |
|
318 try { |
|
319 os.close(); |
|
320 } catch (IOException e) {} |
|
321 } |
|
322 } |
|
323 /* |
|
324 * END XML METHODS |
|
325 */ |
|
326 |
|
327 |
|
328 |
|
329 /* |
|
330 * PARCABLE METHODS |
|
331 */ |
|
332 |
|
333 public int describeContents() { |
|
334 return 0; |
|
335 } |
|
336 |
|
337 public void writeToParcel(Parcel dest, int flags) { |
|
338 dest.writeString(name); |
|
339 dest.writeString(grave); |
|
340 dest.writeString(flag); |
|
341 dest.writeString(voice); |
|
342 dest.writeString(fort); |
|
343 dest.writeString(hash); |
|
344 dest.writeStringArray(hats); |
|
345 dest.writeStringArray(hogNames); |
|
346 dest.writeIntArray(levels); |
|
347 dest.writeInt(color); |
|
348 dest.writeInt(hogCount); |
|
349 dest.writeString(file); |
|
350 } |
|
351 |
|
352 |
|
353 public void readFromParcel(Parcel src){ |
|
354 name = src.readString(); |
|
355 grave = src.readString(); |
|
356 flag = src.readString(); |
|
357 voice = src.readString(); |
|
358 fort = src.readString(); |
|
359 hash = src.readString(); |
|
360 src.readStringArray(hats); |
|
361 src.readStringArray(hogNames); |
|
362 src.readIntArray(levels); |
|
363 color = src.readInt(); |
|
364 hogCount = src.readInt(); |
|
365 file = src.readString(); |
|
366 } |
|
367 |
|
368 public static final Parcelable.Creator<Team> CREATOR = new Parcelable.Creator<Team>() { |
|
369 public Team createFromParcel(Parcel source) { |
|
370 return new Team(source); |
|
371 } |
|
372 public Team[] newArray(int size) { |
|
373 return new Team[size]; |
|
374 } |
|
375 |
|
376 }; |
|
377 |
|
378 /* |
|
379 * END PARCABLE METHODS |
|
380 */ |
|
381 |
|
382 } |
79 } |