Source code: org/zazof/jteg/AttaqueMessage.java
1 package org.zazof.jteg;
2
3 import java.util.StringTokenizer;
4
5 /**
6 *
7 * An observer gets this messages "attaque=..." when it logs in.
8 * <br>
9 * Message that processes "attaque=" events.
10 * This message is valid for following protocols:
11 * <ul>
12 * <li>version 3: OK
13 * <li>version 4: ?
14 * <li>version 5: ?
15 * </ul>
16 * Content = "ataque=x,y" ; country x attaques country y
17 *
18 * @author Jef De Geeter
19 * @author Yves Vandewoude
20 */
21
22 public class AttaqueMessage extends Message{
23
24 // token contains
25 public AttaqueMessage(Message nextMessage){
26 setNextMessage(nextMessage);
27 }
28
29 public Message decodeMessage(String messageDescription)
30 throws UnknownMessageException
31 {
32 String firstToken = (new StringTokenizer(messageDescription,"=")).nextToken();
33 if (!(firstToken.equals("ataque")))
34 {
35 // Not for me...
36 if (this.hasNextMessage())
37 {
38 return getNextMessage().decodeMessage(messageDescription);
39 }
40 throw new UnknownMessageException(messageDescription);
41 }
42 else
43 {
44 StringTokenizer st = new StringTokenizer(messageDescription, "=,");
45 st.nextToken(); //ataque
46 $from = Integer.parseInt(st.nextToken());
47 $to = Integer.parseInt(st.nextToken());
48 if (DEBUG) System.out.println("AttaqueMessage: from " + $from + ", to " + $to);
49 return this;
50 }
51 }
52
53 public String constructMessageString(String[] messageParts)
54 throws UnknownMessageException
55 {
56 if (!(messageParts[0]).equals("attack"))
57 {
58 // Not for me...
59 if (this.hasNextMessage())
60 {
61 return getNextMessage().constructMessageString(messageParts);
62 }
63 throw new UnknownMessageException("No String could be constructed for a message with this parameter: " + messageParts[0]);
64 }
65 else
66 {
67 StringBuffer sb = new StringBuffer("ataque=");
68 sb.append(messageParts[1]); // From CountryID
69 sb.append(",");
70 sb.append(messageParts[2]); // To CountryID
71 return sb.toString();
72 }
73 }
74
75
76
77
78 public String getMessageName(){
79 return "attaque";
80 }
81
82 /*
83 @return country_id of the country who is attacking
84 */
85 public int attackFrom(){
86 return $from;
87 }
88
89 /*
90 @return country_id of the country which has been attacked
91 */
92 public int attackTo(){
93 return $to;
94 }
95
96 private int $from; // country id which attackues
97 private int $to; // country id which is been attackued
98 private static boolean DEBUG = false;
99 }