Skip to content

Commit

Permalink
refactor: Twitch Extension buddy handling
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Jul 17, 2024
1 parent 8735ee0 commit 7f4da16
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions Hearthstone Deck Tracker/Live/BoardStateWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,18 @@ private int[] SortedDbfIds(IEnumerable<BattlegroundsTeammateBoardStateEntity> 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)
Expand Down Expand Up @@ -322,8 +328,10 @@ private Tuple<BoardStatePlayer, BoardStatePlayer> 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;
}

Expand All @@ -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
Expand All @@ -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),
Expand Down

0 comments on commit 7f4da16

Please sign in to comment.