Skip to content

Commit

Permalink
refactor(BobsBuddy): secret enumeration
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed May 7, 2024
1 parent c704abd commit 566eab6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
58 changes: 36 additions & 22 deletions Hearthstone Deck Tracker/BobsBuddy/BobsBuddyInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
using Hearthstone_Deck_Tracker.Utility.Extensions;
using Entity = Hearthstone_Deck_Tracker.Hearthstone.Entities.Entity;
using BobsBuddy;
using BobsBuddy.Spells;
using HearthDb;

namespace Hearthstone_Deck_Tracker.BobsBuddy
{
Expand Down Expand Up @@ -55,8 +53,6 @@ private static bool ReportErrors
public Entity? LastAttackingHero = null;
public int LastAttackingHeroAttack;
private static List<string> _recentHDTLog = new List<string>();
private static List<Entity> _currentOpponentSecrets = new List<Entity>();
private static Dictionary<Entity, Entity> _opponentSecretMap = new();

private List<Entity> _opponentHand = new();
private readonly Dictionary<Entity, Entity> _opponentHandMap = new();
Expand Down Expand Up @@ -459,8 +455,6 @@ private void SnapshotBoardState(int turn)

input.SetTurn(turn);

_currentOpponentSecrets = _game.Opponent.Secrets.ToList();

var playerSide = GetOrderedMinions(_game.Player.Board)
.Where(e => e.IsControlledBy(_game.Player.Id))
.Select(e => GetMinionFromEntity(simulator.MinionFactory, true, e, GetAttachedEntities(e.Id)));
Expand Down Expand Up @@ -571,20 +565,8 @@ internal async void UpdateOpponentHand(Entity entity, Entity copy)
await TryRerun();
}

internal async void UpdateSecret(Entity entity)
internal async void UpdateOpponentSecret(Entity entity)
{
var oldSecret = _currentOpponentSecrets.Find(x => x.Id == entity.Id);
if(oldSecret != null)
_opponentSecretMap[oldSecret] = entity;

_currentOpponentSecrets = _currentOpponentSecrets.Select(x => {
if(_opponentSecretMap.TryGetValue(x, out var retval))
{
return retval;
}
return entity;
}).ToList();

await TryRerun();
}

Expand Down Expand Up @@ -628,7 +610,13 @@ private IEnumerable<Entity> GetAttachedEntities(int entityId)

try
{
_input.SetupSecretsFromDbfidList(_currentOpponentSecrets.Select(x => x != null && !string.IsNullOrEmpty(x.CardId) ? (int?)x.Card.DbfId : null).ToList(), false);
_input.SetupSecretsFromDbfidList(
_game.Opponent.Secrets
.Select(x => !string.IsNullOrEmpty(x.CardId) ? (int?)x.Card.DbfId : null)
.Distinct(new SecretDbfIdComparer())
.ToList(),
false
);
DebugLog($"Set opponent S. with {_input.OpponentSecrets.Count} S.");

DebugLog("----- Simulation Input -----");
Expand All @@ -651,14 +639,14 @@ private IEnumerable<Entity> GetAttachedEntities(int entityId)
DebugLog($"[{quest.QuestCardId} ({quest.QuestProgress}/{quest.QuestProgressTotal}): {quest.RewardCardId}]");


if(_input.PlayerSecrets.Count() > 0)
if(_input.PlayerSecrets.Any())
{
DebugLog("Detected the following player S.");
foreach(var s in _input.PlayerSecrets)
DebugLog(s.ToString());
}

if(_input.OpponentSecrets.Count() > 0)
if(_input.OpponentSecrets.Any())
{
DebugLog("Detected the following opponent S.");
foreach(var s in _input.OpponentSecrets)
Expand Down Expand Up @@ -845,5 +833,31 @@ private void AlertWithLastInputOutput(string result)
if(_input != null && Output != null)
Sentry.QueueBobsBuddyTerminalCase(_input, Output, result, _turn, _recentHDTLog, _game.CurrentRegion);
}

/**
* A comparer that keeps unknown secrets (null) and de-duplicates dbf ids otherwise.
* For example { 1, null, 3, 3, null} will be deduplicated to {1, null, 3, null}.
*/
private class SecretDbfIdComparer : IEqualityComparer<int?>
{
public bool Equals(int? x, int? y)
{
if (x == null || y == null)
{
return false;
}

return x == y;
}

public int GetHashCode(int? obj)
{
if (obj == null)
{
return 0;
}
return obj.GetHashCode();
}
}
}
}
2 changes: 1 addition & 1 deletion Hearthstone Deck Tracker/GameEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,7 @@ public void HandleOpponentSecretTrigger(Entity entity, string? cardId, int turn,
GameEvents.OnOpponentSecretTriggered.Execute(card);
if(_game.IsBattlegroundsMatch && Config.Instance.RunBobsBuddy && _game.CurrentGameStats != null)
BobsBuddyInvoker.GetInstance(_game.CurrentGameStats.GameId, _game.GetTurnNumber())
?.UpdateSecret(entity);
?.UpdateOpponentSecret(entity);
}

public void HandleOpponentDeckDiscard(Entity entity, string? cardId, int turn)
Expand Down

0 comments on commit 566eab6

Please sign in to comment.