forked from HearthSim/Hearthstone-Deck-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeckManager.cs
342 lines (329 loc) · 12.4 KB
/
DeckManager.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using HearthMirror.Objects;
using Hearthstone_Deck_Tracker.Enums;
using Hearthstone_Deck_Tracker.Hearthstone;
using Hearthstone_Deck_Tracker.Hearthstone.Entities;
using Hearthstone_Deck_Tracker.Importing;
using Hearthstone_Deck_Tracker.Importing.Game;
using Hearthstone_Deck_Tracker.Importing.Game.ImportOptions;
using Hearthstone_Deck_Tracker.Utility.Extensions;
using Hearthstone_Deck_Tracker.Utility.Logging;
using Hearthstone_Deck_Tracker.Windows;
using Card = Hearthstone_Deck_Tracker.Hearthstone.Card;
using Deck = Hearthstone_Deck_Tracker.Hearthstone.Deck;
namespace Hearthstone_Deck_Tracker
{
public class DeckManager
{
private static bool _waitingForClass;
private static bool _waitingForUserInput;
private static int _waitingForDraws;
private static int _autoSelectCount;
public static Guid IgnoredDeckId;
public static List<Card> NotFoundCards { get; set; } = new List<Card>();
internal static void ResetAutoSelectCount() => _autoSelectCount = 0;
public static async Task DetectCurrentDeck()
{
var deck = DeckList.Instance.ActiveDeck;
if(deck == null || deck.DeckId == IgnoredDeckId || _waitingForClass || _waitingForUserInput)
return;
if(string.IsNullOrEmpty(Core.Game.Player.Class))
{
_waitingForClass = true;
while(string.IsNullOrEmpty(Core.Game.Player.Class))
await Task.Delay(100);
_waitingForClass = false;
}
var cardEntites = Core.Game.Player.RevealedEntities.Where(x => (x.IsMinion || x.IsSpell || x.IsWeapon) && !x.Info.Created && !x.Info.Stolen && x.Card.Collectible).GroupBy(x => x.CardId).ToList();
var notFound = cardEntites.Where(x => !deck.GetSelectedDeckVersion().Cards.Any(c => c.Id == x.Key && c.Count >= x.Count())).ToList();
if(notFound.Any())
{
var activeVersion = deck.Version;
AutoImport(false);
if(activeVersion != deck.Version && cardEntites.All(ce => deck.GetSelectedDeckVersion().Cards.Any(c => c.Id == ce.Key && c.Count >= ce.Count())))
{
//if autoimport finds a new version of the selected deck, the new version will be set as selected, but not active.
//We are still using the old one for these checks AND exclude the selected version from possible targets to switch to,
//so if the newly imported version matches all existing entites, use that one.
Core.MainWindow.SelectDeck(deck, true);
return;
}
NotFoundCards = notFound.SelectMany(x => x).Select(x => x.Card).Distinct().ToList();
Log.Warn("Cards not found in deck: " + string.Join(", ", NotFoundCards.Select(x => $"{x.Name} ({x.Id})")));
if(Config.Instance.AutoDeckDetection)
await AutoSelectDeck(deck, Core.Game.Player.Class, Core.Game.CurrentGameMode, Core.Game.CurrentFormat, cardEntites);
}
else
NotFoundCards.Clear();
}
private static async Task AutoSelectDeck(Deck currentDeck, string heroClass, GameMode mode, Format? currentFormat, List<IGrouping<string, Entity>> cardEntites = null)
{
_waitingForDraws++;
await Task.Delay(500);
_waitingForDraws--;
if(_waitingForDraws > 0)
return;
var validDecks = DeckList.Instance.Decks.Where(x => x.Class == heroClass && !x.Archived).ToList();
if(currentDeck != null)
validDecks.Remove(currentDeck);
validDecks = validDecks.FilterByMode(mode, currentFormat);
if(cardEntites != null)
validDecks = validDecks.Where(x => cardEntites.All(ce => x.GetSelectedDeckVersion().Cards.Any(c => c.Id == ce.Key && c.Count >= ce.Count()))).ToList();
if(_autoSelectCount > 1)
{
Log.Info("Too many auto selects. Showing dialog.");
ShowDeckSelectionDialog(validDecks);
return;
}
if(validDecks.Count == 0)
{
if(cardEntites == null || !AutoSelectDeckVersion(heroClass, mode, currentFormat, cardEntites))
{
Log.Info("No matching deck found, using no-deck mode");
Core.MainWindow.SelectDeck(null, true);
}
return;
}
if(validDecks.Count == 1)
{
var deck = validDecks.Single();
Log.Info("Found one matching deck: " + deck);
Core.MainWindow.SelectDeck(deck, true);
_autoSelectCount++;
return;
}
var lastUsed = DeckList.Instance.LastDeckClass.FirstOrDefault(x => x.Class == heroClass);
if(lastUsed != null)
{
var deck = validDecks.FirstOrDefault(x => x.DeckId == lastUsed.Id);
if(deck != null)
{
Log.Info($"Last used {heroClass} deck matches!");
Core.MainWindow.SelectDeck(deck, true);
_autoSelectCount++;
return;
}
}
if(cardEntites == null || !AutoSelectDeckVersion(heroClass, mode, currentFormat, cardEntites))
ShowDeckSelectionDialog(validDecks);
}
private static bool AutoSelectDeckVersion(string heroClass, GameMode mode, Format? format, List<IGrouping<string, Entity>> cardEntites)
{
var validDecks = DeckList.Instance.Decks.Where(x => x.Class == heroClass && !x.Archived).ToList();
validDecks = validDecks.FilterByMode(mode, format);
foreach(var deck in validDecks)
{
foreach(var version in deck.VersionsIncludingSelf.Where(x => x != deck.SelectedVersion).Select(deck.GetVersion))
{
if(!cardEntites.All(ce => version.Cards.Any(c => c.Id == ce.Key && c.Count >= ce.Count())))
continue;
Log.Info($"Found matching version on {deck.Name}: {version.Version.ShortVersionString}.");
deck.SelectVersion(version);
Core.MainWindow.SelectDeck(deck, true);
_autoSelectCount++;
return true;
}
}
Log.Info("Found no matching version.");
return false;
}
private static void ShowDeckSelectionDialog(List<Deck> decks)
{
decks.Add(new Deck("Use no deck", "", new List<Card>(), new List<string>(), "", "", DateTime.Now, false, new List<Card>(),
SerializableVersion.Default, new List<Deck>(), false, "", Guid.Empty, ""));
if(decks.Count == 1 && DeckList.Instance.ActiveDeck != null)
{
decks.Add(new Deck("No match - Keep using active deck", "", new List<Card>(), new List<string>(), "", "", DateTime.Now, false,
new List<Card>(), SerializableVersion.Default, new List<Deck>(), false, "", Guid.Empty, ""));
}
_waitingForUserInput = true;
Log.Info("Waiting for user input...");
var dsDialog = new DeckSelectionDialog(decks);
dsDialog.ShowDialog();
var selectedDeck = dsDialog.SelectedDeck;
if(selectedDeck != null)
{
if(selectedDeck.Name == "Use no deck")
{
Log.Info("Auto deck detection disabled.");
Core.MainWindow.SelectDeck(null, true);
NotFoundCards.Clear();
}
else if(selectedDeck.Name == "No match - Keep using active deck")
{
IgnoredDeckId = DeckList.Instance.ActiveDeck?.DeckId ?? Guid.Empty;
Log.Info($"Now ignoring {DeckList.Instance.ActiveDeck?.Name}");
NotFoundCards.Clear();
}
else
{
Log.Info("Selected deck: " + selectedDeck.Name);
Core.MainWindow.SelectDeck(selectedDeck, true);
}
}
else
{
Log.Info("Auto deck detection disabled.");
Core.MainWindow.ShowMessage("Auto deck selection disabled.", "This can be re-enabled by selecting \"AUTO\" in the bottom right of the deck picker.").Forget();
Config.Instance.AutoDeckDetection = false;
Config.Save();
Core.MainWindow.DeckPickerList.UpdateAutoSelectToggleButton();
}
_waitingForUserInput = false;
}
public static void ResetIgnoredDeckId() => IgnoredDeckId = Guid.Empty;
public static void ImportDecks(IEnumerable<ImportedDeck> decks, bool brawl, bool importNew = true, bool updateExisting = true, bool select = true)
{
Deck toSelect = null;
foreach(var deck in decks)
{
if(deck.SelectedImportOption is NewDeck)
{
if(!importNew)
continue;
Log.Info($"Saving {deck.Deck.Name} as new deck.");
var newDeck = new Deck {
Class = deck.Class,
Name = deck.Deck.Name,
HsId = deck.Deck.Id,
Cards = new ObservableCollection<Card>(deck.Deck.Cards.Select(x =>
{
var card = Database.GetCardFromId(x.Id);
card.Count = x.Count;
return card;
})),
LastEdited = DateTime.Now,
IsArenaDeck = false
};
if(brawl)
{
newDeck.Tags.Add("Brawl");
newDeck.Name = Helper.ParseDeckNameTemplate(Config.Instance.BrawlDeckNameTemplate, newDeck);
}
DeckList.Instance.Decks.Add(newDeck);
toSelect = newDeck;
}
else
{
if(!updateExisting)
continue;
var existing = deck.SelectedImportOption as ExistingDeck;
if(existing == null)
continue;
var target = existing.Deck;
target.HsId = deck.Deck.Id;
if(brawl && !target.Tags.Any(x => x.ToUpper().Contains("BRAWL")))
target.Tags.Add("Brawl");
if(existing.NewVersion.Major == 0)
Log.Info($"Assinging id to existing deck: {deck.Deck.Name}.");
else
{
Log.Info($"Saving {deck.Deck.Name} as {existing.NewVersion.ShortVersionString} (prev={target.Version.ShortVersionString}).");
DeckList.Instance.Decks.Remove(target);
var oldDeck = (Deck)target.Clone();
oldDeck.Versions = new List<Deck>();
target.Name = deck.Deck.Name;
target.LastEdited = DateTime.Now;
target.Versions.Add(oldDeck);
target.Version = existing.NewVersion;
target.SelectedVersion = existing.NewVersion;
target.HearthStatsDeckVersionId = "";
target.Cards.Clear();
var cards = deck.Deck.Cards.Select(x =>
{
var card = Database.GetCardFromId(x.Id);
card.Count = x.Count;
return card;
});
foreach(var card in cards)
target.Cards.Add(card);
var clone = (Deck)target.Clone();
DeckList.Instance.Decks.Add(clone);
toSelect = clone;
}
}
}
DeckList.Save();
Core.MainWindow.DeckPickerList.UpdateDecks();
Core.MainWindow.UpdateIntroLabelVisibility();
if(select && toSelect != null)
Core.MainWindow.SelectDeck(toSelect, true);
Core.UpdatePlayerCards(true);
}
private static DateTime _lastAutoImport;
public static bool AutoImport(bool select)
{
if((DateTime.UtcNow - _lastAutoImport).TotalSeconds < 5)
return false;
_lastAutoImport = DateTime.UtcNow;
switch(Core.Game.CurrentGameMode)
{
case GameMode.Ranked:
case GameMode.Casual:
case GameMode.Friendly:
case GameMode.Practice:
return AutoImportConstructed(select);
case GameMode.Arena:
AutoImportArena(Config.Instance.SelectedArenaImportingBehaviour ?? ArenaImportingBehaviour.AutoImportSave);
break;
}
return false;
}
public static bool AutoImportConstructed(bool select)
{
var decks = DeckImporter.FromConstructed();
if(decks.Any() && (Config.Instance.ConstructedAutoImportNew || Config.Instance.ConstructedAutoUpdate))
{
ImportDecks(decks, false, Config.Instance.ConstructedAutoImportNew, Config.Instance.ConstructedAutoUpdate, select);
return true;
}
return false;
}
public static bool AutoImportArena(ArenaImportingBehaviour behaviour, ArenaInfo info = null)
{
var deck = info ?? DeckImporter.FromArena();
if(deck?.Deck.Cards.Sum(x => x.Count) != 30)
return false;
Log.Info($"Found new complete {deck.Deck.Hero} arena deck!");
var recentArenaDecks =
DeckList.Instance.Decks.Where(d => d.IsArenaDeck && d.Cards.Sum(x => x.Count) == 30).OrderByDescending(
d => d.LastPlayedNewFirst).Take(15);
if(recentArenaDecks.Any(d => d.Cards.All(c => deck.Deck.Cards.Any(c2 => c.Id == c2.Id && c.Count == c2.Count))))
Log.Info("...but we already have that one. Discarding.");
else if(Core.Game.IgnoredArenaDecks.Contains(deck.Deck.Id))
Log.Info("...but it was already discarded by the user. No automatic action taken.");
else if(behaviour == ArenaImportingBehaviour.AutoAsk)
{
Core.MainWindow.ShowNewArenaDeckMessageAsync(deck.Deck);
return true;
}
else if(behaviour == ArenaImportingBehaviour.AutoImportSave)
{
Log.Info("...auto saving new arena deck.");
Core.MainWindow.ImportArenaDeck(deck.Deck);
return true;
}
return false;
}
}
public static class DeckListExtensions
{
public static List<Deck> FilterByMode(this List<Deck> decks, GameMode mode, Format? format)
{
var filtered = new List<Deck>(decks);
if(mode == GameMode.Arena)
filtered = filtered.Where(x => x.IsArenaDeck && x.IsArenaRunCompleted != true).ToList();
else if(mode != GameMode.None)
{
filtered = filtered.Where(x => !x.IsArenaDeck).ToList();
if(format == Format.Standard)
filtered = filtered.Where(x => x.StandardViable).ToList();
}
return filtered;
}
}
}