Source code: org/zazof/jtegServer/FichasMH.java
1 package org.zazof.jtegServer;
2
3 import java.util.*;
4 import org.apache.log4j.Category;
5
6 /**
7 * This MessageHandler process "fichas", "fichas2" and "fichasc" messages.
8 */
9 public class FichasMH extends MessageHandler{
10
11 public FichasMH(){
12 }
13
14 protected void processMessage(Message m){
15 Player p = m.getPlayer();
16
17 // update the status of the player
18 if (mode == FICHAS) p.setStatus(Player.POST_ARMIES);
19 if (mode == FICHAS2) p.setStatus(Player.POST_ARMIES2);
20 if (mode == FICHASC) p.setStatus(Player.IDLE);
21
22 int nbActivePlayers = m.getGameController().getPlayerContainer().getNumberActivePlayers();
23 if (m.getMessageID().equals("fichasc")) mode = FICHASC;
24 log.debug("count: " + $count + ", nbActivePlayers: " + nbActivePlayers + ", mode: " + mode);
25
26 // parse the content of the message and do some checking
27 int index = 0;
28 StringTokenizer content = m.getContentStringTokenizer(":,");
29 // buffers for the message data
30 int countries[] = new int[64];
31 int armies[] = new int[64];
32 while(content.hasMoreTokens()){
33 countries[index] = Integer.parseInt((String) content.nextElement());
34 armies[index] = Integer.parseInt((String) content.nextElement());
35 Country c = m.getWorld().getCountry(countries[index]);
36 // check if this player owns the country
37 if (p != c.getOwner()){
38 // player does not own this country
39 if (mode == FICHAS) p.sendMessage("error=fichas");
40 if (mode == FICHAS2) p.sendMessage("error=fichas2");
41 if (mode == FICHASC) p.sendMessage("error=fichasc");
42 return;
43 }
44 // check if armies > 0
45 if (armies[index] <= 0){
46 if (mode == FICHAS) p.sendMessage("error=fichas");
47 if (mode == FICHAS2) p.sendMessage("error=fichas2");
48 if (mode == FICHASC) p.sendMessage("error=fichasc");
49 return;
50 }
51 index++;
52 }
53
54 // all data is valid, update the gamemodel
55 updateGameModel(m.getWorld(), index, countries, armies);
56
57 // decide what we have to send back to the clients
58 Enumeration activePlayers = m.getGameController().getPlayerContainer().getElements();
59 PlayerContainer pc = m.getGameController().getPlayerContainer();
60 if ($count == nbActivePlayers && mode == FICHAS){
61 // start witch fichas2
62 log.debug("start with fichas2");
63 mode = FICHAS2;
64 $count = 0;
65 }
66 if ($count == nbActivePlayers && mode == FICHAS2){
67 nextPlayer = (Player) activePlayers.nextElement();
68 log.debug("fichas2 ended, give the turn to player: " + nextPlayer.getName());
69 pc.sendMessage("turno=" + nextPlayer.getID());
70 $count = 1;
71 return;
72 }
73 if ($count == nbActivePlayers && mode == FICHASC){
74 nextPlayer = (Player) activePlayers.nextElement();
75 log.debug("fichasc ended, give the turn to player: " + nextPlayer.getName());
76 pc.sendMessage("turno=" + nextPlayer.getID());
77 $count = 1;
78 return;
79 }
80 if ($count < nbActivePlayers){
81 // find the next player
82 for(int i = 0; i < $count; i++){
83 activePlayers.nextElement();
84 }
85 nextPlayer = (Player) activePlayers.nextElement();
86 log.debug("count: " + $count + ", nbActivePlayers: " + nbActivePlayers + ", mode: " + mode + ", next player: " + nextPlayer.getName());
87 nextPlayer.setStatus(Player.PLACING_ARMIES);
88
89 if (mode == FICHAS) pc.sendMessage("fichas=" + nextPlayer.getID() + ",5");
90 if (mode == FICHAS2) pc.sendMessage("fichas2=" + nextPlayer.getID() + ",3");
91 if (mode == FICHASC){
92 int armiesPlacing = m.getWorld().countArmies(nextPlayer) / 2;
93 log.debug("player " + nextPlayer.getName() + " must place " + armiesPlacing + " armies");
94 int continentInfo = createContinentInfo(nextPlayer, m.getWorld());
95 pc.sendMessage("fichasc=" + nextPlayer.getID() + "," + continentInfo + "," + armiesPlacing);
96 }
97 $count++;
98 }
99 }
100
101 /**
102 * if all data is valid, update the model
103 */
104 private void updateGameModel(World w, int nbCountries, int countries[], int armies[]){
105 for(int i=0; i<nbCountries; i++){
106 Country c = w.getCountry(countries[i]);
107 c.addArmies(armies[i]);
108 }
109 }
110
111 /**
112 * creates a magic number :-) which contains info about the countries owned by the player
113 */
114 private int createContinentInfo(Player p, World w){
115 int result = 0;
116 Enumeration continents = w.getContinents();
117 while (continents.hasMoreElements()){
118 Continent c = (Continent) continents.nextElement();
119 if (c.getOwner() == p){
120 // player p owns this continent
121 if (c.getName().equals("South America")) result |= 0x1;
122 if (c.getName().equals("North America")) result |= 0x2;
123 if (c.getName().equals("Africa")) result |= 0x4;
124 if (c.getName().equals("Oceania")) result |= 0x8;
125 if (c.getName().equals("Europe")) result |= 0x10;
126 if (c.getName().equals("Asia")) result |= 0x20;
127 }
128 }
129 return result;
130 }
131
132 private Player nextPlayer;
133 private int $count = 1;
134
135 private int FICHAS = 0;
136 private int FICHAS2 = 1;
137 private int FICHASC = 2;
138 private int mode = FICHAS;
139
140 static Category log = Category.getInstance("gameplay.FichasMH");
141 }