From 7f4da16ada8c0b36b2ae0f93079b551be93e1b71 Mon Sep 17 00:00:00 2001 From: Benedict Etzel Date: Wed, 17 Jul 2024 20:04:29 +0200 Subject: [PATCH] refactor: Twitch Extension buddy handling --- .../Live/BoardStateWatcher.cs | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/Hearthstone Deck Tracker/Live/BoardStateWatcher.cs b/Hearthstone Deck Tracker/Live/BoardStateWatcher.cs index 4e38cef48..f7f3fb6bd 100644 --- a/Hearthstone Deck Tracker/Live/BoardStateWatcher.cs +++ b/Hearthstone Deck Tracker/Live/BoardStateWatcher.cs @@ -127,12 +127,18 @@ private int[] SortedDbfIds(IEnumerable en private int? BuddyDbfId(Player player) { - if(Core.Game.BattlegroundsBuddiesEnabled) + if(!Core.Game.BattlegroundsBuddiesEnabled) return null; - var buddyDbfId = player.Hero?.GetTag(GameTag.BACON_COMPANION_ID); - if(buddyDbfId == 0) + + var meter = player.Board.FirstOrDefault(x => x.GetTag(GameTag.CARDTYPE) == (int)CardType.BATTLEGROUND_HERO_BUDDY); + if(meter == null || meter?.GetTag(GameTag.ZONE) != (int)Zone.PLAY) return null; - return buddyDbfId; + + var buddyDbfId = meter?.GetTag(GameTag.BACON_COMPANION_ID); + if(buddyDbfId == 0) + buddyDbfId = player.Hero?.GetTag(GameTag.BACON_COMPANION_ID); + + return buddyDbfId != 0 ? buddyDbfId : null; } private int? BgsQuestReward(Player player, bool heroPower) @@ -322,8 +328,10 @@ private Tuple GetBattlegroundsSoloPlayerBoar ); } - private static int GetTag(BattlegroundsTeammateBoardStateEntity entity, GameTag tag) + private static int GetTag(BattlegroundsTeammateBoardStateEntity? entity, GameTag tag) { + if(entity == null) + return 0; return entity.Tags.TryGetValue((int)tag, out var value) ? value : 0; } @@ -340,10 +348,18 @@ int controller entity => GetTag(entity, GameTag.ZONE) == (int)Zone.PLAY ).ToList(); - var hero =inPlay.FirstOrDefault(entity => GetTag(entity, GameTag.CARDTYPE) == (int)CardType.HERO); + var hero = inPlay.FirstOrDefault(entity => GetTag(entity, GameTag.CARDTYPE) == (int)CardType.HERO); var heroPower = inPlay.FirstOrDefault(entity => GetTag(entity, GameTag.CARDTYPE) == (int)CardType.HERO_POWER); + var weapon = inPlay.FirstOrDefault(entity => GetTag(entity, GameTag.CARDTYPE) == (int)CardType.WEAPON) ?? inPlay.FirstOrDefault(entity => GetTag(entity, GameTag.CARDTYPE) == (int)CardType.BATTLEGROUND_QUEST_REWARD); + var buddyDbfId = 0; + if(Core.Game.BattlegroundsBuddiesEnabled) + { + var meter = friendlyEntities.FirstOrDefault(x => GetTag(x, GameTag.CARDTYPE) == (int)CardType.BATTLEGROUND_HERO_BUDDY); + if(meter != null && GetTag(meter, GameTag.ZONE) == (int)Zone.PLAY) + buddyDbfId = GetTag(meter, GameTag.BACON_COMPANION_ID); + } var board = inPlay.Where(x => (CardType)GetTag(x, GameTag.CARDTYPE) is CardType.MINION or CardType.LOCATION or CardType.BATTLEGROUND_SPELL @@ -362,7 +378,7 @@ int controller Board = SortedDbfIds(board), Hero = DbfId(hero), HeroPower = DbfId(heroPower), - Weapon = DbfId(weapon), + Weapon = weapon != null ? DbfId(weapon) : buddyDbfId, Hand = new BoardStateHand { Cards = SortedDbfIds(hand),