From 7484b520e5ea33e5f368f76d6f3926e12edd3382 Mon Sep 17 00:00:00 2001 From: Evan Kranzler Date: Wed, 24 Mar 2021 18:04:28 -0400 Subject: [PATCH] [KHM] fixed Invasion of the Giants not correctly counting spells for its third chapter (fixes #7664) --- .../src/mage/cards/i/InvasionOfTheGiants.java | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/Mage.Sets/src/mage/cards/i/InvasionOfTheGiants.java b/Mage.Sets/src/mage/cards/i/InvasionOfTheGiants.java index cd67d815d4..dbbe8f22a4 100644 --- a/Mage.Sets/src/mage/cards/i/InvasionOfTheGiants.java +++ b/Mage.Sets/src/mage/cards/i/InvasionOfTheGiants.java @@ -16,11 +16,15 @@ import mage.cards.CardSetInfo; import mage.constants.*; import mage.filter.FilterCard; import mage.game.Game; +import mage.game.events.GameEvent; +import mage.game.stack.Spell; import mage.target.common.TargetCardInHand; import mage.target.common.TargetOpponentOrPlaneswalker; import mage.util.CardUtil; -import mage.watchers.common.CastSpellLastTurnWatcher; +import mage.watchers.Watcher; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; /** @@ -64,7 +68,7 @@ public final class InvasionOfTheGiants extends CardImpl { // III — The next Giant spell you cast this turns costs {2} less to cast. sagaAbility.addChapterEffect(this, SagaChapter.CHAPTER_III, new InvasionOfTheGiantsEffect()); - this.addAbility(sagaAbility); + this.addAbility(sagaAbility, new InvasionOfTheGiantsWatcher()); } private InvasionOfTheGiants(final InvasionOfTheGiants card) { @@ -94,9 +98,9 @@ class InvasionOfTheGiantsEffect extends CostModificationEffectImpl { @Override public void init(Ability source, Game game) { super.init(source, game); - CastSpellLastTurnWatcher watcher = game.getState().getWatcher(CastSpellLastTurnWatcher.class); + InvasionOfTheGiantsWatcher watcher = game.getState().getWatcher(InvasionOfTheGiantsWatcher.class); if (watcher != null) { - spellsCast = watcher.getAmountOfSpellsPlayerCastOnCurrentTurn(source.getControllerId()); + spellsCast = watcher.getCount(source.getControllerId()); } } @@ -108,11 +112,11 @@ class InvasionOfTheGiantsEffect extends CostModificationEffectImpl { @Override public boolean applies(Ability abilityToModify, Ability source, Game game) { - CastSpellLastTurnWatcher watcher = game.getState().getWatcher(CastSpellLastTurnWatcher.class); + InvasionOfTheGiantsWatcher watcher = game.getState().getWatcher(InvasionOfTheGiantsWatcher.class); if (watcher == null) { return false; } - if (watcher.getAmountOfSpellsPlayerCastOnCurrentTurn(source.getControllerId()) > spellsCast) { + if (watcher.getCount(source.getControllerId()) > spellsCast) { discard(); // only one use return false; } @@ -129,3 +133,33 @@ class InvasionOfTheGiantsEffect extends CostModificationEffectImpl { return new InvasionOfTheGiantsEffect(this); } } + +class InvasionOfTheGiantsWatcher extends Watcher { + + private final Map playerMap = new HashMap<>(); + + InvasionOfTheGiantsWatcher() { + super(WatcherScope.GAME); + } + + @Override + public void watch(GameEvent event, Game game) { + if (event.getType() != GameEvent.EventType.SPELL_CAST) { + return; + } + Spell spell = game.getSpell(event.getSourceId()); + if (spell != null && spell.hasSubtype(SubType.GIANT, game)) { + playerMap.compute(event.getPlayerId(), (u, i) -> i == null ? 1 : Integer.sum(i, 1)); + } + } + + @Override + public void reset() { + super.reset(); + playerMap.clear(); + } + + int getCount(UUID playerId) { + return playerMap.getOrDefault(playerId, 0); + } +}