Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/zazof/jtegServer/TurnoMH.java


1   package org.zazof.jtegServer;
2   
3   import java.util.*;
4   import org.apache.log4j.Category;
5   
6   /**
7    * This MessageHandler process "turno" messages.
8    */
9   public class TurnoMH extends MessageHandler{
10  
11    public TurnoMH(){
12      $count = 1;
13    }
14  
15    protected void processMessage(Message m){
16      Player p = m.getPlayer();
17      // set the conquered countries of the player to 0, because his turn is over
18      p.setConqueredCountries(0);
19      PlayerContainer pc = m.getGameController().getPlayerContainer();
20      Enumeration activePlayers = pc.getActivePlayers();
21      int nbActivePlayers = pc.getNumberActivePlayers();
22  
23      log.debug("count: " + $count + ", nbActivePlayers: " + nbActivePlayers);
24  
25      // check if we need to start another turn (starting with fichasc)
26      if ($count == nbActivePlayers){
27        Player fichascPlayer = (Player) activePlayers.nextElement();
28        int armiesPlacing = m.getWorld().countArmies(fichascPlayer) / 2;
29        fichascPlayer.setStatus(Player.PLACING_ARMIES);
30        int continentInfo = createContinentInfo(fichascPlayer, m.getWorld());
31        pc.sendMessage("fichasc=" + fichascPlayer.getID() + "," + continentInfo + "," + armiesPlacing);
32        log.debug("Player " + fichascPlayer.getName() + " may start placing " + armiesPlacing + " armies.");
33        $count = 1;
34        return;
35      }
36  
37      for(int i = 0; i < $count; i++){
38        activePlayers.nextElement();
39      }
40      Player turnPlayer = (Player) activePlayers.nextElement();
41      m.getGameController().getPlayerContainer().sendMessage("turno=" + turnPlayer.getID());
42      log.debug("Player " + turnPlayer.getName() + " gets the turn (count: " + $count + ")");
43      $count++;
44    }
45  
46    /** 
47     * creates a magic number :-) which contains info about the countries owned by the player
48     */
49    private int createContinentInfo(Player p, World w){
50      int result = 0;
51      Enumeration continents = w.getContinents();
52      while (continents.hasMoreElements()){
53        Continent c = (Continent) continents.nextElement();
54        if (c.getOwner() == p){
55          // player p owns this continent
56          if (c.getName().equals("South America")) result |= 0x1;
57          if (c.getName().equals("North America")) result |= 0x2;
58          if (c.getName().equals("Africa")) result |= 0x4;
59          if (c.getName().equals("Oceania")) result |= 0x8;
60          if (c.getName().equals("Europe")) result |= 0x10;
61          if (c.getName().equals("Asia")) result |= 0x20;
62        }
63      }
64      return result;
65    }
66  
67    private int $count;
68  
69    static Category log = Category.getInstance("gameplay.TurnoMH");
70  }