forked from Drakulix/GameReplay.Mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recorder.cs
234 lines (188 loc) · 9.24 KB
/
Recorder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace GameReplay.Mod
{
public class Recorder : ICommListener, IOkCancelCallback
{
private List<String> messages = new List<String>();
private String saveFolder;
private string gameID;
private Mod uiClass;
//private DateTime timestamp;
private MiniCommunicator comm;
public bool recording = false;
private BattleMode bttlmd = null;
private BattleModeUI bttlmdUI = null;
private GameStateCreator gsc;
private int lastwasturn = 0;
private bool lastwasturnwhite=true;
private List<Message> tempmessages = new List<Message>();
private Settings settings;
public Recorder(String saveFolder, Mod uiClass, Settings stngs)
{
this.settings = stngs;
this.saveFolder = saveFolder;
//App.Communicator.addListener(this);
this.comm = App.Communicator;
this.comm.addListener(this);
/*IpPort address = App.SceneValues.battleMode.address;
if (!address.Equals(App.Communicator.getAddress()))
{
this.comm = App.SceneValues.battleMode.specCommGameObject.GetComponent<MiniCommunicator>();
this.comm.addListener(this);
this.comm.setEnabled(true, true);
}
*/
this.uiClass = uiClass;
this.recording = true;
//timestamp = DateTime.Now;
gsc = new GameStateCreator();
}
public void recordSpectator()
{
this.comm.removeListener(this);
IpPort address = App.SceneValues.battleMode.address;
if (!address.Equals(App.Communicator.getAddress()))
{
this.comm = App.SceneValues.battleMode.specCommGameObject.GetComponent<MiniCommunicator>();
this.comm.addListener(this);
this.comm.setEnabled(true, true);
}
messages.Add("{\"version\":\"0.112.1\",\"assetURL\":\"http://download.scrolls.com/assets/\",\"roles\":\"GAME,RESOURCE\",\"msg\":\"ServerInfo\"}");
}
public void handleMessage(Message msg)
{
// save the message on help-list
if (msg is SpectateRedirectMessage || msg is BattleRedirectMessage || msg is BattleRejoinMessage || msg is FailMessage || msg is OkMessage || msg is PingMessage || msg is WhisperMessage || msg is RoomInfoMessage || msg is HandViewMessage || msg is AbilityInfoMessage) // whispers are private, pings are uncessary
{
return;
}
if (msg is GameInfoMessage)
{
gameID = (msg as GameInfoMessage).gameId.ToString();
}
tempmessages.Add(msg);
//set the idol hp
if (msg is GameStateMessage)//updating idols, for start of game
{
gsc.GameStateupdateIdols((msg as GameStateMessage).whiteGameState.board.idols, true);
gsc.GameStateupdateIdols((msg as GameStateMessage).blackGameState.board.idols, false);
gsc.setTurn((msg as GameStateMessage).turn);
gsc.whitePlayerName = (msg as GameStateMessage).whiteGameState.playerName;
gsc.blackPlayerName = (msg as GameStateMessage).blackGameState.playerName;
}
if (msg is GameInfoMessage)//updating idols, for start of game
{
gsc.updateIdols((msg as GameInfoMessage).whiteIdols);
gsc.updateIdols((msg as GameInfoMessage).blackIdols);
gsc.setTurn(0);
gsc.whitePlayerName = (msg as GameInfoMessage).white;
gsc.blackPlayerName = (msg as GameInfoMessage).black;
}
saveTillBeginTurn(); // try to save the tempmessages in messages
}
private void saveTillBeginTurn()
{
while (tempmessages.Count >= 1 && !tempmessages[0].getRawText().Contains("TurnBegin"))
{
Message msg = tempmessages[0];
tempmessages.RemoveAt(0);
this.messages.Add(msg.getRawText());
Console.WriteLine("## add to recorder:" + msg.getRawText() + "\r\n###");
if (msg is NewEffectsMessage && msg.getRawText().Contains("IdolUpdate"))//updating idols, battlemode is to slow for us
{
List<EffectMessage> idolupdates = new List<EffectMessage>();
foreach (EffectMessage current in NewEffectsMessage.parseEffects(msg.getRawText()))
{
if (current.type == "IdolUpdate") { idolupdates.Add(current); }
}
gsc.updateIdols(idolupdates);
}
if (msg is NewEffectsMessage && msg.getRawText().Contains("EndGame"))
{
this.recording = false;
//save
this.comm.removeListener(this);
if (this.settings.alwayssave)
{
File.WriteAllLines(saveFolder + Path.DirectorySeparatorChar + gameID + ".sgr", messages.ToArray());
uiClass.parseRecord(saveFolder + Path.DirectorySeparatorChar + gameID + ".sgr");
Console.WriteLine("Save Recorded Game");
}
else
{
App.Popups.ShowOkCancel(this, "savereplay", "Save", "Want to save the recorded game?", "Ok", "No");
}
//File.WriteAllLines(saveFolder + Path.DirectorySeparatorChar + gameID + ".sgr", messages.ToArray());
//uiClass.parseRecord (saveFolder + Path.DirectorySeparatorChar + gameID + ".sgr");
}
}
}
public void turnBeginEnds()
{
saveTillBeginTurn();// maybe there are messages in front of turnbegin messages (prob. = 0)
// save beginTurn effect
if(tempmessages.Count==0) return;
Message msg = tempmessages[0];
this.messages.Add(msg.getRawText());
Console.WriteLine("### add to recorder (turnbegin):" + msg.getRawText() + "\r\n###");
if (msg is NewEffectsMessage && msg.getRawText().Contains("TurnBegin"))
{
List<EffectMessage> idolupdates = new List<EffectMessage>();
foreach (EffectMessage current in NewEffectsMessage.parseEffects(msg.getRawText()))
{
if (current.type == "TurnBegin") { gsc.setTurn((current as EMTurnBegin).turn); }
}
this.lastwasturnwhite = true;
if (msg.getRawText().Contains("black")) this.lastwasturnwhite = false;
}
tempmessages.RemoveAt(0); //remove the turnbegin messages
// create gamestatus + save it
string mygsc = gsc.create(this.bttlmd, this.bttlmdUI, this.lastwasturnwhite);
Console.WriteLine("##created:" + mygsc);
this.messages.Add(mygsc);
// add the next saved messages
saveTillBeginTurn();
}
public void onConnect(OnConnectData ocd)
{
return; //I (still) don't care
}
public void stoprecording()
{
this.recording = false;
//save
messages.Add("{\"effects\":[{\"EndGame\":{\"winner\":\"black\",\"whiteStats\":{\"profileId\":\"RobotEasy\",\"idolDamage\":0,\"unitDamage\":0,\"unitsPlayed\":0,\"spellsPlayed\":0,\"enchantmentsPlayed\":0,\"scrollsDrawn\":0,\"totalMs\":1,\"mostDamageUnit\":0,\"idolsDestroyed\":0},\"blackStats\":{\"profileId\":\"RobotEasy\",\"idolDamage\":0,\"unitDamage\":0,\"unitsPlayed\":0,\"spellsPlayed\":0,\"enchantmentsPlayed\":0,\"scrollsDrawn\":0,\"totalMs\":1,\"mostDamageUnit\":0,\"idolsDestroyed\":0},\"whiteGoldReward\":{\"matchReward\":0,\"matchCompletionReward\":0,\"idolsDestroyedReward\":0,\"totalReward\":0},\"blackGoldReward\":{\"matchReward\":0,\"matchCompletionReward\":0,\"idolsDestroyedReward\":0,\"totalReward\":0}}}],\"msg\":\"NewEffects\"}");
this.comm.removeListener(this);
if (this.settings.alwayssave)
{
File.WriteAllLines(saveFolder + Path.DirectorySeparatorChar + gameID + ".sgr", messages.ToArray());
uiClass.parseRecord(saveFolder + Path.DirectorySeparatorChar + gameID + ".sgr");
Console.WriteLine("Save Recorded Game");
}
else
{
App.Popups.ShowOkCancel(this, "savereplay", "Save", "Want to save the recorded game?", "Ok", "No");
}
}
public void setBm(BattleMode b)
{
this.bttlmd = b;
}
public void setBmUI(BattleModeUI bui)
{
this.bttlmdUI = bui;
}
public void PopupOk(string s)
{
File.WriteAllLines(saveFolder + Path.DirectorySeparatorChar + gameID + ".sgr", messages.ToArray());
uiClass.parseRecord(saveFolder + Path.DirectorySeparatorChar + gameID + ".sgr");
Console.WriteLine("Save Recorded Game");
}
public void PopupCancel(string s)
{
}
}
}