Skip to content

Commit

Permalink
OpponentSecrets: NullOrEmpty checks for SetZero/SetMax, refactoring/f…
Browse files Browse the repository at this point in the history
…ormatting
  • Loading branch information
Alexander Zeier committed Oct 30, 2015
1 parent dce3e42 commit cf20969
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 102 deletions.
192 changes: 92 additions & 100 deletions Hearthstone Deck Tracker/Hearthstone/Secrets/OpponentSecrets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Markup;
using Hearthstone_Deck_Tracker.Enums;
using Hearthstone_Deck_Tracker.Hearthstone;
using Hearthstone_Deck_Tracker.Hearthstone.Entities;
Expand All @@ -17,15 +16,15 @@ public class OpponentSecrets
public OpponentSecrets(GameV2 game)
{
Secrets = new List<SecretHelper>();
Game = game;
Game = game;
}

public List<SecretHelper> Secrets { get; private set; }
public int proposedAttackerEntityId { get; set; }
public int proposedDefenderEntityId { get; set; }
public GameV2 Game { get; private set; }
public int ProposedAttackerEntityId { get; set; }
public int ProposedDefenderEntityId { get; set; }
public GameV2 Game { get; private set; }

public List<HeroClass> DisplayedClasses
public List<HeroClass> DisplayedClasses
{
get { return Secrets.Select(x => x.HeroClass).Distinct().OrderBy(x => x).ToList(); }
}
Expand Down Expand Up @@ -74,136 +73,129 @@ public void NewSecretPlayed(HeroClass heroClass, int id, int turn, string knownC
if(knownCardId != null)
{
foreach(var cardId in SecretHelper.GetSecretIds(heroClass))
{
helper.PossibleSecrets[cardId] = cardId == knownCardId;
}
}
Secrets.Add(helper);
Logger.WriteLine("Added secret with id:" + id, "OpponentSecrets");
}

public void SecretRemoved(int id, string cardId)
{
int index = Secrets.FindIndex(s => s.Id == id);
if(index == -1)
public void SecretRemoved(int id, string cardId)
{
int index = Secrets.FindIndex(s => s.Id == id);
if(index == -1)
{
Logger.WriteLine(string.Format("Secret with id={0}, cardId={1} not found when trying to remove it.", id, cardId), "OpponentSecrets");
return;
}
Entity attacker, defender;
Game.Entities.TryGetValue(proposedAttackerEntityId, out attacker);
Game.Entities.TryGetValue(proposedDefenderEntityId, out defender);

// see http://hearthstone.gamepedia.com/Advanced_rulebook#Combat for fast vs. slow secrets

// a few fast secrets can modify combat
// freezing trap and vaporize remove the attacking minion
// misdirection, noble sacrifice change the target

// if multiple secrets are in play and a fast secret triggers,
// we need to eliminate older secrets which would have been triggered by the attempted combat
if (CardIds.Secrets.FastCombat.Contains(cardId) && attacker != null && defender != null)
{
ZeroFromAttack(Game.Entities[proposedAttackerEntityId], Game.Entities[proposedDefenderEntityId], true, index);
}

Secrets.Remove(Secrets[index]);
Logger.WriteLine("Removed secret with id:" + id, "OpponentSecrets");
}

public void ZeroFromAttack(Entity attacker, Entity defender, bool fastOnly = false, int stopIndex = -1)
{
if (!Config.Instance.AutoGrayoutSecrets)
return;

if (stopIndex == -1)
stopIndex = Secrets.Count;

SetZeroOlder(CardIds.Secrets.Paladin.NobleSacrifice, stopIndex);

if (defender.IsHero)
{
if (!fastOnly)
{
SetZeroOlder(CardIds.Secrets.Hunter.BearTrap, stopIndex);
SetZeroOlder(CardIds.Secrets.Mage.IceBarrier, stopIndex);
}

SetZeroOlder(CardIds.Secrets.Hunter.ExplosiveTrap, stopIndex);

if (Game.IsMinionInPlay)
SetZeroOlder(CardIds.Secrets.Hunter.Misdirection, stopIndex);

if (attacker.IsMinion)
{
SetZeroOlder(CardIds.Secrets.Mage.Vaporize, stopIndex);
SetZeroOlder(CardIds.Secrets.Hunter.FreezingTrap, stopIndex);
}
}
else
{
if (!fastOnly)
SetZeroOlder(CardIds.Secrets.Hunter.SnakeTrap, stopIndex);

if (attacker.IsMinion)
SetZeroOlder(CardIds.Secrets.Hunter.FreezingTrap, stopIndex);
}

if (Core.MainWindow != null)
Core.Overlay.ShowSecrets();
}

public void ClearSecrets()
}
Entity attacker, defender;
Game.Entities.TryGetValue(ProposedAttackerEntityId, out attacker);
Game.Entities.TryGetValue(ProposedDefenderEntityId, out defender);

// see http://hearthstone.gamepedia.com/Advanced_rulebook#Combat for fast vs. slow secrets

// a few fast secrets can modify combat
// freezing trap and vaporize remove the attacking minion
// misdirection, noble sacrifice change the target

// if multiple secrets are in play and a fast secret triggers,
// we need to eliminate older secrets which would have been triggered by the attempted combat
if(CardIds.Secrets.FastCombat.Contains(cardId) && attacker != null && defender != null)
ZeroFromAttack(Game.Entities[ProposedAttackerEntityId], Game.Entities[ProposedDefenderEntityId], true, index);

Secrets.Remove(Secrets[index]);
Logger.WriteLine("Removed secret with id:" + id, "OpponentSecrets");
}

public void ZeroFromAttack(Entity attacker, Entity defender, bool fastOnly = false, int stopIndex = -1)
{
if(!Config.Instance.AutoGrayoutSecrets)
return;

if(stopIndex == -1)
stopIndex = Secrets.Count;

SetZeroOlder(CardIds.Secrets.Paladin.NobleSacrifice, stopIndex);

if(defender.IsHero)
{
if(!fastOnly)
{
SetZeroOlder(CardIds.Secrets.Hunter.BearTrap, stopIndex);
SetZeroOlder(CardIds.Secrets.Mage.IceBarrier, stopIndex);
}

SetZeroOlder(CardIds.Secrets.Hunter.ExplosiveTrap, stopIndex);

if(Game.IsMinionInPlay)
SetZeroOlder(CardIds.Secrets.Hunter.Misdirection, stopIndex);

if(attacker.IsMinion)
{
SetZeroOlder(CardIds.Secrets.Mage.Vaporize, stopIndex);
SetZeroOlder(CardIds.Secrets.Hunter.FreezingTrap, stopIndex);
}
}
else
{
if(!fastOnly)
SetZeroOlder(CardIds.Secrets.Hunter.SnakeTrap, stopIndex);

if(attacker.IsMinion)
SetZeroOlder(CardIds.Secrets.Hunter.FreezingTrap, stopIndex);
}

if(Core.MainWindow != null)
Core.Overlay.ShowSecrets();
}

public void ClearSecrets()
{
Secrets.Clear();
Logger.WriteLine("Cleared secrets", "OpponentSecrets");
}

public void SetMax(string cardId)
{
if(string.IsNullOrEmpty(cardId))
return;
foreach(var secret in Secrets)
secret.PossibleSecrets[cardId] = true;
}

public void SetZero(string cardId)
{
SetZeroOlder(cardId, Secrets.Count);
}
public void SetZero(string cardId)
{
if(string.IsNullOrEmpty(cardId))
return;
SetZeroOlder(cardId, Secrets.Count);
}

public void SetZeroOlder(string cardId, int stopIndex)
{
for (int index = 0; index < stopIndex; index++)
Secrets[index].PossibleSecrets[cardId] = false;
}
public void SetZeroOlder(string cardId, int stopIndex)
{
if(string.IsNullOrEmpty(cardId))
return;
for(var index = 0; index < stopIndex; index++)
Secrets[index].PossibleSecrets[cardId] = false;
}

public List<Secret> GetSecrets()
public List<Secret> GetSecrets()
{
var returnThis = DisplayedClasses.SelectMany(SecretHelper.GetSecretIds).Select(cardId => new Secret(cardId, 0)).ToList();

foreach (var secret in Secrets)
foreach(var secret in Secrets)
{
foreach (var possible in secret.PossibleSecrets)
foreach(var possible in secret.PossibleSecrets)
{
if (possible.Value)
{
if(possible.Value)
returnThis.Find(x => x.CardId == possible.Key).Count = 1;
}
}

}

return returnThis;
}

public List<Secret> GetDefaultSecrets(HeroClass heroClass)
{
var count = SecretHelper.GetMaxSecretCount(heroClass);
var returnThis = new List<Secret>();

foreach(var cardId in SecretHelper.GetSecretIds(heroClass))
returnThis.Add(new Secret(cardId, 1));

return returnThis;
return SecretHelper.GetSecretIds(heroClass).Select(cardId => new Secret(cardId, 1)).ToList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,11 @@ public void TagChange(IHsGameState gameState, string rawTag, int id, string rawV
}
else if (tag == GAME_TAG.PROPOSED_DEFENDER)
{
game.OpponentSecrets.proposedDefenderEntityId = value;
game.OpponentSecrets.ProposedDefenderEntityId = value;
}
else if (tag == GAME_TAG.PROPOSED_ATTACKER)
{
game.OpponentSecrets.proposedAttackerEntityId = value;
game.OpponentSecrets.ProposedAttackerEntityId = value;
}
else if (tag == GAME_TAG.NUM_MINIONS_PLAYED_THIS_TURN && value > 0)
{
Expand Down

0 comments on commit cf20969

Please sign in to comment.