1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.gscg.common.interactionlayer.location;
17
18 import java.rmi.RemoteException;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.Map;
22
23 import nl.tudelft.simulation.actor.ActorInterface;
24 import nl.tudelft.simulation.event.Event;
25 import nl.tudelft.simulation.event.EventListenerInterface;
26 import nl.tudelft.simulation.event.EventProducer;
27 import nl.tudelft.simulation.event.EventType;
28 import nl.tudelft.simulation.language.d3.DirectedPoint;
29 import nl.tudelft.simulation.logger.Logger;
30
31 import org.gscg.common.GameActorInterface;
32 import org.gscg.common.interactionlayer.dataobjects.LocationAndNameData;
33 import org.gscg.gameactors.GameActorInteractiveInterface;
34 import org.gscg.gameleader.interactionlayer.GameLeaderInteractionLayer;
35 import org.gscg.singleuser.interactionlayer.SingleUserInteractionLayerInterface;
36
37 /***
38 * The YellowPage object is a place holder for the actors in an interactive
39 * simulation. Whenever an actor is added or removed this object sends
40 * LocationAndNameData objects to all the client-side graphical user interfaces
41 * which upon receiving this data adjust their WorldPanel. It is furthermore
42 * used to support social communication by means of text messages between human
43 * controlled actors and a game leader.
44 * <p>
45 *
46 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
47 * Delft, the Netherlands. All rights reserved.
48 *
49 * See for project information <a href="http://www.simulation.tudelft.nl/">
50 * www.simulation.tudelft.nl </a>.
51 *
52 * The source code and binary code of this software is proprietary information
53 * of Delft University of Technology.
54 *
55 * @author <a
56 * href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
57 * van Houten </a>
58 * @version $Revision: 1.3 $ $Date: 2005/08/09 15:43:41 $
59 * @since 1.0.0
60 */
61 public class YellowPage extends EventProducer
62 {
63 /*** the serial version uid */
64 private static final long serialVersionUID = 11L;
65
66 /*** to indicate the adding or removing of new actors */
67 public static final EventType UPDATE_ACTORS = new EventType("UPDATE_ACTORS");
68
69 /*** to indicate a change in the status of a human controlled actor */
70 public static final EventType UPDATE_INTERACTIVE_PLAYER_STATUS = new EventType(
71 "UPDATE_INTERACTIVE_PLAYER_STATUS");
72
73 /*** the entries with the actors */
74 private Map entries = new HashMap();
75
76 /***
77 * constructs a new YellowPage
78 */
79 public YellowPage()
80 {
81 super();
82 }
83
84 /***
85 * Method addActor.
86 *
87 * @param actor the actor
88 * @param actorType the type of actor
89 * @param interactive indicates whether the actor is human controlled or not
90 */
91 public void addActor(final GameActorInterface actor,
92 final String actorType, final boolean interactive)
93 {
94 if (!this.entries.containsKey(actor))
95 {
96 try
97 {
98 this.entries.put(actor, new LocationAndNameData(actor
99 .getSmallMapLocation(), actor.getName(), actorType,
100 interactive));
101 this.update(false, null);
102 } catch (Exception exception)
103 {
104 Logger.severe("YellowPage", "addActor", exception);
105 }
106 }
107 }
108
109 /***
110 * Method addGameLeader.
111 *
112 * @param gameLeader the game leader
113 */
114 public void addGameLeader(final GameLeaderInteractionLayer gameLeader)
115 {
116 if (!this.entries.containsKey(gameLeader))
117 {
118 try
119 {
120 this.entries.put(gameLeader, new LocationAndNameData(
121 new DirectedPoint(0.0, 0.0, 0.0), "GAME LEADER",
122 "gameleader", true));
123 this.update(false, null);
124 } catch (Exception exception)
125 {
126 Logger.severe("YellowPage", "addActor", exception);
127 }
128 }
129 }
130
131 /***
132 * Method removeActor.
133 *
134 * @param actor the actor
135 */
136 public void removeActor(final GameActorInterface actor)
137 {
138 if (this.entries.containsKey(actor))
139 {
140 this.entries.remove(actor);
141 this.update(false, null);
142 }
143 }
144
145 /***
146 * Method updateActor.
147 *
148 * @param actor the actor
149 * @param actorType the type of actor
150 * @param interactive indicates whether the actor is human controlled or not
151 */
152 public void updateActor(final GameActorInterface actor,
153 final String actorType, final boolean interactive)
154 {
155 if (this.entries.containsKey(actor))
156 {
157 try
158 {
159 LocationAndNameData data = (LocationAndNameData) this.entries
160 .get(actor);
161 data.setLocation(actor.getSmallMapLocation());
162 data.setName(actor.getName());
163 this.update(false, null);
164 } catch (Exception exception)
165 {
166 Logger.severe("YellowPage", "addActor", exception);
167 }
168 } else
169 {
170 this.addActor(actor, actorType, interactive);
171 }
172 }
173
174 /***
175 * Method updateInteractiveOnlineStatus.
176 *
177 * @param actor the human controlled actor actor to update
178 * @param status the status of the actor, true if the human controlled actor
179 * is online
180 * @param persistent indicates whether the status is changed because of
181 * saving, if true , no event is fired
182 */
183 public void updateInteractiveOnlineStatus(final ActorInterface actor,
184 final boolean status, final boolean persistent)
185 {
186 LocationAndNameData data = (LocationAndNameData) this.entries
187 .get(actor);
188 if (data != null)
189 {
190 data.setStatus(status);
191 if (!persistent)
192 {
193 super
194 .fireEvent(new Event(
195 YellowPage.UPDATE_INTERACTIVE_PLAYER_STATUS,
196 this, data));
197 }
198 }
199 }
200
201 /***
202 * Method updateInteractiveOnlineStatus.
203 *
204 * @param gameLeader the game leader
205 * @param status the status of the actor, true if the human controlled actor
206 * is online
207 * @param persistent indicates whether the status is changed because of
208 * saving, if true , no event is fired
209 */
210 public void updateInteractiveOnlineStatus(
211 final GameLeaderInteractionLayer gameLeader, final boolean status,
212 final boolean persistent)
213 {
214 try
215 {
216 LocationAndNameData data = (LocationAndNameData) this.entries
217 .get(gameLeader);
218 if (data != null)
219 {
220 data.setStatus(status);
221 if (!persistent)
222 {
223
224 super.fireEvent(new Event(
225 YellowPage.UPDATE_INTERACTIVE_PLAYER_STATUS, this,
226 data));
227 }
228 }
229 } catch (Exception exception)
230 {
231 Logger.severe(this, "updateInteractiveOnlineStatus", exception);
232 }
233 }
234
235 /***
236 * Method update.
237 *
238 * @param announce indicates whether this is an announce event
239 * @param owner may be null if announce == false
240 */
241 public void update(final boolean announce,
242 final EventListenerInterface owner)
243 {
244 if (this.entries.values() != null)
245 {
246 LocationAndNameData[] dataArray = (LocationAndNameData[]) this.entries
247 .values().toArray(
248 new LocationAndNameData[this.entries.values()
249 .size()]);
250 if (announce)
251 {
252 try
253 {
254
255 if (SingleUserInteractionLayerInterface.class.isAssignableFrom(
256 owner.getClass()))
257 {
258 ((SingleUserInteractionLayerInterface) owner)
259 .notifyAnnounced(new Event(
260 YellowPage.UPDATE_ACTORS, this,
261 dataArray));
262 } else
263 {
264 owner.notify(new Event(YellowPage.UPDATE_ACTORS, this,
265 dataArray));
266 }
267 } catch (RemoteException remoteException)
268 {
269 Logger.severe(this, "update", remoteException);
270 }
271 } else
272 {
273 super.fireEvent(new Event(YellowPage.UPDATE_ACTORS, this,
274 dataArray));
275 }
276 }
277 }
278
279 /***
280 * Method findActor.
281 *
282 * @param name the name of the actor
283 * @return returns an actor interface based on the name, or null if no actor
284 * could be found
285 */
286 public EventListenerInterface findActor(final String name)
287 {
288 EventListenerInterface result = null;
289 for (Iterator i = this.entries.keySet().iterator(); i.hasNext();)
290 {
291 Object listener = i.next();
292 if (listener instanceof ActorInterface)
293 {
294 if (((ActorInterface) listener).getName().equals(name))
295 {
296 result = ((GameActorInteractiveInterface) listener)
297 .getSingleUserInteractionLayer();
298 break;
299 }
300 } else
301 {
302
303 if (name.equals("GAME LEADER"))
304 {
305 result = (GameLeaderInteractionLayer) listener;
306 } else
307 {
308 result = null;
309 }
310 }
311 }
312
313 return result;
314 }
315
316 /***
317 * @see nl.tudelft.simulation.event.EventProducerInterface#addListener(nl.tudelft.simulation.event.EventListenerInterface,
318 * nl.tudelft.simulation.event.EventType)
319 */
320 public synchronized boolean addListener(
321 final EventListenerInterface listener, final EventType eventType)
322 {
323 try
324 {
325 ((SingleUserInteractionLayerInterface) listener)
326 .addEventType(eventType);
327 } catch (RemoteException remoteException)
328 {
329 Logger.severe(this, "addListener", remoteException);
330 }
331 return super.addListener(listener, eventType, false);
332 }
333 }