Skip to content

Commit

Permalink
feat: add normal/triple card id lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
azeier committed Dec 5, 2024
1 parent 7f94663 commit 7205fc6
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions HearthDb/Cards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public static class Cards
public static readonly Dictionary<string, Card> BaconPoolMinions = new Dictionary<string, Card>();
public static readonly Dictionary<int, Card> BaconPoolMinionsByDbfId = new Dictionary<int, Card>();

public static readonly Dictionary<string, string> NormalToTripleCardIds = new Dictionary<string, string>();
public static readonly Dictionary<string, string> TripleToNormalCardIds = new Dictionary<string, string>();

public static readonly Dictionary<int, int> NormalToTripleDbfIds = new Dictionary<int, int>();
public static readonly Dictionary<int, int> TripleToNormalDbfIds = new Dictionary<int, int>();

static Cards()
{
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("HearthDb.CardDefs.xml");
Expand All @@ -33,6 +39,8 @@ static Cards()
{
var xml = new XmlSerializer(typeof(CardDefs.CardDefs));
var cardDefs = (CardDefs.CardDefs)xml.Deserialize(tr);
var baconTriples = new List<(Card, int)>();
var nonBaconTriples = new List<(Card, int)>();
foreach(var entity in cardDefs.Entites)
{
// For some reason Deflect-o-bot is missing divine shield
Expand All @@ -47,15 +55,46 @@ static Cards()
Collectible.Add(entity.CardId, card);
CollectibleByDbfId.Add(entity.DbfId, card);
}

if (card.IsBaconPoolMinion)
{
BaconPoolMinions.Add(entity.CardId, card);
BaconPoolMinionsByDbfId.Add(entity.DbfId, card);
}

var tripleDbfId = card.Entity.Tags.FirstOrDefault(x => x.EnumId == 1429);
if (tripleDbfId != null)
{
if(card.IsBaconPoolMinion)
baconTriples.Add((card, tripleDbfId.Value));
else
nonBaconTriples.Add((card, tripleDbfId.Value));
}
}

// Triples have to be resolved after the first loop since we need to look up the triple card from the id
// Loop over non-bacon first in case both contain a mapping to the same card.
// We want to use the bacon one in that case.
foreach (var (card, tripleDbfId) in nonBaconTriples.Concat(baconTriples))
{
if (!AllByDbfId.TryGetValue(tripleDbfId, out var triple))
continue;
NormalToTripleCardIds[card.Id] = triple.Id;
NormalToTripleDbfIds[card.DbfId] = triple.DbfId;
TripleToNormalCardIds[triple.Id] = card.Id;
TripleToNormalDbfIds[triple.DbfId] = card.DbfId;
}
}
}

/// <summary>
/// Will try to return the card ID of the triple. Returns the normal card id if it cannot be found.
/// </summary>
public static string TryGetTripleId(string normalCardId)
{
return NormalToTripleCardIds.TryGetValue(normalCardId, out var triple) ? triple : normalCardId;
}

public static Card GetFromName(string name, Locale lang, bool collectible = true)
=> (collectible ? Collectible : All).Values.FirstOrDefault(x => x.GetLocName(lang)?.Equals(name, StringComparison.InvariantCultureIgnoreCase) ?? false);

Expand Down

0 comments on commit 7205fc6

Please sign in to comment.