1 package cz.cuni.amis.pogamut.ut2004.analyzer;
2
3 import java.io.File;
4
5 import com.google.inject.Inject;
6
7 import cz.cuni.amis.pogamut.base.communication.command.IAct;
8 import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
9 import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
10 import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
11 import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
12 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.ConfigurationObserver;
13 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.InitializeObserver;
14 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.BotKilled;
15 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.GameRestarted;
16 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.MyInventory;
17 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Self;
18 import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
19 import cz.cuni.amis.pogamut.ut2004.observer.impl.UT2004Observer;
20 import cz.cuni.amis.utils.exception.PogamutException;
21
22
23
24
25
26
27
28
29
30 public class UT2004AnalyzerObserver extends UT2004Observer implements IUT2004AnalyzerObserver {
31
32 private UnrealId observedBotId;
33
34 private IWorldEventListener<GameRestarted> gameRestartedListener = new IWorldEventListener<GameRestarted>() {
35
36 @Override
37 public void notify(GameRestarted event) {
38 if (event.isStarted()) {
39 gameRestartStarted();
40 } else
41 if (event.isFinished()) {
42 gameRestartEnd();
43 } else {
44 throw new PogamutException("GameRestarted has started==false && finished==false as well, invalid!", this);
45 }
46 }
47
48 };
49
50 @Inject
51 public UT2004AnalyzerObserver(UT2004AnalyzerObserverParameters params,
52 IComponentBus bus, IAgentLogger agentLogger,
53 UT2004WorldView worldView, IAct act) {
54 super(params, bus, agentLogger, worldView, act);
55 observedBotId = UnrealId.get(params.getObservedAgentId());
56 getWorldView().addEventListener(GameRestarted.class, gameRestartedListener);
57 }
58
59 @Override
60 public UT2004AnalyzerObserverParameters getParams() {
61 return (UT2004AnalyzerObserverParameters) super.getParams();
62 }
63
64 @Override
65 public UnrealId getObservedBotId() {
66 return observedBotId;
67 }
68
69
70
71
72
73 public String getOutputFilePath() {
74 String path = getParams().getOutputPath();
75 if (path == null) path = ".";
76 path += File.separator;
77 if (getParams().getFileName() != null) {
78 path += getParams().getFileName();
79 } else {
80 path += getObservedBotId().toString();
81 path += ".csv";
82 }
83 return path;
84 }
85
86
87
88
89
90
91
92
93
94 protected void gameRestartStarted() {
95 }
96
97
98
99
100
101
102
103
104
105 protected void gameRestartEnd() {
106 }
107
108
109
110
111
112 @Override
113 protected void startAgent() {
114 super.startAgent();
115 getAct().act(new InitializeObserver().setId(getParams().getObservedAgentId()));
116 configureObserver();
117 }
118
119 @Override
120 protected void startPausedAgent() {
121 super.startPausedAgent();
122 getAct().act(new InitializeObserver().setId(getParams().getObservedAgentId()));
123 configureObserver();
124 }
125
126
127
128
129
130
131
132 protected void configureObserver() {
133 getAct().act(new ConfigurationObserver().setUpdate(0.2).setAll(true).setSelf(true).setAsync(true).setGame(false).setSee(false).setSpecial(false));
134 }
135
136 }