1 package cz.cuni.amis.pogamut.ut2004.hideandseek.observer;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Map.Entry;
8
9 import com.google.inject.Inject;
10
11 import cz.cuni.amis.pogamut.base.communication.command.IAct;
12 import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEventListener;
13 import cz.cuni.amis.pogamut.base.communication.worldview.object.event.WorldObjectUpdatedEvent;
14 import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
15 import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
16 import cz.cuni.amis.pogamut.ut2004.agent.params.UT2004AgentParameters;
17 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.ConfigurationObserver;
18 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.InitializeObserver;
19 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Player;
20 import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
21 import cz.cuni.amis.pogamut.ut2004.observer.impl.UT2004Observer;
22
23 public class HSObserver extends UT2004Observer {
24
25 private boolean observing = false;
26
27 private IWorldObjectEventListener<Player, WorldObjectUpdatedEvent<Player>> playerUpdatedListener = new IWorldObjectEventListener<Player, WorldObjectUpdatedEvent<Player>>() {
28
29 @Override
30 public void notify(WorldObjectUpdatedEvent<Player> event) {
31 playerUpdated(event);
32 }
33 };
34
35 private Map<Player, Long> visibleSinceMillis = new HashMap<Player, Long>();
36
37 @Inject
38 public HSObserver(UT2004AgentParameters params, IComponentBus bus, IAgentLogger agentLogger, UT2004WorldView worldView, IAct act) {
39 super(params, bus, agentLogger, worldView, act);
40 if (!(params instanceof HSObserverParams)) throw new RuntimeException("HSObserver must be instantiated with HSObserverParams not " + params.getClass().getSimpleName() + ".");
41 getLogger().addDefaultConsoleHandler();
42 }
43
44 public HSObserverParams getParams() {
45 return (HSObserverParams)super.getParams();
46 };
47
48 protected void startAgent() {
49 super.startAgent();
50
51 getWorldView().addObjectListener(Player.class, WorldObjectUpdatedEvent.class, playerUpdatedListener);
52
53 configureObserver();
54 }
55
56 public void configureObserver() {
57
58 getAct().act(new InitializeObserver().setId(getParams().getBotIdToObserve()));
59
60 getAct().act(
61 new ConfigurationObserver()
62 .setAll(true)
63 .setUpdate(0.21)
64 .setSelf(false)
65 .setAsync(false)
66 .setGame(false)
67 .setSee(true)
68 .setSpecial(false)
69 );
70 log.info("START OBSERVING: " + getParams().getBotIdToObserve());
71 }
72
73 protected void playerUpdated(WorldObjectUpdatedEvent<Player> event) {
74 Player player = event.getObject();
75
76 if (player.isVisible()) {
77
78 synchronized(visibleSinceMillis) {
79 if (visibleSinceMillis.containsKey(player)) {
80
81 return;
82 }
83
84 visibleSinceMillis.put(player, System.currentTimeMillis());
85 }
86 } else {
87 synchronized(visibleSinceMillis) {
88
89 if (visibleSinceMillis.containsKey(player)) {
90
91 visibleSinceMillis.remove(player);
92 return;
93 }
94 }
95 }
96 }
97
98 public List<Player> getPlayersVisibleMoreThanMillis(long millis) {
99 List<Player> result = new ArrayList<Player>(visibleSinceMillis.size());
100 long currTime = System.currentTimeMillis();
101 synchronized(visibleSinceMillis) {
102 for (Entry<Player, Long> entry : visibleSinceMillis.entrySet()) {
103 double visibleFor = currTime - entry.getValue();
104 if (visibleFor > millis) {
105 result.add(entry.getKey());
106 }
107 }
108 }
109 return result;
110 }
111
112 }