Skip to content

Commit 97bd073

Browse files
committed
fix: timewarped tavern affecting game state
1 parent 21b3dcb commit 97bd073

3 files changed

Lines changed: 28 additions & 39 deletions

File tree

HearthWatcher/SpecialShopChoicesStateWatcher.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Threading.Tasks;
33
using HearthWatcher.EventArgs;
44
using HearthWatcher.Providers;
@@ -14,6 +14,7 @@ public class SpecialShopChoicesStateWatcher
1414
private bool _running;
1515
private bool _watch;
1616
private SpecialShopChoicesArgs _prev = null;
17+
public SpecialShopChoicesArgs? CurrentState => _prev;
1718

1819
public SpecialShopChoicesStateWatcher(ISpecialShopChoicesProvider opponentBoardProvider, int delay = 16)
1920
{

Hearthstone Deck Tracker/Live/BoardStateWatcher.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ private int ZonePosition(BattlegroundsTeammateBoardStateEntity e) =>
105105
private int[] SortedDbfIds(IEnumerable<BattlegroundsTeammateBoardStateEntity> entities) =>
106106
entities.OrderBy(ZonePosition).Select(DbfId).ToArray();
107107

108+
private int[] SortedDbfIds(IEnumerable<BoardCard> boardCards) =>
109+
boardCards
110+
.Where(c => c?.CardId != null)
111+
.Select(c => Database.GetCardFromId(c.CardId)?.DbfId ?? 0)
112+
.Where(dbfId => dbfId != 0)
113+
.ToArray();
114+
108115
private int HeroId(Entity playerEntity) => playerEntity.GetTag(GameTag.HERO_ENTITY);
109116

110117
private int WeaponId(Entity playerEntity) => playerEntity.GetTag(GameTag.WEAPON);
@@ -263,6 +270,13 @@ private int HeroDbfId(Entity? entity)
263270
var anomalyId = new[] { GameTag.ANOMALY1, GameTag.ANOMALY2 }.Select(x => Core.Game.GameEntity?.GetTag(x)).FirstOrDefault(x => x is > 0);
264271
var anomaly = anomalyId.HasValue && Core.Game.Entities.TryGetValue(anomalyId.Value, out var anomalyEntity) ? anomalyEntity?.Card.DbfId : null;
265272

273+
// Check if the special shop (timewarped tavern) is currently active
274+
var specialShopState = Watchers.SpecialShopChoicesStateWatcher.CurrentState;
275+
var specialShopActive = specialShopState?.IsActive == true && specialShopState.BoardCards.Count > 0;
276+
var opponentBoard = specialShopActive
277+
? SortedDbfIds(specialShopState!.BoardCards)
278+
: SortedDbfIds(opponent.Board.Where(x => x.TakesBoardSlot));
279+
266280
return new BoardState
267281
{
268282
Player = new BoardStatePlayer
@@ -292,7 +306,7 @@ private int HeroDbfId(Entity? entity)
292306
},
293307
Opponent = new BoardStatePlayer
294308
{
295-
Board = SortedDbfIds(opponent.Board.Where(x => x.TakesBoardSlot)),
309+
Board = opponentBoard,
296310
Deck = new BoardStateDeck
297311
{
298312
Size = opponent.DeckCount
@@ -325,6 +339,13 @@ private Tuple<BoardStatePlayer, BoardStatePlayer> GetBattlegroundsSoloPlayerBoar
325339
int? opponentWeaponEntityId = opponentEntity != null ? WeaponId(opponentEntity) : null;
326340
int opponentWeapon = opponentWeaponEntityId.HasValue ? DbfId(Find(opponent, opponentWeaponEntityId.Value)) : 0;
327341

342+
// Check if the special shop (timewarped tavern) is currently active
343+
var specialShopState = Watchers.SpecialShopChoicesStateWatcher.CurrentState;
344+
var specialShopActive = specialShopState?.IsActive == true && specialShopState.BoardCards.Count > 0;
345+
var opponentBoard = specialShopActive
346+
? SortedDbfIds(specialShopState!.BoardCards)
347+
: SortedDbfIds(opponent.Board.Where(x => x.TakesBoardSlot));
348+
328349
return new Tuple<BoardStatePlayer, BoardStatePlayer>(
329350
new BoardStatePlayer
330351
{
@@ -346,7 +367,7 @@ private Tuple<BoardStatePlayer, BoardStatePlayer> GetBattlegroundsSoloPlayerBoar
346367
Fatigue = playerEntity?.GetTag(GameTag.FATIGUE) ?? 0
347368
}, new BoardStatePlayer
348369
{
349-
Board = SortedDbfIds(opponent.Board.Where(x => x.TakesBoardSlot)),
370+
Board = opponentBoard,
350371
Hero = HeroDbfId(opponentEntity != null ? Find(opponent, HeroId(opponentEntity)) : null),
351372
HeroPower = BgsQuestReward(opponent, true) ?? BgsTrinket(opponent, TrinketHeroPowerSlot) ?? DbfId(FindHeroPower(opponent)),
352373
Weapon = opponentWeapon != 0 ? opponentWeapon :

Hearthstone Deck Tracker/Windows/OverlayWindow.xaml.cs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public partial class OverlayWindow : Window, INotifyPropertyChanged
7373
private readonly Dictionary<UIElement, ResizeGrip> _movableElements = new Dictionary<UIElement, ResizeGrip>();
7474
private readonly List<FrameworkElement> _clickableElements = new List<FrameworkElement>();
7575
private readonly HashSet<FrameworkElement> _hoverableElements = new HashSet<FrameworkElement>();
76-
private readonly HashSet<int> _specialShopEntityIds = new HashSet<int>();
7776
private readonly List<Rectangle> _playerHand = new List<Rectangle>();
7877
private readonly List<Rectangle> _leaderboardIcons = new List<Rectangle>();
7978
private readonly List<HearthstoneTextBlock> _leaderboardDeadForText = new List<HearthstoneTextBlock>();
@@ -1391,44 +1390,12 @@ internal void HandleSpecialShop(SpecialShopChoicesArgs args)
13911390
if(!_game.IsBattlegroundsMatch)
13921391
return;
13931392

1394-
var isActive = args.IsActive;
1395-
var boardCards = args.BoardCards;
1396-
1397-
// Clear previously added entities by resetting their tags
1398-
foreach(var entityId in _specialShopEntityIds)
1399-
{
1400-
if(_game.Entities.TryGetValue(entityId, out var previousEntity))
1401-
{
1402-
previousEntity.Info.Created = false;
1403-
previousEntity.SetTag(GameTag.ZONE, 0);
1404-
previousEntity.SetTag(GameTag.ZONE_POSITION, 0);
1405-
previousEntity.SetTag(GameTag.CONTROLLER, 0);
1406-
}
1407-
}
1408-
_specialShopEntityIds.Clear();
1409-
1410-
if(!isActive)
1411-
return;
1412-
if(boardCards == null)
1393+
if(!args.IsActive)
14131394
return;
14141395

1415-
Core.Overlay.BattlegroundsMinionPinningViewModel.OnShopChange(boardCards, args.MousedOverSlot);
1396+
var boardCards = args.BoardCards;
14161397

1417-
for(var i = 0; i < boardCards.Count; i++)
1418-
{
1419-
var card = boardCards[i];
1420-
if(card == null)
1421-
continue;
1422-
var entity = _game.Entities.FirstOrDefault(x => x.Value.Id == card.EntityId).Value;
1423-
if(entity != null)
1424-
{
1425-
entity.SetTag(GameTag.CONTROLLER, _game.Opponent.Id);
1426-
entity.SetTag(GameTag.ZONE_POSITION, i);
1427-
entity.SetTag(GameTag.ZONE, (int)Zone.PLAY);
1428-
_game.Opponent.CreateInPlay(entity, _game.GetTurnNumber());
1429-
_specialShopEntityIds.Add(entity.Id);
1430-
}
1431-
}
1398+
Core.Overlay.BattlegroundsMinionPinningViewModel.OnShopChange(boardCards, args.MousedOverSlot);
14321399
}
14331400

14341401
private void BgsInspirationCover_OnMouseDown(object sender, MouseButtonEventArgs e) => HideBgsInspiration();

0 commit comments

Comments
 (0)