From 7e08b3c3d5e84bb4e6429b21ff00e2c44088088b Mon Sep 17 00:00:00 2001 From: Oleg Agafonov Date: Wed, 26 Jun 2019 21:40:17 +0400 Subject: [PATCH] Test framework: added support to create custom instant/sorcery; --- .../serverside/base/MageTestPlayerBase.java | 64 +++++++++++++++---- Mage/src/main/java/mage/cards/CardImpl.java | 13 ++++ 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/Mage.Tests/src/test/java/org/mage/test/serverside/base/MageTestPlayerBase.java b/Mage.Tests/src/test/java/org/mage/test/serverside/base/MageTestPlayerBase.java index 45eda5e7fe5..18da4cd1033 100644 --- a/Mage.Tests/src/test/java/org/mage/test/serverside/base/MageTestPlayerBase.java +++ b/Mage.Tests/src/test/java/org/mage/test/serverside/base/MageTestPlayerBase.java @@ -3,6 +3,7 @@ package org.mage.test.serverside.base; import mage.abilities.Abilities; import mage.abilities.AbilitiesImpl; import mage.abilities.Ability; +import mage.abilities.SpellAbility; import mage.cards.Card; import mage.cards.CardImpl; import mage.cards.CardSetInfo; @@ -23,6 +24,7 @@ import mage.server.util.config.Plugin; import mage.util.Copier; import org.apache.log4j.Level; import org.apache.log4j.Logger; +import org.junit.Assert; import org.junit.BeforeClass; import org.mage.test.player.TestComputerPlayer; import org.mage.test.player.TestPlayer; @@ -356,13 +358,45 @@ public abstract class MageTestPlayerBase { if (playerD != null) playerD.setChooseStrictMode(enable); } + protected void addCustomCardWithSpell(TestPlayer controllerPlayer, SpellAbility spellAbility, Ability extraAbility, CardType cardType) { + addCustomCardWithSpell(spellAbility.getCardName(), controllerPlayer, spellAbility, extraAbility, cardType); + } + + protected void addCustomCardWithSpell(String customName, TestPlayer controllerPlayer, SpellAbility spellAbility, Ability extraAbility, CardType cardType) { + addCustomCardWithAbility(customName, controllerPlayer, extraAbility, spellAbility, cardType, spellAbility.getManaCostsToPay().getText(), spellAbility.getZone()); + } + protected void addCustomCardWithAbility(String customName, TestPlayer controllerPlayer, Ability ability) { - // add custom card with selected ability to battlefield + addCustomCardWithAbility(customName, controllerPlayer, ability, null, CardType.ENCHANTMENT, "", Zone.BATTLEFIELD); + } + + protected void addCustomCardWithAbility(String customName, TestPlayer controllerPlayer, Ability ability, SpellAbility spellAbility, + CardType cardType, String spellCost, Zone putAtZone) { CustomTestCard.clearCustomAbilities(customName); - CustomTestCard.addCustomAbility(customName, ability); + CustomTestCard.addCustomAbility(customName, spellAbility, ability); + CardSetInfo testSet = new CardSetInfo(customName, "custom", "123", Rarity.COMMON); - PermanentCard card = new PermanentCard(new CustomTestCard(controllerPlayer.getId(), testSet), controllerPlayer.getId(), currentGame); - getBattlefieldCards(controllerPlayer).add(card); + PermanentCard card = new PermanentCard(new CustomTestCard(controllerPlayer.getId(), testSet, cardType, spellCost), controllerPlayer.getId(), currentGame); + + switch (putAtZone) { + case BATTLEFIELD: + getBattlefieldCards(controllerPlayer).add(card); + break; + case GRAVEYARD: + getGraveCards(controllerPlayer).add(card); + break; + case HAND: + getHandCards(controllerPlayer).add(card); + break; + case LIBRARY: + getLibraryCards(controllerPlayer).add(card); + break; + case COMMAND: + getCommandCards(controllerPlayer).add(card); + break; + default: + Assert.fail("Unsupported zone: " + putAtZone); + } } } @@ -370,28 +404,30 @@ public abstract class MageTestPlayerBase { class CustomTestCard extends CardImpl { static private Map> abilitiesList = new HashMap<>(); // card name -> abilities + static private Map spellAbilitiesList = new HashMap<>(); // card name -> spell ability - static protected void addCustomAbility(String cardName, Ability ability) { + static void addCustomAbility(String cardName, SpellAbility spellAbility, Ability ability) { if (!abilitiesList.containsKey(cardName)) { abilitiesList.put(cardName, new AbilitiesImpl<>()); } Abilities oldAbilities = abilitiesList.get(cardName); - oldAbilities.add(ability); + if (ability != null) oldAbilities.add(ability); + + spellAbilitiesList.put(cardName, spellAbility); } - static protected void clearCustomAbilities(String cardName) { + static void clearCustomAbilities(String cardName) { abilitiesList.remove(cardName); + spellAbilitiesList.remove(cardName); } - static public void clearCustomAbilities() { - abilitiesList.clear(); - } - - - public CustomTestCard(UUID ownerId, CardSetInfo setInfo) { - super(ownerId, setInfo, new CardType[]{CardType.ENCHANTMENT}, ""); + CustomTestCard(UUID ownerId, CardSetInfo setInfo, CardType cardType, String spellCost) { + super(ownerId, setInfo, new CardType[]{cardType}, spellCost); // load dynamic abilities by card name + if (spellAbilitiesList.containsKey(setInfo.getName())) { + this.replaceSpellAbility(spellAbilitiesList.get(setInfo.getName())); + } Abilities extraAbitilies = abilitiesList.get(setInfo.getName()); if (extraAbitilies != null) { for (Ability ability : extraAbitilies) { diff --git a/Mage/src/main/java/mage/cards/CardImpl.java b/Mage/src/main/java/mage/cards/CardImpl.java index 317bec4adda..d903a30b0b3 100644 --- a/Mage/src/main/java/mage/cards/CardImpl.java +++ b/Mage/src/main/java/mage/cards/CardImpl.java @@ -335,6 +335,19 @@ public abstract class CardImpl extends MageObjectImpl implements Card { ability.addWatcher(watcher); } + public void replaceSpellAbility(SpellAbility newAbility) { + SpellAbility oldAbility = this.getSpellAbility(); + while (oldAbility != null) { + abilities.remove(oldAbility); + spellAbility = null; + oldAbility = this.getSpellAbility(); + } + + if (newAbility != null) { + addAbility(newAbility); + } + } + @Override public SpellAbility getSpellAbility() { if (spellAbility == null) {