From 0c2e08f54ee9cd3552e09be69ed55d9a336842c1 Mon Sep 17 00:00:00 2001 From: Oleg Agafonov Date: Mon, 18 May 2020 06:46:39 +0400 Subject: [PATCH] * UI: choose spell to cast dialog - added card name for spell abilities (split, adventure, additional spell, etc, see #6549); --- .../java/mage/view/AbilityPickerView.java | 22 ++++- .../test/serverside/AbilityPickerTest.java | 87 +++++++++++++++++++ 2 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 Mage.Tests/src/test/java/org/mage/test/serverside/AbilityPickerTest.java diff --git a/Mage.Common/src/main/java/mage/view/AbilityPickerView.java b/Mage.Common/src/main/java/mage/view/AbilityPickerView.java index 8ea265aee0..7cb356d20b 100644 --- a/Mage.Common/src/main/java/mage/view/AbilityPickerView.java +++ b/Mage.Common/src/main/java/mage/view/AbilityPickerView.java @@ -1,6 +1,7 @@ package mage.view; import mage.abilities.Ability; +import mage.abilities.SpellAbility; import java.io.Serializable; import java.util.LinkedHashMap; @@ -28,15 +29,30 @@ public class AbilityPickerView implements Serializable { if (objectName == null) { rule = ability.getRule(true); } else { - rule = ability.getRule(objectName); - if (rule.isEmpty()) { - rule = ability.toString(); + // spell abilities must start with "Cast name" (split cards have different names for each spell part) + if (ability instanceof SpellAbility) { + SpellAbility spell = (SpellAbility) ability; + rule = getAbilityRules(spell, spell.getCardName()); + if (!rule.startsWith("Cast ")) { + rule = spell.toString() + ": " + rule; // spell.toString() must return this.name (example: Cast Armed) + } + } else { + rule = getAbilityRules(ability, objectName); } } choices.put(ability.getId(), num + ". " + rule); } } + private String getAbilityRules(Ability ability, String objectName) { + String rule = ability.getRule(objectName); + if (rule.isEmpty()) { + rule = ability.toString(); + } + rule = Character.toUpperCase(rule.charAt(0)) + rule.substring(1); + return rule; + } + public AbilityPickerView(Map modes, String message) { this.choices = modes; this.message = message; diff --git a/Mage.Tests/src/test/java/org/mage/test/serverside/AbilityPickerTest.java b/Mage.Tests/src/test/java/org/mage/test/serverside/AbilityPickerTest.java new file mode 100644 index 0000000000..a064f280e4 --- /dev/null +++ b/Mage.Tests/src/test/java/org/mage/test/serverside/AbilityPickerTest.java @@ -0,0 +1,87 @@ +package org.mage.test.serverside; + +import mage.abilities.Abilities; +import mage.abilities.Ability; +import mage.cards.repository.CardInfo; +import mage.cards.repository.CardRepository; +import mage.game.permanent.PermanentCard; +import mage.game.permanent.PermanentImpl; +import mage.view.AbilityPickerView; +import org.junit.Assert; +import org.junit.Test; +import org.mage.test.serverside.base.CardTestPlayerBase; + +/** + * @author JayDi85 + */ +public class AbilityPickerTest extends CardTestPlayerBase { + + @Test + public void test_PickerChoices_FusedSpells() { + // must be 3 spells for choices + Abilities abilities = getAbilitiesFromCard("Armed // Dangerous"); + Assert.assertEquals(3, abilities.size()); + + AbilityPickerView view = new AbilityPickerView("test name", abilities, "test message"); + Assert.assertEquals(3, view.getChoices().size()); + view.getChoices().values().forEach(c -> { + Assert.assertTrue("Must start with Cast text, but found: " + c, c.contains("Cast ")); + }); + } + + @Test + public void test_PickerChoices_AdventureSpells() { + // must be 2 spells for choices and 1 static ability + Abilities abilities = getAbilitiesFromCard("Foulmire Knight"); + Assert.assertEquals(3, abilities.size()); + + AbilityPickerView view = new AbilityPickerView("test name", abilities, "test message"); + Assert.assertEquals(3, view.getChoices().size()); + view.getChoices().values().forEach(c -> { + if (c.contains("Deathtouch")) { + return; + } + Assert.assertTrue("Must start with Cast text, but found: " + c, c.contains("Cast ")); + }); + } + + @Test + public void test_PickerChoices_ActivatedAbilities() { + // must be 1 cast + 3 abilities + Abilities abilities = getAbilitiesFromCard("Dimir Cluestone"); + Assert.assertEquals(4, abilities.size()); + + AbilityPickerView view = new AbilityPickerView("test name", abilities, "test message"); + Assert.assertEquals(4, view.getChoices().size()); + int castCount = 0; + int abilsCount = 0; + for (String c : view.getChoices().values()) { + if (c.contains("Cast ")) { + castCount++; + } else { + abilsCount++; + } + } + Assert.assertEquals(1, castCount); + Assert.assertEquals(3, abilsCount); + } + + @Test + public void test_PickerChoices_AdditionalSpells() { + // must be 2 cast + Abilities abilities = getAbilitiesFromCard("Cling to Dust"); + Assert.assertEquals(2, abilities.size()); + + AbilityPickerView view = new AbilityPickerView("test name", abilities, "test message"); + Assert.assertEquals(2, view.getChoices().size()); + view.getChoices().values().forEach(c -> { + Assert.assertTrue("Must start with Cast text, but found: " + c, c.contains("Cast ")); + }); + } + + private Abilities getAbilitiesFromCard(String cardName) { + CardInfo info = CardRepository.instance.findCard(cardName); + PermanentImpl permanent = new PermanentCard(info.getCard(), playerA.getId(), currentGame); + return permanent.getAbilities(currentGame); + } +}