-
Notifications
You must be signed in to change notification settings - Fork 71
/
Plugin.cs
2059 lines (1776 loc) · 70.2 KB
/
Plugin.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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.IO;
using System.Windows.Media.Imaging;
using System.Reflection;
using Hearthstone_Deck_Tracker;
using Hearthstone_Deck_Tracker.Plugins;
using Hearthstone_Deck_Tracker.Hearthstone;
using System.Diagnostics;
using System.Threading;
using Newtonsoft.Json;
using System.Runtime.InteropServices;
using MahApps.Metro.Controls.Dialogs;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Converters;
using Hearthstone_Deck_Tracker.Utility;
using Hearthstone_Deck_Tracker.Utility.Logging;
using HearthMirror;
namespace ArenaHelper
{
public class Plugin: IPlugin
{
public class ConfigData
{
public int windowx;
public int windowy;
public bool manualclicks;
public bool overlay;
public bool debug;
public bool autosave;
public bool updated;
public ConfigData()
{
ResetWindow();
manualclicks = false;
overlay = true;
debug = false;
autosave = false;
updated = false;
}
public void ResetWindow()
{
windowx = 100;
windowy = 100;
}
}
public class CardTierInfo
{
public string id;
public string name;
public List<string> value;
public CardTierInfo(string id, string name, List<string> value)
{
this.id = id;
this.name = name;
this.value = value;
}
}
public class HeroData
{
public int index;
public string id;
public string name;
public string image;
public HeroData(int index, string id, string name, string image)
{
this.index = index;
this.id = id;
this.name = name;
this.image = image;
}
}
public class ArenaData
{
public string deckname;
public string deckguid;
public List<string> detectedheroes;
public string pickedhero;
public List<Tuple<string, string, string>> detectedcards;
public List<Tuple<double, double, double>> cardrating;
public List<string> pickedcards;
public ArenaData()
{
deckname = "";
deckguid = "";
detectedheroes = new List<string>();
pickedhero = "";
detectedcards = new List<Tuple<string, string, string>>();
cardrating = new List<Tuple<double, double, double>>();
pickedcards = new List<string>();
}
}
public enum PluginState { Idle, SearchHeroes, SearchBigHero, DetectedHeroes, SearchCards, SearchCardValues, DetectedCards, Done };
private List<double> currentcardvalues = new List<double>();
private static PluginState state;
private List<int> mouseindex = new List<int>();
private ArenaData arenadata = new ArenaData();
private string currentfilename = "";
private ConfigData configdata = new ConfigData();
private bool configinit = false;
private List<Controls.ValueOverlay> valueoverlays = new List<Controls.ValueOverlay>();
private Controls.AdviceOverlay adviceoverlay = null;
private Update.AHDataVersion dataversion;
private const string DataVersionFile = "version.json";
private const string TierListFile = "cardtier.json";
public static List<HeroData> herolist = new List<HeroData>();
public static List<CardTierInfo> cardtierlist = new List<CardTierInfo>();
private Detection detection = new Detection();
// Arena detection
bool inarena = false;
bool stablearena = false;
Stopwatch arenastopwatch;
// Configure heroes
bool configurehero = false;
// Log reader
private DateTime loglasttime = DateTime.MinValue;
private string loglastline = "";
private DateTime loglastchoice = DateTime.MinValue;
private string loglastheroid = "";
private string loglastcardid = "";
//"DraftManager.OnChosen(): hero=HERO_03 premium=STANDARD"
public static readonly Regex HeroChosenRegex = new Regex(@"DraftManager\.OnChosen\(\): hero=(?<id>(.+)) .*");
private List<Card> previouscards = new List<Card>();
private bool samecarddelay;
private DateTime samecardtime = DateTime.MinValue;
private const int samecardmaxtime = 4000;
// Updates
private DateTime lastpluginupdatecheck = DateTime.MinValue;
private Update.GithubRelease latestrelease = null;
private bool haspluginupdates = false;
private TimeSpan pluginupdatecheckinterval = TimeSpan.FromHours(1);
private bool showingupdatemessage = false;
private DateTime lastdataupdatecheck = DateTime.MinValue;
private bool hasdataupdates = false;
private TimeSpan dataupdatecheckinterval = TimeSpan.FromHours(1);
Stopwatch stopwatch;
protected MenuItem MainMenuItem { get; set; }
internal static ArenaWindow arenawindow = null;
private SemaphoreSlim mutex = new SemaphoreSlim(1);
private const int ArenaDetectionTime = 750;
private const int MaxCardCount = 30;
private const string DetectingArena = "Detecting arena...";
private const string DetectingHeroes = "Detecting heroes...";
private const string DetectingCards = "Detecting cards...";
private const string DetectingValues = "Getting values...";
private const string DetectionWarning = "Wait for detection. Don't make a selection yet!";
private const string DoneMessage = "All cards are picked. You can start a new arena run or save the deck.";
private const string ConfigFile = "arenahelper.json";
private Plugins plugins = new Plugins();
private string DataDir
{
get { return Path.Combine(Config.Instance.DataDir, "ArenaHelper"); }
}
private string DataDataDir
{
get { return Path.Combine(Config.Instance.DataDir, "ArenaHelper", "Data"); }
}
private string DeckDataDir
{
get { return Path.Combine(Config.Instance.DataDir, "ArenaHelper", "Decks"); }
}
public string Name
{
get { return "Arena Helper"; }
}
public string Description
{
get { return "Arena Helper is a plugin for Hearthstone Deck Tracker that tries to detect heroes and cards when drafting a Hearthstone arena deck. Detected cards are displayed alongside the value of the card, that is specified in the Arena Tier List. The created deck can be saved to Hearthstone Deck Tracker.\n\nFor more information and updates, check out:\nhttps://github.com/rembound/Arena-Helper"; }
}
public string ButtonText
{
get { return "Arena Helper"; }
}
public string Author
{
get { return "Rembound"; }
}
public Version Version
{
get { return new Version("0.9.6"); }
}
public MenuItem MenuItem
{
get { return MainMenuItem; }
}
public void OnLoad()
{
try
{
// Load plugins
plugins.LoadPlugins();
state = PluginState.Idle;
// Set hero list
herolist.Clear();
herolist.Add(new HeroData(0, "Warrior", "Warrior", "warrior.png"));
herolist.Add(new HeroData(1, "Shaman", "Shaman", "shaman.png"));
herolist.Add(new HeroData(2, "Rogue", "Rogue", "rogue.png"));
herolist.Add(new HeroData(3, "Paladin", "Paladin", "paladin.png"));
herolist.Add(new HeroData(4, "Hunter", "Hunter", "hunter.png"));
herolist.Add(new HeroData(5, "Druid", "Druid", "druid.png"));
herolist.Add(new HeroData(6, "Warlock", "Warlock", "warlock.png"));
herolist.Add(new HeroData(7, "Mage", "Mage", "mage.png"));
herolist.Add(new HeroData(8, "Priest", "Priest", "priest.png"));
herolist.Add(new HeroData(9, "DemonHunter", "Demon Hunter", "demon-hunter.png"));
AddMenuItem();
stopwatch = Stopwatch.StartNew();
// Load data
LoadData();
// Add log events
Hearthstone_Deck_Tracker.API.LogEvents.OnArenaLogLine.Add(OnArenaLogLine);
}
catch (Exception e)
{
string errormsg = "OnLoad Error: " + e.Message + "\n" + e.ToString();
Log.Info(errormsg);
}
}
private void AddMenuItem()
{
MainMenuItem = new MenuItem()
{
Header = "Arena Helper"
};
MainMenuItem.Click += (sender, args) =>
{
ActivateArenaWindow();
};
}
private void ActivateArenaWindow()
{
try
{
if (arenawindow == null)
{
InitializeMainWindow();
// Show about page when auto updated
if (configdata.updated)
{
// Clean auto updater
Update.CleanAutoUpdate();
arenawindow.FlyoutAbout.IsOpen = true;
configdata.updated = false;
SaveConfig();
}
arenawindow.Show();
}
else
{
// Reset window position when reactivating
arenawindow.Left = 100;
arenawindow.Top = 100;
arenawindow.WindowState = System.Windows.WindowState.Normal;
arenawindow.Activate();
}
}
catch (Exception e)
{
string errormsg = "ActivateArenaWindow: " + e.Message + "\n" + e.ToString();
Log.Info(errormsg);
}
}
protected void InitializeMainWindow()
{
if (arenawindow == null)
{
arenawindow = new ArenaWindow();
arenawindow.StringVersion = "v" + Version.ToString();
// Load config
LoadConfig();
AddElements();
ApplyConfig();
arenawindow.Closed += (sender, args) =>
{
plugins.CloseArena(arenadata, state);
SaveConfig();
RemoveElements();
arenawindow = null;
configinit = false;
};
// Init
InitConfigureHero();
arenawindow.onbuttonnewarenaclick = new ArenaWindow.OnEvent(OnButtonNewArenaClick);
arenawindow.onbuttonsaveclick = new ArenaWindow.OnEvent(OnButtonSaveClick);
arenawindow.onwindowlocation = new ArenaWindow.OnEvent(OnWindowLocation);
arenawindow.onheroclick = new ArenaWindow.OnOverrideClick(OnHeroClick);
arenawindow.oncardclick = new ArenaWindow.OnOverrideClick(OnCardClick);
arenawindow.onconfigurehero = new ArenaWindow.OnEvent(OnConfigureHero);
arenawindow.oncheroclick = new ArenaWindow.OnOverrideClick(OnCHeroClick);
arenawindow.oncheckboxoverlay = new ArenaWindow.OnCheckbox(OnCheckboxOverlay);
arenawindow.oncheckboxmanual = new ArenaWindow.OnCheckbox(OnCheckboxManual);
arenawindow.oncheckboxautosave = new ArenaWindow.OnCheckbox(OnCheckboxAutoSave);
arenawindow.onupdatedownloadclick = new ArenaWindow.OnEvent(OnUpdateDownloadClick);
// Get the latest arena data
string newestfilename = "";
if (Directory.Exists(DeckDataDir))
{
var newest = Directory.GetFiles(DeckDataDir).Select(x => new FileInfo(x)).OrderByDescending(x => x.CreationTime).FirstOrDefault();
if (newest != null)
{
newestfilename = newest.FullName;
}
}
LoadArenaData(newestfilename);
}
}
private void InitConfigureHero()
{
configurehero = false;
SetHeroControl(arenawindow.CHero0, herolist[0].id);
SetHeroControl(arenawindow.CHero1, herolist[1].id);
SetHeroControl(arenawindow.CHero2, herolist[2].id);
SetHeroControl(arenawindow.CHero3, herolist[3].id);
SetHeroControl(arenawindow.CHero4, herolist[4].id);
SetHeroControl(arenawindow.CHero5, herolist[5].id);
SetHeroControl(arenawindow.CHero6, herolist[6].id);
SetHeroControl(arenawindow.CHero7, herolist[7].id);
SetHeroControl(arenawindow.CHero8, herolist[8].id);
SetHeroControl(arenawindow.CHero9, herolist[9].id);
arenawindow.CHero10.HeroName.Text = "Cancel";
arenawindow.Update();
}
private void LoadConfig()
{
string filename = Path.Combine(DataDir, ConfigFile);
if (File.Exists(filename))
{
try
{
// Load the data
ConfigData loadedconfigdata = JsonConvert.DeserializeObject<ConfigData>(File.ReadAllText(filename));
if (loadedconfigdata != null)
{
// Set data
configdata = loadedconfigdata;
// Fix window position for legacy configs
if (!(configdata.windowx > -32000 && configdata.windowy > -32000))
{
configdata.ResetWindow();
}
// Set window position
arenawindow.Left = configdata.windowx;
arenawindow.Top = configdata.windowy;
}
else
{
Log.Info("Arena Helper: Error loading config, null");
}
}
catch (Exception e)
{
Log.Info("Arena Helper: Error loading config");
}
}
// Set options
arenawindow.CheckBoxOverlay.IsChecked = configdata.overlay;
arenawindow.CheckBoxManual.IsChecked = configdata.manualclicks;
arenawindow.CheckBoxAutoSave.IsChecked = configdata.autosave;
configinit = true;
}
private void ApplyConfig()
{
}
private void ShowOverlay(bool show)
{
ShowValueOverlay(show);
ShowAdviceOverlay(show);
}
private void ShowValueOverlay(bool show)
{
System.Windows.Visibility vis = System.Windows.Visibility.Hidden;
if (show)
{
vis = System.Windows.Visibility.Visible;
}
foreach (var overlay in valueoverlays)
{
overlay.Visibility = vis;
}
}
private void ShowAdviceOverlay(bool show)
{
System.Windows.Visibility vis = System.Windows.Visibility.Hidden;
if (show)
{
vis = System.Windows.Visibility.Visible;
}
adviceoverlay.Visibility = vis;
}
private void SaveConfig()
{
string filename = Path.Combine(DataDir, ConfigFile);
if (!Directory.Exists(DataDir))
Directory.CreateDirectory(DataDir);
string json = JsonConvert.SerializeObject(configdata, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(filename, json);
}
public void OnButtonNewArenaClick()
{
NewArena();
SaveArenaData();
plugins.NewArena(arenadata);
}
public void OnButtonSaveClick()
{
SaveDeck(false);
}
public void OnHeroClick(int index)
{
// Override detection
if (configdata.manualclicks)
{
}
}
public void OnCardClick(int index)
{
// Override detection
if (!configdata.manualclicks)
{
return;
}
if (state == PluginState.DetectedCards)
{
// Manually pick a card
PickCard(index);
}
}
public void OnConfigureHero()
{
// Override hero detection
configurehero = !configurehero;
SetState(state);
}
// Configure hero click
public void OnCHeroClick(int index)
{
configurehero = false;
if (index >= 0 && index < herolist.Count)
{
PickHero(index);
}
else
{
SetState(state);
}
}
public void OnCheckboxOverlay(bool check)
{
if (configinit)
{
configdata.overlay = check;
SaveConfig();
ApplyConfig();
SetState(state); // Set state to update overlay
}
}
public void OnCheckboxManual(bool check)
{
if (configinit)
{
configdata.manualclicks = check;
SaveConfig();
ApplyConfig();
}
}
public void OnCheckboxAutoSave(bool check)
{
if (configinit)
{
configdata.autosave = check;
SaveConfig();
ApplyConfig();
}
}
public void OnWindowLocation()
{
// Set window location if not minimized
if (arenawindow.Left > -32000 && arenawindow.Top > -32000)
{
configdata.windowx = (int)arenawindow.Left;
configdata.windowy = (int)arenawindow.Top;
}
}
// Auto update
public async void OnUpdateDownloadClick()
{
Log.Info("Auto Updating Arena Helper");
if (latestrelease != null)
{
arenawindow.UpdateText.Text = "Updating Arena Helper, please wait...";
arenawindow.UpdateButtons.Visibility = System.Windows.Visibility.Hidden;
configdata.updated = true;
SaveConfig();
// Update
bool status = await Update.AutoUpdate(latestrelease);
if (status)
{
// Start the new process
Process.Start(System.Windows.Application.ResourceAssembly.Location);
// Shutdown the old process
Hearthstone_Deck_Tracker.API.Core.MainWindow.Close();
System.Windows.Application.Current.Shutdown();
}
else
{
arenawindow.UpdateText.Text = "There was a problem while auto-updating. Press \"website\" to visit the project page and update manually.";
arenawindow.UpdateDownload.IsEnabled = false;
arenawindow.UpdateButtons.Visibility = System.Windows.Visibility.Visible;
}
}
}
private void SaveDeck(bool autosave)
{
// Save deck
Deck deck = new Deck();
deck.IsArenaDeck = true;
if (autosave)
{
deck.DeckId = new Guid(arenadata.deckguid);
}
foreach (var cardid in arenadata.pickedcards)
{
Card pickedcard = GetCard(cardid);
if (pickedcard == null)
continue;
Card card = (Card)pickedcard.Clone();
card.Count = 1;
// Find out hero
if (string.IsNullOrEmpty(deck.Class) && card.PlayerClass != "Neutral")
deck.Class = card.PlayerClass;
if (deck.Cards.Contains(card))
{
// Increase count
var deckCard = deck.Cards.First(c => c.Equals(card));
deck.Cards.Remove(deckCard);
deckCard.Count++;
deck.Cards.Add(deckCard);
}
else
{
deck.Cards.Add(card);
}
}
// Add tag
deck.Tags.Add("Arena");
// Deck name based on class
deck.Name = Helper.ParseDeckNameTemplate(Config.Instance.ArenaDeckNameTemplate, deck);
if (!autosave)
{
// Set the new deck
Hearthstone_Deck_Tracker.API.Core.MainWindow.ShowDeckEditorFlyout(deck, false);
// Activate the window
Hearthstone_Deck_Tracker.API.Core.MainWindow.ActivateWindow();
}
else
{
// Save the deck
Hearthstone_Deck_Tracker.DeckManager.SaveDeck(deck, deck, true);
// Select the deck and make it active
Hearthstone_Deck_Tracker.API.Core.MainWindow.SelectDeck(null, true);
Hearthstone_Deck_Tracker.API.Core.MainWindow.SelectDeck(deck, true);
}
}
private void SaveArenaData()
{
if (!Directory.Exists(DeckDataDir))
Directory.CreateDirectory(DeckDataDir);
if (currentfilename == "")
{
currentfilename = CreateDeckFilename();
}
string json = JsonConvert.SerializeObject(arenadata, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(currentfilename, json);
}
private string CreateDeckFilename()
{
string filename = string.Format("Arena-{0}", DateTime.Now.ToString("yyyyMMdd-HHmm"));
return Helper.GetValidFilePath(DeckDataDir, filename, ".json");
}
private void LoadArenaData(string filename)
{
// Init state
SetState(PluginState.Idle);
NewArena();
bool validarenadata = false;
ArenaData loadedarenadata = null;
if (File.Exists(filename))
{
try
{
Log.Info("Arena Helper: loading data");
// Load the data
loadedarenadata = JsonConvert.DeserializeObject<ArenaData>(File.ReadAllText(filename));
if (loadedarenadata != null)
{
// Set the data
arenadata = loadedarenadata;
validarenadata = true;
Log.Info("Arena Helper: Card Rating: " + arenadata.cardrating.Count);
}
else
{
Log.Info("Arena Helper: Error loading arena data, null");
}
}
catch (Exception e)
{
Log.Info("Arena Helper: Error loading arena data");
}
}
if (validarenadata)
{
// Set current filename
currentfilename = filename;
// Make sure there is a guid for legacy arena runs
if (arenadata.deckguid == "")
{
arenadata.deckguid = Guid.NewGuid().ToString();
SaveArenaData();
}
if (arenadata.pickedhero != "")
{
// Hero is picked
if (arenadata.pickedcards.Count == MaxCardCount)
{
// All cards picked
SetState(PluginState.Done);
}
else if ((arenadata.detectedcards.Count - 1) == arenadata.pickedcards.Count)
{
// Cards detected, but not picked
UpdateDetectedCards();
SetState(PluginState.SearchCardValues);
}
else
{
// Not all cards picked, not picking a card
// Search for new cards
SetState(PluginState.SearchCards);
}
}
else
{
// No hero picked
if (arenadata.detectedheroes.Count == 3)
{
// Heroes detected
// Show the heroes
UpdateDetectedHeroes();
SetState(PluginState.SearchBigHero);
}
else
{
// No heroes detected
SetState(PluginState.SearchHeroes);
}
}
// Resume arena
plugins.ResumeArena(arenadata, state);
UpdateTitle();
UpdateHero();
}
else
{
// No arena found, started a new one
// Save the arena data
SaveArenaData();
plugins.NewArena(arenadata);
}
}
private void NewArena()
{
// Initialize variables
currentfilename = "";
loglasttime = DateTime.MinValue;
loglastline = "";
loglastchoice = DateTime.MinValue;
loglastheroid = "";
loglastcardid = "";
previouscards.Clear();
samecarddelay = false;
arenawindow.Hero0.HeroImage.Source = null;
arenawindow.Hero1.HeroImage.Source = null;
arenawindow.Hero2.HeroImage.Source = null;
arenawindow.Card0 = null;
arenawindow.Card1 = null;
arenawindow.Card2 = null;
arenawindow.Update();
// Clear data
arenadata.deckname = Helper.ParseDeckNameTemplate(Config.Instance.ArenaDeckNameTemplate);
arenadata.deckguid = Guid.NewGuid().ToString();
arenadata.detectedheroes.Clear();
arenadata.pickedhero = "";
arenadata.detectedcards.Clear();
arenadata.cardrating.Clear();
arenadata.pickedcards.Clear();
// Invalidate arena
inarena = false;
stablearena = false;
// Init state
SetState(PluginState.SearchHeroes);
UpdateTitle();
UpdateHero();
ResetHeroSize();
}
public void OnUnload()
{
//RemoveElements();
if (arenawindow != null)
{
if (arenawindow.IsVisible)
{
arenawindow.Close();
}
arenawindow = null;
}
}
public void OnButtonPress()
{
ActivateArenaWindow();
}
public async void OnUpdate()
{
try
{
// Check for plugin updates
// CheckUpdate();
}
catch (Exception e)
{
string errormsg = "CheckUpdate: " + e.Message + "\n" + e.ToString();
Log.Info(errormsg);
}
await mutex.WaitAsync();
try
{
if (arenawindow != null && state != PluginState.Done)
{
stopwatch.Restart();
// Size updates
UpdateSize();
await Detect();
}
}
catch (Exception e)
{
string errormsg = "OnUpdate Error: " + e.Message + "\n" + e.ToString();
Log.Info(errormsg);
}
finally
{
mutex.Release();
}
}
public void OnArenaLogLine(string logline)
{
if (arenawindow == null)
return;
try
{
//Log.Info("AH LogLine: " + logline);
// Only process new lines
DateTime loglinetime;
if (logline.Length > 20 && DateTime.TryParse(logline.Substring(2, 16), out loglinetime))
{
if (loglinetime > DateTime.Now)
{
loglinetime = loglinetime.AddDays(-1);
}
if (loglinetime < loglasttime || (loglinetime == loglasttime && logline == loglastline))
{
// Skip old lines and the previous line
// Lines with the same timestamp could be needed, if it is not the same as the previous
return;
}
//Logger.WriteLine("AH LogLine process");
// Set new time
loglasttime = loglinetime;
loglastline = logline;
}
else
{
return;
}
// Modified from ArenaHandler.cs
var heromatch = HeroChosenRegex.Match(logline);
var match = Hearthstone_Deck_Tracker.LogReader.LogConstants.NewChoiceRegex.Match(logline);
if (heromatch.Success)
{
// Hero chosen
string heroid = Database.GetHeroNameFromId(heromatch.Groups["id"].Value, false);
if (heroid != null)
{
HeroData hero = GetHero(heroid);
if (hero != null)
{
// Hero choice detection, final
Log.Info("AH Hero chosen: " + heroid);
PickHero(hero.index);
}
}
}
else if (match.Success)
{
string heroid = Database.GetHeroNameFromId(match.Groups["id"].Value, false);
if (heroid != null)
{
if (GetHero(heroid) != null)
{
// Hero choice detection, not final
Log.Info("AH Hero choice: " + heroid);
loglastheroid = heroid;
}
}
else
{
// Card choice detection
var cardid = match.Groups["id"].Value;
var dtime = DateTime.Now.Subtract(loglastchoice).TotalMilliseconds;
// This should not be necessary, but HDT does it
if (loglastcardid == cardid && dtime < 1000)
{
Log.Info(string.Format("AH Card with the same ID ({0}) was chosen less {1} ms ago. Ignoring.", cardid, dtime));
return;
}
Log.Info("AH Card choice: " + cardid);
loglastchoice = DateTime.Now;
loglastcardid = cardid;
}
}
}
catch (Exception e)
{
string errormsg = "OnArenaLogLine: " + e.Message + "\n" + e.ToString();
Log.Info(errormsg);
}
}
private void UpdateSize()
{
var hsrect = Helper.GetHearthstoneRect(true);
if (hsrect.Width <= 0 || hsrect.Height <= 0)
{
return;
}
// Position card values
for (int i = 0; i < valueoverlays.Count; i++)
{
Point cardpos = Detection.GetHSPos(hsrect, i * Detection.cardwidth + Detection.cardrect.X, Detection.cardrect.Y, Detection.scalewidth, Detection.scaleheight);
Point cardsize = Detection.GetHSSize(hsrect, Detection.cardrect.Width, Detection.cardrect.Height - 8, Detection.scalewidth, Detection.scaleheight);
Canvas.SetLeft(valueoverlays[i], cardpos.X + cardsize.X / 2 - valueoverlays[i].RenderSize.Width/2);