Skip to content

Commit

Permalink
feat(DeckImporter): import Whizbang decks from memory
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Dec 16, 2023
1 parent 6c68239 commit bfde397
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 31 deletions.
12 changes: 12 additions & 0 deletions Hearthstone Deck Tracker/DeckManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,18 @@ public static bool AutoImportArena(ArenaImportingBehaviour behaviour, ArenaInfo?
return false;
}

public static void AutoImportTemplateDeckById(IGame game, int deckId)
{
if(DeckList.Instance.Decks.All(x => x.HsId != deckId))
{
var selectedDeck2 = DeckImporter.FromTemplateDeck(deckId);
if(selectedDeck2 is null)
return;
ImportDecks(new List<ImportedDeck>() { selectedDeck2 }, false);
}
AutoSelectDeckById(game, deckId);
}

internal static async void AutoSelectDeckById(IGame game, long id)
{
Log.Info($"Trying to select deck for id={id}");
Expand Down
2 changes: 1 addition & 1 deletion Hearthstone Deck Tracker/Hearthstone/QueueEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private void AutoSelectArenaDeck()

private static long GetSelectedDeckId(Mode mode)
{
var selectedDeckId = Reflection.Client.GetSelectedDeckInMenu();
var selectedDeckId = Reflection.Client.GetDeckPickerSelectedDeck();
if(selectedDeckId > 0)
return selectedDeckId;
if(mode != TAVERN_BRAWL)
Expand Down
68 changes: 42 additions & 26 deletions Hearthstone Deck Tracker/Importing/DeckImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Hearthstone_Deck_Tracker.Utility;
using Hearthstone_Deck_Tracker.Utility.RemoteData;
using Hearthstone_Deck_Tracker.Utility.Extensions;
using HearthDb.Enums;

#endregion

Expand Down Expand Up @@ -121,32 +122,7 @@ public static List<ImportedDeck> GetImportedDecks(IEnumerable<HearthMirror.Objec
foreach (var deck in hsDecks)
{
if(deck.Cards.Count == 1 && deck.Cards.Single().Id == CardIds.Collectible.Neutral.WhizbangTheWonderful)
{
var data = Remote.Config.Data;
if (data != null)
{
var whizbangDecks = data.WhizbangDecks.Select(x =>
{
if(!Hearthstone.CardIds.CardClassHero.TryGetValue(x.Class, out var hero))
return null;
return new HearthMirror.Objects.Deck
{
Id = x.DeckId,
Name = x.Title,
Cards = x.Cards.Select(c => {
var card = Database.GetCardFromDbfId(c.DbfId);
if(card == null)
return null;
return new HearthMirror.Objects.Card(card.Id, c.Count, 0);
}).Where(x => x != null).ToList(),
Hero = hero,
};
}).WhereNotNull();

importedDecks.AddRange(GetImportedDecks(whizbangDecks, localDecks));
continue;
}
}
continue; // Cannot be imported here (will need to happen in the context of a game with a deck id)

var otherDecks = hsDecks.Except(new[] {deck});
var existing = localDecks.Where(x => otherDecks.All(d => d.Id != x.HsId)).Select(x =>
Expand Down Expand Up @@ -198,5 +174,45 @@ public static List<ImportedDeck> GetImportedDecks(IEnumerable<HearthMirror.Objec
}
return null;
}

public static ImportedDeck? FromTemplateDeck(int id)
{
var deck = GetTemplateDeck(id);
if(deck is null)
return null;
return new ImportedDeck(deck, null, DeckList.Instance.Decks);
}

private static HearthMirror.Objects.Deck? GetTemplateDeck(int id)
{
try
{
var deck = Reflection.Client.GetTemplateDeckById(id);
if(deck is null)
return null;

if(!Hearthstone.CardIds.CardClassHero.TryGetValue((CardClass)deck.Class, out var hero))
return null;

return new HearthMirror.Objects.Deck
{
Id = deck.DeckId,
Name = deck.Title,
Cards = deck.Cards.GroupBy(dbfId => dbfId, (dbfId, dbfIds) =>
{
var card = Database.GetCardFromDbfId(dbfId);
if(card == null)
return null;
return new HearthMirror.Objects.Card(card.Id, dbfIds.Count(), 0);
}).WhereNotNull().ToList(),
Hero = hero,
};
}
catch(Exception e)
{
Log.Error(e);
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ private void WhizbangDeckIdChange(int id, int value, IGame game)
game.Opponent.IsPlayingWhizbang = true;
if(!entity.IsPlayer)
return;
DeckManager.AutoSelectDeckById(game, value);
DeckManager.AutoImportTemplateDeckById(game, value);
}

private void CreatorChanged(int id, int value, IGame game)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ internal class Config
[JsonProperty("arena")]
public ArenaData? Arena { get; set; }

[JsonProperty("whizbang_decks")]
public List<WhizbangDeck>? WhizbangDecks { get; set; }

[JsonProperty("battlegrounds_short_names")]
public List<CardShortName>? BattlegroundsShortNames { get; set; }

Expand Down

0 comments on commit bfde397

Please sign in to comment.