-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardPackOpeningCounter.cs
551 lines (497 loc) · 17.8 KB
/
CardPackOpeningCounter.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
using Hearthstone_Deck_Tracker.LogReader;
using Hearthstone_Deck_Tracker.LogReader.Interfaces;
using Hearthstone_Deck_Tracker.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using Hearthstone_Deck_Tracker.Enums.Hearthstone;
using Hearthstone_Deck_Tracker.API;
using MahApps.Metro.Controls;
using Hearthstone_Deck_Tracker.Plugins;
using System.Diagnostics;
using HDT_CardPackOpeningCounter.HSData;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using static HDT_CardPackOpeningCounter.HSData.CardDatabase;
namespace HDT_CardPackOpeningCounter
{
public class CardPackOpeningCounter : IPlugin
{
public static int common = 0;
public static int rare = 0;
public static int epic = 0;
public static int legendary = 0;
public static int goldenCommon = 0;
public static int goldenRare = 0;
public static int goldenEpic = 0;
public static int goldenLegendary = 0;
private BackgroundWorker worker;
private string[] previousLog = new string[] { };
private static List<string> newCards = new List<string>();
private static List<CardCollectionModification> ccmQueue = new List<CardCollectionModification>();
private String hearthStoneDirectory;
public string Name => "Card Pack Opener Counter";
public string Description => "A HDT Plugin that assists you when your opening your glorious packs \nby counting the received cards and displaying the amount\nbased on rarity and quality (normal & golden).";
public string ButtonText => "Open the Card Pack Opener Counter";
public string Author => "herby2212";
public Version Version => new Version(1, 2, 0);
protected int i = 0;
protected static MainWindow MainWindow;
protected MenuItem MainMenuItem;
public void OnButtonPress()
{
initializeMainWindow();
MainWindow.Show();
}
public void OnLoad()
{
CardDatabase.loadHSData();
worker = new BackgroundWorker();
initializeMainWindow();
initializeMainMenuItem();
}
private void initializeMainWindow()
{
if(MainWindow == null)
{
MainWindow = new MainWindow(this, common, rare, epic, legendary, goldenCommon, goldenRare, goldenEpic, goldenLegendary);
MainWindow.Closed += mainWindowClosedEventHandler;
}
}
private void initializeMainMenuItem()
{
if(MainMenuItem == null)
{
MainMenuItem = new MainMenuItem();
MainMenuItem.Click += mainMenuItemClickEventHandler;
}
}
private void mainMenuItemClickEventHandler(object sender, EventArgs e)
{
OnButtonPress();
}
private void mainWindowClosedEventHandler(object sender, EventArgs e)
{
MainWindow.Closed -= mainWindowClosedEventHandler;
MainWindow = null;
}
public void OnUnload()
{
if(MainWindow != null)
{
MainWindow.Close();
}
}
public void OnUpdate()
{
if(!checkIfHearthstoneIsRunning())
{
return;
}
if(hearthStoneDirectory == null)
{
initializeHearthStonePath();
}
if (checkfInPackOpening())
{
setupLogFileAnalyzer();
updateCardCountWindows();
}
}
private void setupLogFileAnalyzer()
{
String netLogFilePath = this.hearthStoneDirectory + @"\Logs\Net.log";
this.analyzeLogFile(netLogFilePath);
}
private void analyzeLogFile(String netLogFilePath)
{
FileStream stream = File.Open(netLogFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader reader = new StreamReader(stream);
string test = "";
List<String> logList = new List<String>();
List<string> logNewRead = new List<string>();
while (!reader.EndOfStream)
{
string text = reader.ReadLine();
logNewRead.Add(text);
}
string[] logNew = logNewRead.ToArray();
if (logNew.Length == previousLog.Length)
{
return;
}
test += "longer then previous reading out data... (" + logNew.Length + " - " + previousLog.Length + ")\n";
List<string> newDataRead = new List<string>();
int i = 0;
foreach (string s in logNew)
{
if (i + 1 >= previousLog.Length)
{
newDataRead.Add(s);
test += s + "\n";
}
else
{
if (!s.Equals(previousLog[i]))
{
newDataRead.Add(s);
test += s + "\n";
}
}
i++;
}
string[] newData = newDataRead.ToArray();
test += "reading data completed. NewData size: " + newData.Length + ". Setting logNew to previousLog.";
previousLog = logNew;
newCards.Clear();
bool _inOpening = false;
string packOpeningCards = "";
foreach (string s in newData)
{
if (s.Contains("Network.OpenBooster")) {
_inOpening = true;
packOpeningCards += s;
} else if(s.Contains("NetCacheBoosters") && _inOpening)
{
packOpeningCards += s;
CardPackOpeningCounter.newCards.Add(packOpeningCards);
packOpeningCards = "";
_inOpening = false;
} else if (_inOpening)
{
packOpeningCards += s;
}
}
worker.DoWork -= new DoWorkEventHandler(CardAddHandler);
worker.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(CardAddCompleted);
worker.DoWork += new DoWorkEventHandler(CardAddHandler);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CardAddCompleted);
worker.WorkerSupportsCancellation = true;
worker.RunWorkerAsync();
}
private void CardAddCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Check for outstanding card additions
if(ccmQueue.Count > 0)
{
foreach (CardCollectionModification ccm in ccmQueue)
{
addCardBasedOnCardId(ccm.AssetCardId, ccm.Premium);
}
ccmQueue.Clear();
}
}
private void updateCardCountWindows()
{
if (!MainWindow.IsVisible)
{
return;
}
MainWindow.refresh(common, rare, epic, legendary);
MainWindow.refreshGolden(goldenCommon, goldenRare, goldenEpic, goldenLegendary);
DustCalculation();
}
private void CardAddHandler(object sender, DoWorkEventArgs e)
{
String openings = "";
foreach (string packOpening in newCards)
{
openings += "PACK START\n";
MatchCollection match = Regex.Matches(packOpening, @"{ (.+?)}");
foreach(Match m in match)
{
var asset = Regex.Matches(m.Value, @"\d+");
CardCollectionModification ccm = new CardCollectionModification(int.Parse(asset[0].Value), int.Parse(asset[1].Value), int.Parse(asset[2].Value), int.Parse(asset[3].Value));
ccmQueue.Add(ccm);
openings += m.Value + " - " + asset[0].Value + " - " + asset[1].Value + " - " + asset[2].Value + " - " + asset[3].Value + " - queueSize: " + ccmQueue.Count + "\n";
}
openings += "PACK END\n";
}
}
private void changeCardBasedOnRarity(RARITY rarity, int premium, CardCountModificationMode cardCountModificationMode)
{
bool golden = Convert.ToBoolean(premium);
if (rarity == RARITY.COMMON)
{
switch (cardCountModificationMode)
{
case CardCountModificationMode.ADD:
if (golden)
{
goldenCommon += 1;
}
else
{
common += 1;
}
break;
case CardCountModificationMode.REMOVE:
if (golden)
{
goldenCommon = goldenCommon == 0 ? 0 : goldenCommon -= 1;
}
else
{
common = common == 0 ? 0 : common -= 1;
}
break;
}
}
else if (rarity == RARITY.RARE)
{
switch (cardCountModificationMode)
{
case CardCountModificationMode.ADD:
if (golden)
{
goldenRare += 1;
}
else
{
rare += 1;
}
break;
case CardCountModificationMode.REMOVE:
if (golden)
{
goldenRare = goldenRare == 0 ? 0 : goldenRare -= 1;
}
else
{
rare = rare == 0 ? 0 : rare -= 1;
}
break;
}
}
else if (rarity == RARITY.EPIC)
{
switch (cardCountModificationMode)
{
case CardCountModificationMode.ADD:
if (golden)
{
goldenEpic += 1;
}
else
{
epic += 1;
}
break;
case CardCountModificationMode.REMOVE:
if (golden)
{
goldenEpic = goldenEpic == 0 ? 0 : goldenEpic -= 1;
}
else
{
epic = epic == 0 ? 0 : epic -= 1;
}
break;
}
}
else if (rarity == RARITY.LEGENDARY)
{
switch (cardCountModificationMode)
{
case CardCountModificationMode.ADD:
if (golden)
{
goldenLegendary += 1;
}
else
{
legendary += 1;
}
break;
case CardCountModificationMode.REMOVE:
if (golden)
{
goldenLegendary = goldenLegendary == 0 ? 0 : goldenLegendary -= 1;
}
else
{
legendary = legendary == 0 ? 0 : legendary -= 1;
}
break;
}
}
}
public void addCardBasedOnRarity(RARITY rarity, int premium)
{
changeCardBasedOnRarity(rarity, premium, CardCountModificationMode.ADD);
updateCardCountWindows();
}
public void removeCardBasedOnRarity(RARITY rarity, int premium)
{
changeCardBasedOnRarity(rarity, premium, CardCountModificationMode.REMOVE);
updateCardCountWindows();
}
private enum CardCountModificationMode
{
ADD,
REMOVE
}
private void addCardBasedOnCardId(int cardId, int premium)
{
RARITY rarity = CardDatabase.getRarity(cardId);
addCardBasedOnRarity(rarity, premium);
}
private void initializeHearthStonePath()
{
Process hearthstone = Process.GetProcessesByName("hearthstone")[0];
String path = hearthstone.MainModule.FileName;
String directory = path.Remove(path.LastIndexOf("\\"), path.Length - path.LastIndexOf("\\"));
hearthStoneDirectory = directory;
}
internal List<string> saveCounts() {
List<String> lines = new List<string>();
for (int i = 0; i < common; i++)
{
lines.Add("Card:Common");
}
for (int i = 0; i < rare; i++)
{
lines.Add("Card:Rare");
}
for (int i = 0; i < epic; i++)
{
lines.Add("Card:Epic");
}
for (int i = 0; i < legendary; i++)
{
lines.Add("Card:Legendary");
}
for (int i = 0; i < goldenCommon; i++)
{
lines.Add("Card:Golden:Common");
}
for (int i = 0; i < goldenRare; i++)
{
lines.Add("Card:Golden:Rare");
}
for (int i = 0; i < goldenEpic; i++)
{
lines.Add("Card:Golden:Epic");
}
for (int i = 0; i < goldenLegendary; i++)
{
lines.Add("Card:Golden:Legendary");
}
return lines;
}
internal void loadFile(string fileName)
{
resetCount();
String[] log = File.ReadAllLines(fileName);
foreach (string line in log)
{
if (line.Contains("Card:Common"))
{
common += 1;
}
else if (line.Contains("Card:Rare"))
{
rare += 1;
}
else if (line.Contains("Card:Epic"))
{
epic += 1;
}
else if (line.Contains("Card:Legendary"))
{
legendary += 1;
}
else if (line.Contains("Card:Golden:Common"))
{
goldenCommon += 1;
}
else if (line.Contains("Card:Golden:Rare"))
{
goldenRare += 1;
}
else if (line.Contains("Card:Golden:Epic"))
{
goldenEpic += 1;
}
else if (line.Contains("Card:Golden:Legendary"))
{
goldenLegendary += 1;
}
}
updateCardCountWindows();
}
public void resetCount()
{
common = 0;
rare = 0;
epic = 0;
legendary = 0;
goldenCommon = 0;
goldenRare = 0;
goldenEpic = 0;
goldenLegendary = 0;
updateCardCountWindows();
}
private void cardsTotal()
{
int cardsTotal = 0;
int cardPacks = 0;
cardsTotal = common + rare + epic + legendary
+ goldenCommon + goldenRare + goldenEpic + goldenLegendary;
cardPacks = cardsTotal / 5;
MainWindow.refreshPackValues(cardsTotal, cardPacks);
}
private void DustCalculation()
{
int craftingCost = 0;
int dustingValue = 0;
/*
*
* Crafting Cost
*
*/
craftingCost = (common * 40) + (rare * 100) + (epic * 400) + (legendary * 1600)
+ (goldenCommon * 400) + (goldenRare * 800) + (goldenEpic * 1600) + (goldenLegendary * 3200);
/*
*
* Dusting Value
*
*/
dustingValue = (common * 5) + (rare * 20) + (epic * 100) + (legendary * 400)
+ (goldenCommon * 50) + (goldenRare * 100) + (goldenEpic * 400) + (goldenLegendary * 1600);
MainWindow.refreshDustValues(craftingCost, dustingValue);
cardsTotal();
}
private bool checkIfHearthstoneIsRunning()
{
return Core.Game.IsRunning;
}
private Boolean checkfInPackOpening()
{
return Core.Game.CurrentMode == Mode.PACKOPENING;
}
public MenuItem MenuItem => MainMenuItem;
public static PluginSettings Settings { get; set; }
}
class CardCollectionModification
{
public CardCollectionModification(int amountSeen, int assetCardId, int premium, int quantity)
{
AmountSeen = amountSeen;
AssetCardId = assetCardId;
Premium = premium;
Quantity = quantity;
}
int AmountSeen { get; set; }
public int AssetCardId { get; set; }
//0 = Normal; 1 = Golden; 3 = Signature
public int Premium { get; set; }
int Quantity { get; set; }
}
}