Methapolis  0.27
 All Classes Namespaces Files Functions Variables Enumerator
ServerMicropolis.java
Go to the documentation of this file.
1 package micropolisj.network;
2 
3 import java.rmi.RemoteException;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Map.Entry;
7 
8 import micropolisj.engine.GameLevel;
9 import micropolisj.engine.Micropolis;
10 import micropolisj.engine.PlayerInfo;
11 import micropolisj.engine.ToolStroke;
12 import micropolisj.network.PlayerInput.BudgetInput;
13 
14 public class ServerMicropolis extends Micropolis {
15 
18  private Map<Integer, PlayerInfo> playerInfos;
19 
20  // constructor
21  // setup new server
22  public ServerMicropolis() {
23  this(null, null);
24  }
25 
26  // recreate server
27  public ServerMicropolis(RemoteDistributor server, Map<Integer, PlayerInfo> playerInfos) {
28  super();
29  try {
30  if(server == null) {
31  this.server = new RemoteDistributor(this);
32  }
33  else {
34  this.server = server;
35  }
36  socketBuilder = new NetworkServer(this.server);
37  if(playerInfos == null) {
38  this.playerInfos = new HashMap<Integer, PlayerInfo>();
39  this.playerInfos.put(0, new PlayerInfo(this));
40  }
41  else {
42  this.playerInfos = playerInfos;
43  }
44  }
45  catch(RemoteException e) {
46  // TODO Auto-generated catch block
47  e.printStackTrace();
48  }
49  }
50 
51  @Override
52  public void animate() {
53  for(Entry<Integer, PlayerInfo> playerInf : playerInfos.entrySet()) {
54  playerInfo = playerInf.getValue();
55  super.animate();
56  }
57  // gives remoteDistributor MapInfo
59  Map<PlayerInput, Integer> inputs = server.getInput();
60  // reacts to ClientInput
61  for(Entry<PlayerInput, Integer> entry : inputs.entrySet()) {
62  // TODO: react to playerID
63  ToolStroke stroke = entry.getKey().getToolStroke();
64  if(stroke != null) {
65  stroke.setCity(this);
66  stroke.apply();
67  }
68  BudgetInput budgetNum = entry.getKey().getBudgetNumbers();
69  doBudgetInput(budgetNum, getPlayerInfo(entry.getValue()));
70  }
72  }
73 
74  private void doBudgetInput(BudgetInput bud, PlayerInfo pI) {
75  if(bud != null) {
76  pI.cityTax = bud.cityTax;
77  pI.roadPercent = bud.roadPercent;
79  pI.firePercent = bud.policePercent;
81  }
82  }
83 
84  @Override
85  public int getNumberOfPlayers() {
86  return playerInfos.size();
87  }
88 
89  @Override
91  return getPlayerInfo(0);
92  }
93 
94  @Override
95  public PlayerInfo getPlayerInfo(int playerID) {
96  return playerInfos.get(playerID);
97  }
98 
100  MapInfo mapInfo = new MapInfo(map, sprites, cityTime);
101  // mapInfo.cityBudget = playerInfo.budget;
102  return mapInfo;
103  }
104 
105  public void addNewPlayer(int playerID) {
106  PlayerInfo newPlayerInfo = new PlayerInfo(this);
107  newPlayerInfo.budget.totalFunds = GameLevel.getStartingFunds(this.gameLevel);
108  playerInfos.put(playerID, newPlayerInfo);
109  }
110 
112  return server;
113  }
114 
116  this.server = server;
117  }
118 
120  return socketBuilder;
121  }
122 
124  this.socketBuilder = socketBuilder;
125  }
126 
127  public Map<Integer, PlayerInfo> getPlayerInfos() {
128  return playerInfos;
129  }
130 
131  public void setPlayerInfos(Map<Integer, PlayerInfo> playerInfos) {
132  this.playerInfos = playerInfos;
133  }
134 
135 }