1 import java.awt.*;
2 import java.applet.*;
3 import javax.swing.*;
4 import java.awt.image.*;
5 import java.awt.geom.*;
6 import java.net.*;
7 import java.io.*;
8
15 public class PlayerLoader
16 {
17
20 Player p;
21
24 protected boolean fin;
25
28 static BufferedReader br;
29
32 int quotes, bracket, element;
33
36 char c;
37 int ch, i;
38
41 StringBuffer s;
42
43
46 StringBuffer sbtemp = new StringBuffer();
47
48
54 public PlayerLoader(BufferedReader br) throws IOException, InvalidFileFormatException
55 {
56 p = new Player();
57 this.br = br;
58 s = new StringBuffer();
59 quotes = 0;
60 bracket = 0;
61 element = 0;
62 startReading();
63 }
64
70 public Player getPlayer() { return p; }
71
77 protected boolean isChar(int c)
78 {
79 return ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') );
80 }
81
86 protected void startReading() throws IOException, InvalidFileFormatException
87 {
88
89 s.setLength(0);
91 while((ch = br.read()) != '>' && ch != -1)
92 {
93 if(isChar(ch))
94 s.append((char)ch);
95 }
96 if(!s.toString().equalsIgnoreCase("Player"))
97 throw new InvalidFileFormatException("Invalid File Format" + s.toString());
98 bracket++;
99 s.setLength(0);
100
101 while((ch = br.read()) != -1)
102 {
103 if(ch == '\n')
104 continue;
105 else if(ch == '<')
106 {
107 s.setLength(0);
108 bracket++;
109 while((c = (char)br.read()) != '>' && c != ' ')
110 {
111 s.append(c);
112 }
113 String st = s.toString();
114 if(st.equalsIgnoreCase("name"))
115 {
116 s.setLength(0);
117 while((i = br.read()) != '<')
118 {
119 s.append((char)i);
120 }
121 p.setName(s.toString());
122 }
123 else if(st.equalsIgnoreCase("money"))
124 {
125 s.setLength(0);
126 while((i = br.read()) != '<')
127 {
128 s.append((char)i);
129 }
130 st = s.toString();
131 try
132 {
133 p.setMoney(Integer.parseInt(st));
134 }
135 catch(Exception e)
136 {
137 throw new InvalidFileFormatException("Invalid File Format");
138 }
139 }
140 else if(st.equalsIgnoreCase("monsterVector"))
141 {
142 s.setLength(0);
143 while((i = br.read()) != '=');
144 while((i = br.read()) != '>')
145 {
146 s.append((char)i);
147 }
148 st = s.toString();
149 if(st.equalsIgnoreCase("home"))
150 {
151 processMonsterVector(br, p, false);
152 }
153 else if(st.equalsIgnoreCase("hand"))
154 {
155 processMonsterVector(br, p, true);
156 }
157 else
158 throw new InvalidFileFormatException("Invalid File Format");
159 }
160 else if(st.equalsIgnoreCase("itemVector"))
161 {
162 processItemVector(br, p);
163 }
164 else if(st.equalsIgnoreCase("badgeVector"))
165 {
166 processBadge(br, p);
167 }
168 else if(st.indexOf("!") != -1) {
170 continue;
171 }
172 else if(st.equalsIgnoreCase("/player"))
173 {
174 fin = true;
175 continue;
176 }
177 else
178 {
179 throw new InvalidFileFormatException("Invalid File Format");
180 }
181 }
182 else if(ch == '>')
183 bracket--;
184 }
185 fin = true;
186 }
187
195 protected void processMonsterVector(BufferedReader br, Player p, boolean hand) throws IOException, InvalidFileFormatException
196 {
197 int i;
198 String st;
199 while(true)
200 {
201 i = br.read();
202 if(i == '>')
203 {
204 bracket--;
205 }
206 else if(i == '<')
207 {
208 i = br.read();
209 if(i == '/')
210 {
211 s.setLength(0);
212 while((i = br.read()) != '>')
213 {
214 s.append((char)i);
215 }
216 if(s.toString().equalsIgnoreCase("monsterVector"))
217 {
218 s.setLength(0);
219 return;
220 }
221 else
222 {
223 throw new InvalidFileFormatException("Invalid File Format");
224 }
225 }
226 s.setLength(0);
227 s.append((char)i);
228 while((i = br.read()) != '>')
229 {
230 s.append((char)i);
231 }
232 bracket--;
233 if(s.toString().equalsIgnoreCase("monster"))
234 {
235 Monster m = processMonster(br);
236 if(hand)
237 p.addMonster(m);
238 else
239 p.addMonsterHome(m);
240 }
241 else
242 {
243 throw new InvalidFileFormatException("Invalid File Format");
244 }
245 }
246 }
247 }
248
256 protected Monster processMonster(BufferedReader br) throws IOException, InvalidFileFormatException
257 {
258 Monster m = null;
259 int i, pi, pj, lev = 20;
260 String st;
261 while(true)
262 {
263 i = br.read();
264 if(i == '>')
265 bracket--;
266 else if(i == '<')
267 {
268 i = br.read();
269 if(i == '/')
270 {
271 s.setLength(0);
272 while((i = br.read()) != '>')
273 {
274 s.append((char)i);
275 }
276 st = s.toString();
277 if(st.equalsIgnoreCase("kind"))
278 {
279 continue;
280 }
281 else if(st.equalsIgnoreCase("hp"))
282 {
283 continue;
284 }
285 else if(st.equalsIgnoreCase("defp"))
286 {
287 continue;
288 }
289 else if(st.equalsIgnoreCase("attp"))
290 {
291 continue;
292 }
293 else if(st.equalsIgnoreCase("exp"))
294 {
295 continue;
296 }
297 else if(st.equalsIgnoreCase("level"))
298 {
299 continue;
300 }
301 else if(st.equalsIgnoreCase("attack"))
302 {
303 continue;
304 }
305 else if(st.equalsIgnoreCase("monster"))
306 {
307 s.setLength(0);
308 return m;
309 }
310 else
311 {
312 throw new InvalidFileFormatException("Invalid File Format");
313 }
314
315 }
316 s.setLength(0);
317 s.append((char)i);
318 while((i = br.read()) != '>')
319 {
320 s.append((char)i);
321 }
322 bracket++;
323 st = s.toString();
324 if(st.equalsIgnoreCase("hp"))
325 {
326 s.setLength(0);
327 while((i = br.read()) != '<')
328 {
329 s.append((char)i);
330 }
331 try
332 {
333 int hp = Integer.parseInt(s.toString());
334 m.setMaxHP(hp);
335 m.setHP(hp);
336 }
337 catch(Exception e)
338 {
339 throw new InvalidFileFormatException("Invalid File Format");
340 }
341
342 }
343 else if(st.equalsIgnoreCase("defp"))
344 {
345 s.setLength(0);
346 while((i = br.read()) != '<')
347 {
348 s.append((char)i);
349 }
350 try
351 {
352 int def = Integer.parseInt(s.toString());
353 m.setMaxDefense(def);
354 m.setDefense(def);
355 }
356 catch(Exception e)
357 {
358 throw new InvalidFileFormatException("Invalid File Format");
359 }
360 }
361 else if(st.equalsIgnoreCase("attp"))
362 {
363 s.setLength(0);
364 while((i = br.read()) != '<')
365 {
366 s.append((char)i);
367 }
368 try
369 {
370 int att = Integer.parseInt(s.toString());
371 m.setMaxAttack(att);
372 m.setAttack(att);
373 }
374 catch(Exception e)
375 {
376 throw new InvalidFileFormatException("Invalid File Format");
377 }
378 }
379 else if(st.equalsIgnoreCase("exp"))
380 {
381 s.setLength(0);
382 while((i = br.read()) != '<')
383 {
384 s.append((char)i);
385 }
386 try
387 {
388 int exp = Integer.parseInt(s.toString());
389 m.setMaxExperience(m.getLevel()*10);
390 m.setExperience(exp);
391 }
392 catch(Exception e)
393 {
394 throw new InvalidFileFormatException("Invalid File Format");
395 }
396 }
397 else if(st.equalsIgnoreCase("level"))
398 {
399 s.setLength(0);
400 while((i = br.read()) != '<')
401 {
402 s.append((char)i);
403 }
404 try
405 {
406 lev = Integer.parseInt(s.toString());
407 m.setLevel(lev);
408 }
409 catch(Exception e)
410 {
411 throw new InvalidFileFormatException("Invalid File Format");
412 }
413 }
414 else if(st.equalsIgnoreCase("name"))
415 {
416 s.setLength(0);
417 while((i = br.read()) != '<')
418 {
419 s.append((char)i);
420 }
421 m.setName(s.toString());
422 }
423 else if(st.equalsIgnoreCase("attack"))
424 {
425 s.setLength(0);
426 while((i = br.read()) != '<')
427 {
428 s.append((char)i);
429 }
430 st = s.toString();
431 if(m.getNumberOfAttacks() >= Monster.MAXATTACKS)
432 {
433 continue;
434 }
435 if(st.equalsIgnoreCase("A1"))
436 {
437 m.addAttack(m.getA1());
438 }
439 else if(st.equalsIgnoreCase("A2"))
440 {
441 m.addAttack(m.getA2());
442 }
443 else if(st.equalsIgnoreCase("A3"))
444 {
445 m.addAttack(m.getA3());
446 }
447 else if(st.equalsIgnoreCase("A4"))
448 {
449 m.addAttack(m.getA4());
450 }
451 else if(st.equalsIgnoreCase("A5"))
452 {
453 m.addAttack(m.getA5());
454 }
455 else if(st.equalsIgnoreCase("A6"))
456 {
457 m.addAttack(m.getA6());
458 }
459 else
460 {
461 throw new InvalidFileFormatException("Invalid File Format");
462 }
463 }
464 else if(st.equalsIgnoreCase("kind"))
465 {
466 s.setLength(0);
467 while((i = br.read()) != '<')
468 {
469 s.append((char)i);
470 }
471 st = s.toString();
472 if(st.equalsIgnoreCase("Electric"))
473 {
474 m = new ElectricMon();
475 }
476 else if(st.equalsIgnoreCase("EARTH"))
477 {
478 m = new EarthMon();
479 }
480 else if(st.equalsIgnoreCase("Fire"))
481 {
482 m = new FireMon();
483 }
484 else if(st.equalsIgnoreCase("Water"))
485 {
486 m = new WaterMon();
487 }
488 else if(st.equalsIgnoreCase("Gas"))
489 {
490 m = new GasMon();
491 }
492 else
493 {
494 throw new InvalidFileFormatException("Invalid File Format");
495 }
496 }
497 else
498 {
499 throw new InvalidFileFormatException("Invalid File Format");
500 }
501 }
502 }
503 }
504
505
513 protected void processItemVector(BufferedReader br, Player p) throws IOException, InvalidFileFormatException
514 {
515 int i;
516 String st;
517 Item m;
518 while(true)
519 {
520 i = br.read();
521 if(i == '>')
522 {
523 bracket--;
524 }
525 else if(i == '<')
526 {
527 i = br.read();
528 if(i == '/')
529 {
530 s.setLength(0);
531 while((i = br.read()) != '>')
532 {
533 s.append((char)i);
534 }
535 if(s.toString().equalsIgnoreCase("itemVector"))
536 {
537 s.setLength(0);
538 return;
539 }
540 else
541 {
542 throw new InvalidFileFormatException("Invalid File Format");
543 }
544 }
545 s.setLength(0);
546 s.append((char)i);
547 while((i = br.read()) != '>')
548 {
549 s.append((char)i);
550 }
551 bracket--;
552 if(s.toString().equalsIgnoreCase("item"))
553 {
554 m = processItemForVector(br);
555 p.addItem(m);
556 }
557 else
558 {
559 throw new InvalidFileFormatException("Invalid File Format");
560 }
561 }
562 }
563 }
564
572 protected Item processItemForVector(BufferedReader br) throws IOException, InvalidFileFormatException
573 {
574 int i, pi, pj;
575 String st;
576 Item it = null;
577 while(true)
578 {
579 i = br.read();
580 if(i == '>')
581 bracket--;
582 else if(i == '<')
583 {
584 i = br.read();
585 if(i == '/')
586 {
587 s.setLength(0);
588 while((i = br.read()) != '>')
589 {
590 s.append((char)i);
591 }
592 st = s.toString();
593 if(st.equalsIgnoreCase("potion"))
594 {
595 continue;
596 }
597 else if(st.equalsIgnoreCase("quantity"))
598 {
599 continue;
600 }
601 else if(st.equalsIgnoreCase("monsterBall"))
602 {
603 continue;
604 }
605 else if(st.equalsIgnoreCase("item"))
606 {
607 s.setLength(0);
608 return it;
609 }
610 else
611 {
612 throw new InvalidFileFormatException("Invalid File Format");
613 }
614 }
615 s.setLength(0);
616 s.append((char)i);
617 while((i = br.read()) != '>')
618 {
619 s.append((char)i);
620 }
621 bracket++;
622 st = s.toString();
623 if(st.equalsIgnoreCase("potion"))
624 {
625 s.setLength(0);
626 while((i = br.read()) != '<')
627 {
628 s.append((char)i);
629 }
630 st = s.toString();
631 if(st.equalsIgnoreCase("HPUPFULL"))
632 {
633 it = new PotionItem("HP Up Full", PotionItem.HPUPFULL);
634 }
635 else if(st.equalsIgnoreCase("HPUPHALF"))
636 {
637 it = new PotionItem("HP Up Half", PotionItem.HPUPHALF);
638 }
639 else if(st.equalsIgnoreCase("HPUPTHIRD"))
640 {
641 it = new PotionItem("HP Up Third", PotionItem.HPUPTHIRD);
642 }
643 else if(st.equalsIgnoreCase("DEFUP"))
644 {
645 it = new PotionItem("Defense Up", PotionItem.DEFUP);
646 }
647 else if(st.equalsIgnoreCase("ATTUP"))
648 {
649 it = new PotionItem("Attack Up", PotionItem.ATTUP);
650 }
651 else
652 {
653 throw new InvalidFileFormatException("Invalid File Format: At " + st);
654 }
655 }
656 else if(st.equalsIgnoreCase("quantity"))
657 {
658 s.setLength(0);
659 while((i = br.read()) != '<')
660 {
661 s.append((char)i);
662 }
663 st = s.toString();
664 try
665 {
666 int q = Integer.parseInt(st);
667 it.setItemCount(q);
668 }
669 catch(Exception e)
670 {
671 throw new InvalidFileFormatException("Invalid File Format");
672 }
673 }
674 else if(st.equalsIgnoreCase("monsterBall"))
675 {
676 s.setLength(0);
677 while((i = br.read()) != '<')
678 {
679 s.append((char)i);
680 }
681 st = s.toString();
682 if(st.equalsIgnoreCase("NORMAL"))
683 {
684 it = new MonsterBallItem("Normal MonsterBall", MonsterBallItem.NORMAL);
685 }
686 else if(st.equalsIgnoreCase("MASTER"))
687 {
688 it = new MonsterBallItem("Master MonsterBall", MonsterBallItem.MASTER);
689 }
690 else
691 {
692 throw new InvalidFileFormatException("Invalid File Format");
693 }
694 }
695 else
696 {
697 throw new InvalidFileFormatException("Invalid File Format");
698 }
699 }
700 }
701 }
702
703
712 protected void processBadge(BufferedReader br, Player p) throws IOException, InvalidFileFormatException
713 {
714 int i;
715 String st;
716 while(true)
717 {
718 i = br.read();
719 if(i == '>')
720 {
721 bracket--;
722 }
723 else if(i == '<')
724 {
725 i = br.read();
726 if(i == '/')
727 {
728 s.setLength(0);
729 while((i = br.read()) != '>')
730 {
731 s.append((char)i);
732 }
733 if(s.toString().equalsIgnoreCase("badgeVector"))
734 {
735 s.setLength(0);
736 return;
737 }
738 else if(s.toString().equals("badge"))
739 {
740 continue;
741 }
742 else
743 {
744 throw new InvalidFileFormatException("Invalid File Format");
745 }
746 }
747 s.setLength(0);
748 s.append((char)i);
749 while((i = br.read()) != '>')
750 {
751 s.append((char)i);
752 }
753 bracket--;
754 if(s.toString().equalsIgnoreCase("badge"))
755 {
756 s.setLength(0);
757 while((i = br.read()) != '<')
758 {
759 s.append((char)i);
760 }
761 p.addBadge(s.toString());
762 }
763 else
764 {
765 throw new InvalidFileFormatException("Invalid File Format");
766 }
767 }
768 }
769 }
770
771 }