-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitchPredictionsBG.cs
executable file
·351 lines (288 loc) · 10.8 KB
/
TwitchPredictionsBG.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using HearthDb.Enums;
using Hearthstone_Deck_Tracker.API;
using Hearthstone_Deck_Tracker.Enums;
using Hearthstone_Deck_Tracker.Hearthstone.Entities;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using TwitchPredictionsBG.PredictionStrategy;
namespace TwitchPredictionsBG
{
public class TwitchPredictionsBG
{
// We can store rating start, rating current, rating high for display on stream
// Maybe also game stats, like
// 8. Bulnik, tvari BloodTrail
// 1. Siski, murloki Kappa
private static PropertyFileWriter _ratingWriter = new PropertyFileWriter("rating");
private static PropertyFileWriter _ratingStartWriter = new PropertyFileWriter("ratingStart");
private static PropertyFileWriter _ratingHighWriter = new PropertyFileWriter("ratingHigh");
private static StringPropertyFileWriter _turnWriter = new StringPropertyFileWriter("turn");
private static Config config;
private static Version version;
public static bool isTokenLoaded = false;
public static bool isActivated = false;
private static TwitchApiAdapter apiAdapter;
private static IPredictionOutcomeStrategy predictionStrategy;
private static int _rating
{
get => _ratingWriter.load();
set
{
_ratingWriter.update(value);
}
}
private static int _ratingStart
{
get => _ratingStartWriter.load();
set
{
_ratingStartWriter.update(value);
}
}
private static int _ratingHigh
{
get => _ratingHighWriter.load();
set
{
_ratingHighWriter.update(value);
}
}
private static int _roundCounter = 0;
private static int _lastRoundNr = 0;
private static bool _isStart = true;
public static void OnLoad(Config _config, TwitchApiAdapter _apiAdapter, Version version)
{
config = _config;
apiAdapter = _apiAdapter;
switch (config.predictionStrategy)
{
case "PredictionOutcomeStrategy13_BLUE_57_PINK_48_RETURN":
predictionStrategy = new PredictionOutcomeStrategy13_BLUE_57_PINK_48_RETURN(apiAdapter);
break;
case "ConfigurablePredictionOutcomeStrategy":
predictionStrategy = new ConfigurablePredictionOutcomeStrategy(apiAdapter);
break;
case "PredictionOutcomeStrategy13_BLUE_47_PINK_8_RETURN":
default:
predictionStrategy = new PredictionOutcomeStrategy13_BLUE_47_PINK_8_RETURN(apiAdapter);
break;
}
TwitchPredictionsBG.version = version;
}
// On Game Start Event We Should Start Prediction on Twitch via API Call
// We Should Respect Stream Delay, via Task Delay for example
// We Can Get Stream Delay From OBS, or Just Put Variable in Settings
internal static void GameStart()
{
if (!isActivated)
{
return;
}
if (Core.Game.Spectator)
{
return;
}
if (!InBgMode())
{
return;
}
if (!isTokenLoaded)
{
return;
}
new Thread(async () =>
{
await Task.Delay(config.delay * 1000);
if (await apiAdapter.IsCurrentUserStreamLive())
{
await predictionStrategy.PublishPrediction();
}
}).Start();
}
internal static void TurnStart(ActivePlayer player)
{
if (!isActivated)
{
return;
}
if (Core.Game.Spectator)
{
return;
}
if (!InBgMode())
{
return;
}
var turn = Core.Game.GetTurnNumber();
_turnWriter.update(turn.ToString());
if (isTokenLoaded && turn > 1)
{
var livingHeroes = Core.Game.Entities.Values
.Where(x => x.IsHero && x.Health > 0 && !x.IsInZone(Zone.REMOVEDFROMGAME) && x.HasTag(GameTag.PLAYER_TECH_LEVEL) && (x.IsControlledBy(Core.Game.Player.Id) || !x.IsInPlay));
int positionAtLeast = livingHeroes.Count();
if (positionAtLeast != 0)
{
new Thread(async () =>
{
await Task.Delay(config.delay * 1000);
if (await apiAdapter.IsCurrentUserStreamLive())
{
await predictionStrategy.OnGameProgress(positionAtLeast);
}
}).Start();
}
}
}
internal static void CreateWriter(string name, ref PropertyFileWriter propertyFileWriter)
{
propertyFileWriter = new PropertyFileWriter(name);
}
static bool isCustomRatingSet = false;
internal static void Update()
{
if (!isActivated)
{
return;
}
if (Core.Game.Spectator)
{
return;
}
var coreGame = Core.Game;
string gameRegion;
switch (Core.Game.CurrentRegion)
{
case Region.ASIA:
gameRegion = "Asia";
break;
case Region.CHINA:
gameRegion = "China";
break;
case Region.EU:
gameRegion = "EU";
break;
case Region.UNKNOWN:
gameRegion = "Unk";
break;
case Region.US:
gameRegion = "US";
break;
default:
gameRegion = "";
break;
}
if (gameRegion != "" && gameRegion != "Unk" && config.hsRegion != gameRegion)
{
config.hsRegion = gameRegion;
config.save();
}
// If we could get player name from game, we should store rating to it
// If not, we should store to default path and after we can resolve player name
// We should restore to it
var gamePlayerName = Core.Game.Player.Name;
var playerName = gamePlayerName == "" ? config.hsUserName : gamePlayerName;
if (!String.IsNullOrEmpty(playerName) && !isCustomRatingSet)
{
if (gamePlayerName != "" && gamePlayerName != config.hsUserName)
{
config.hsUserName = gamePlayerName;
config.save();
}
// Config switch -> Save MMR to new Config
int _tmpRating = 0;
int _tmpRatingStart = 0;
// If we cant get there until is start, it means that current rating and start is set before config switch
// So we should backup current rating and start before new rating writer
if (!_isStart)
{
_tmpRating = _rating;
_tmpRatingStart = _ratingStart;
}
CreateWriter($"rating_{playerName}_{config.hsRegion}", ref _ratingWriter);
CreateWriter($"ratingStart_{playerName}_{config.hsRegion}", ref _ratingStartWriter);
CreateWriter($"ratingHigh_{playerName}_{config.hsRegion}", ref _ratingHighWriter);
if (!_isStart)
{
_rating = _tmpRating;
_ratingStart = _tmpRatingStart;
// Highest rating if unknown is equal to current
if (_ratingHigh == 0)
{
_ratingHigh = _rating;
}
}
isCustomRatingSet = true;
}
if (!InBgMenu())
{
return;
}
if (_isStart)
{
_ratingStart = Core.Game.BattlegroundsRatingInfo.Rating;
_rating = _ratingStart;
if (_ratingHigh == 0)
{
_ratingHigh = _ratingStart;
_rating = _ratingStart;
}
_isStart = false;
}
if (_lastRoundNr > _roundCounter)
{
_roundCounter = _lastRoundNr;
int latestRating = Core.Game.BattlegroundsRatingInfo.Rating;
int mmrChange = latestRating - _ratingStart;
_rating = latestRating;
if (_rating > _ratingHigh)
{
_ratingHigh = _rating;
}
}
}
internal static bool InBgMenu()
{
return Core.Game.CurrentMode == Hearthstone_Deck_Tracker.Enums.Hearthstone.Mode.BACON;
}
internal static bool InBgMode()
{
if (Core.Game.CurrentGameMode != GameMode.Battlegrounds) return false;
else return true;
}
internal static void InMenu()
{
}
internal static void GameEnd()
{
if (!isActivated)
{
return;
}
if (Core.Game.Spectator)
{
return;
}
_lastRoundNr++;
Entity hero = Core.Game.Entities.Values
.Where(x => x.IsHero && x.GetTag(GameTag.PLAYER_ID) == Core.Game.Player.Id)
.First();
int endGamePosition = hero.GetTag(GameTag.PLAYER_LEADERBOARD_PLACE);
string heroName = hero.Card.LocalizedName;
string heroId = hero.Card.Id;
if (!isTokenLoaded)
{
return;
}
new Thread(async () =>
{
await Task.Delay(config.delay * 1000);
if (await apiAdapter.IsCurrentUserStreamLive())
{
await predictionStrategy.OnGameEnd(endGamePosition);
}
}).Start();
}
}
}