From e51b054249510fe6a6dd0f520dc49f3a4bfa2036 Mon Sep 17 00:00:00 2001 From: Oleg Agafonov Date: Wed, 24 Apr 2019 21:41:24 +0400 Subject: [PATCH] * Oath of Kaya - fixed that it doesn't triggers on attacks; --- Mage.Sets/src/mage/cards/o/OathOfKaya.java | 86 ++++++++----------- .../cards/abilities/other/OathOfKayaTest.java | 80 +++++++++++++++++ .../main/java/mage/game/combat/Combat.java | 12 +++ 3 files changed, 126 insertions(+), 52 deletions(-) create mode 100644 Mage.Tests/src/test/java/org/mage/test/cards/abilities/other/OathOfKayaTest.java diff --git a/Mage.Sets/src/mage/cards/o/OathOfKaya.java b/Mage.Sets/src/mage/cards/o/OathOfKaya.java index f6f751a1ca5..235d0d0ff7a 100644 --- a/Mage.Sets/src/mage/cards/o/OathOfKaya.java +++ b/Mage.Sets/src/mage/cards/o/OathOfKaya.java @@ -3,7 +3,6 @@ package mage.cards.o; import mage.abilities.Ability; import mage.abilities.TriggeredAbilityImpl; import mage.abilities.common.EntersBattlefieldTriggeredAbility; -import mage.abilities.effects.Effect; import mage.abilities.effects.common.DamageTargetEffect; import mage.abilities.effects.common.GainLifeEffect; import mage.cards.CardImpl; @@ -18,8 +17,6 @@ import mage.players.Player; import mage.target.common.TargetAnyTarget; import mage.target.targetpointer.FixedTarget; -import java.util.HashSet; -import java.util.Set; import java.util.UUID; /** @@ -38,7 +35,8 @@ public final class OathOfKaya extends CardImpl { ability.addTarget(new TargetAnyTarget()); this.addAbility(ability); - // Whenever an opponent attacks a planeswalker you control with one or more creatures, Oath of Kaya deals 2 damage to that player and you gain 2 life. + // Whenever an opponent attacks a planeswalker you control with one or more creatures, + // Oath of Kaya deals 2 damage to that player and you gain 2 life. this.addAbility(new OathOfKayaTriggeredAbility()); } @@ -53,54 +51,14 @@ public final class OathOfKaya extends CardImpl { } class OathOfKayaTriggeredAbility extends TriggeredAbilityImpl { - private final Set attackedThisCombat = new HashSet(); - OathOfKayaTriggeredAbility() { - super(Zone.BATTLEFIELD, null, false); + public OathOfKayaTriggeredAbility() { + super(Zone.BATTLEFIELD, new DamageTargetEffect(2), false); + this.addEffect(new GainLifeEffect(2)); } - private OathOfKayaTriggeredAbility(final OathOfKayaTriggeredAbility ability) { + public OathOfKayaTriggeredAbility(final OathOfKayaTriggeredAbility ability) { super(ability); - this.attackedThisCombat.addAll(ability.attackedThisCombat); - } - - @Override - public boolean checkEventType(GameEvent event, Game game) { - return event.getType() == GameEvent.EventType.ATTACKER_DECLARED - || event.getType() == GameEvent.EventType.DECLARE_ATTACKERS_STEP_POST; - } - - @Override - public boolean checkTrigger(GameEvent event, Game game) { - if (event.getType() == GameEvent.EventType.DECLARE_ATTACKERS_STEP_POST) { - this.attackedThisCombat.clear(); - return false; - } - Player player = game.getPlayer(getSourceId()); - if (player == null) { - return false; - } - for (UUID attackerId : game.getCombat().getAttackers()) { - Permanent attacker = game.getPermanent(attackerId); - if (attacker == null) { - continue; - } - UUID defendingPlayerId = game.getCombat().getDefendingPlayerId(attackerId, game); - UUID defenderId = game.getCombat().getDefenderId(attackerId); - if (defendingPlayerId.equals(defenderId) - || attackedThisCombat.contains(defenderId) - || !player.hasOpponent(defendingPlayerId, game)) { - continue; - } - attackedThisCombat.add(defenderId); - this.getEffects().clear(); - Effect effect = new DamageTargetEffect(2); - effect.setTargetPointer(new FixedTarget(attacker.getControllerId(), game)); - this.addEffect(effect); - this.addEffect(new GainLifeEffect(2)); - return true; - } - return false; } @Override @@ -109,8 +67,32 @@ class OathOfKayaTriggeredAbility extends TriggeredAbilityImpl { } @Override - public String getRule() { - return "Whenever an opponent attacks a planeswalker you control with one or more creatures, " + - "{this} deals 2 damage to that player and you gain 2 life."; + public boolean checkEventType(GameEvent event, Game game) { + return event.getType() == GameEvent.EventType.DECLARED_ATTACKERS; } -} + + @Override + public boolean checkTrigger(GameEvent event, Game game) { + Player you = game.getPlayer(this.getControllerId()); + if (you == null) { + return false; + } + + if (game.getCombat().isPlaneswalkerAttacked(you.getId(), game)) { + for (UUID attacker : game.getCombat().getAttackers()) { + Permanent attackingPermanent = game.getPermanent(attacker); + if (attackingPermanent != null && attackingPermanent.isCreature()) { + getEffects().setTargetPointer(new FixedTarget(attackingPermanent.getControllerId(), game)); + return true; + } + } + } + return false; + } + + @Override + public String getRule() { + return "Whenever an opponent attacks a planeswalker you control with one or more creatures, " + + "{this} deals 2 damage to that player and you gain 2 life."; + } +} \ No newline at end of file diff --git a/Mage.Tests/src/test/java/org/mage/test/cards/abilities/other/OathOfKayaTest.java b/Mage.Tests/src/test/java/org/mage/test/cards/abilities/other/OathOfKayaTest.java new file mode 100644 index 00000000000..cd78d60a121 --- /dev/null +++ b/Mage.Tests/src/test/java/org/mage/test/cards/abilities/other/OathOfKayaTest.java @@ -0,0 +1,80 @@ +package org.mage.test.cards.abilities.other; + +import mage.constants.PhaseStep; +import mage.constants.Zone; +import mage.counters.CounterType; +import org.junit.Test; +import org.mage.test.serverside.base.CardTestPlayerBase; + +/** + * @author JayDi85 + */ +public class OathOfKayaTest extends CardTestPlayerBase { + + @Test + public void test_AttackingPlayer() { + // Whenever an opponent attacks a planeswalker you control with one or more creatures, + // Oath of Kaya deals 2 damage to that player and you gain 2 life. + addCard(Zone.BATTLEFIELD, playerB, "Oath of Kaya", 1); + // + addCard(Zone.BATTLEFIELD, playerA, "Grizzly Bears", 2); // 2/2 + addCard(Zone.BATTLEFIELD, playerB, "Liliana, Dreadhorde General", 1); + + attack(1, playerA, "Grizzly Bears", playerB); + + setStrictChooseMode(true); + setStopAt(1, PhaseStep.END_TURN); + execute(); + assertAllCommandsUsed(); + + assertCounterCount(playerB, "Liliana, Dreadhorde General", CounterType.LOYALTY, 6); + assertLife(playerA, 20); + assertLife(playerB, 20 - 2); + } + + @Test + public void test_AttackingPlaneswalker() { + // Whenever an opponent attacks a planeswalker you control with one or more creatures, + // Oath of Kaya deals 2 damage to that player and you gain 2 life. + addCard(Zone.BATTLEFIELD, playerB, "Oath of Kaya", 1); + // + addCard(Zone.BATTLEFIELD, playerA, "Grizzly Bears", 2); // 2/2 + addCard(Zone.BATTLEFIELD, playerB, "Liliana, Dreadhorde General", 1); + + attack(1, playerA, "Grizzly Bears", "Liliana, Dreadhorde General"); + attack(1, playerA, "Grizzly Bears", "Liliana, Dreadhorde General"); + + setStrictChooseMode(true); + setStopAt(1, PhaseStep.END_TURN); + execute(); + assertAllCommandsUsed(); + + assertCounterCount(playerB, "Liliana, Dreadhorde General", CounterType.LOYALTY, 6 - 2 * 2); + assertLife(playerA, 20 - 2); + assertLife(playerB, 20 + 2); + } + + @Test + public void test_AttackingTwoPlaneswalkers() { + // Whenever an opponent attacks a planeswalker you control with one or more creatures, + // Oath of Kaya deals 2 damage to that player and you gain 2 life. + addCard(Zone.BATTLEFIELD, playerB, "Oath of Kaya", 1); + // + addCard(Zone.BATTLEFIELD, playerA, "Grizzly Bears", 2); // 2/2 + addCard(Zone.BATTLEFIELD, playerB, "Liliana, Dreadhorde General", 1); + addCard(Zone.BATTLEFIELD, playerB, "Vivien, Champion of the Wilds", 1); + + attack(1, playerA, "Grizzly Bears", "Liliana, Dreadhorde General"); + attack(1, playerA, "Grizzly Bears", "Vivien, Champion of the Wilds"); + + setStrictChooseMode(true); + setStopAt(1, PhaseStep.END_TURN); + execute(); + assertAllCommandsUsed(); + + assertCounterCount(playerB, "Liliana, Dreadhorde General", CounterType.LOYALTY, 6 - 2); + assertCounterCount(playerB, "Vivien, Champion of the Wilds", CounterType.LOYALTY, 4 - 2); + assertLife(playerA, 20 - 2); + assertLife(playerB, 20 + 2); + } +} diff --git a/Mage/src/main/java/mage/game/combat/Combat.java b/Mage/src/main/java/mage/game/combat/Combat.java index 66ab9345441..990147a7830 100644 --- a/Mage/src/main/java/mage/game/combat/Combat.java +++ b/Mage/src/main/java/mage/game/combat/Combat.java @@ -1539,6 +1539,18 @@ public class Combat implements Serializable, Copyable { return false; } + public boolean isPlaneswalkerAttacked(UUID defenderId, Game game) { + for (CombatGroup group : groups) { + if (group.defenderIsPlaneswalker) { + Permanent permanent = game.getPermanent(group.getDefenderId()); + if (permanent.isControlledBy(defenderId)) { + return true; + } + } + } + return false; + } + /** * @param attackerId * @return uuid of defending player or planeswalker