From fca084c44e1ffc29c462c67ceff6218742ff868d Mon Sep 17 00:00:00 2001 From: elcnesh Date: Mon, 18 Aug 2014 12:52:45 +0000 Subject: [PATCH] Cleanup code for plural type names (like Clerics), and fix them for type changing effects. --- .../src/main/java/forge/game/card/Card.java | 30 +++------ .../forge/game/card/CardChangedWords.java | 6 ++ .../main/java/forge/game/card/CardUtil.java | 66 +++++++++++++++---- 3 files changed, 69 insertions(+), 33 deletions(-) diff --git a/forge-game/src/main/java/forge/game/card/Card.java b/forge-game/src/main/java/forge/game/card/Card.java index 11fc262cf28..702136b38a2 100644 --- a/forge-game/src/main/java/forge/game/card/Card.java +++ b/forge-game/src/main/java/forge/game/card/Card.java @@ -2399,8 +2399,9 @@ public class Card extends GameEntity implements Comparable { for (final Entry e : Sets.union(this.changedTextColors.toMap().entrySet(), this.changedTextTypes.toMap().entrySet())) { - // only the upper case ones, to avoid duplicity - if (Character.isUpperCase(e.getKey().charAt(0))) { + // ignore lower case and plural form keys, to avoid duplicity + if (Character.isUpperCase(e.getKey().charAt(0)) && + !CardUtil.singularTypes.containsKey(e.getKey())) { sb.append("Text changed: all instances of "); if (e.getKey().equals("Any")) { if (this.changedTextColors.toMap().containsKey(e.getKey())) { @@ -8472,30 +8473,15 @@ public class Card extends GameEntity implements Comparable { if (source.isSpell() && !source.isColorless()) { return true; } - } else if (kw.equals("Protection from Dragons")) { - if (source.isType("Dragon")) { - return true; - } - } else if (kw.equals("Protection from Demons")) { - if (source.isType("Demon")) { - return true; - } - } else if (kw.equals("Protection from Goblins")) { - if (source.isType("Goblin")) { - return true; - } - } else if (kw.equals("Protection from Clerics")) { - if (source.isType("Cleric")) { - return true; - } - } else if (kw.equals("Protection from Gorgons")) { - if (source.isType("Gorgon")) { - return true; - } } else if (kw.equals("Protection from the chosen player")) { if (source.getController().equals(this.chosenPlayer)) { return true; } + } else if (kw.startsWith("Protection from ")) { + final String protectType = CardUtil.getSingularType(kw.substring("Protection from ".length())); + if (source.isType(protectType)) { + return true; + } } } } diff --git a/forge-game/src/main/java/forge/game/card/CardChangedWords.java b/forge-game/src/main/java/forge/game/card/CardChangedWords.java index e2e605bcfae..7ada078ea1d 100644 --- a/forge-game/src/main/java/forge/game/card/CardChangedWords.java +++ b/forge-game/src/main/java/forge/game/card/CardChangedWords.java @@ -61,6 +61,12 @@ public final class CardChangedWords { // the actual change (b->c) resultCache.put(ccw.getOriginalWord(), ccw.getNewWord()); + + // possible plural form + final String singular = CardUtil.getPluralType(ccw.getOriginalWord()); + if (!singular.equals(ccw.getOriginalWord())) { + resultCache.put(singular, ccw.getNewWord()); + } } for (final String key : ImmutableList.copyOf(resultCache.keySet())) { diff --git a/forge-game/src/main/java/forge/game/card/CardUtil.java b/forge-game/src/main/java/forge/game/card/CardUtil.java index 23856c58b55..7b408bd6cc8 100644 --- a/forge-game/src/main/java/forge/game/card/CardUtil.java +++ b/forge-game/src/main/java/forge/game/card/CardUtil.java @@ -17,6 +17,14 @@ */ package forge.game.card; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import com.google.common.collect.ImmutableBiMap; +import com.google.common.collect.ImmutableList; + import forge.ImageKeys; import forge.card.CardCharacteristicName; import forge.card.ColorSet; @@ -30,13 +38,6 @@ import forge.game.spellability.AbilitySub; import forge.game.spellability.SpellAbility; import forge.game.zone.ZoneType; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import com.google.common.collect.Lists; - public final class CardUtil { // disable instantiation private CardUtil() { } @@ -46,17 +47,34 @@ public final class CardUtil { /** List of all keywords that could be modified by text changes. * Mostly this is caused by them having a variable, like a cost. */ - public static final List modifiableKeywords = Lists.newArrayList( + public static final ImmutableList modifiableKeywords = ImmutableList.builder().add( "Enchant", "Protection", "Cumulative upkeep", "Equip", "Buyback", "Cycling", "Echo", "Kicker", "Flashback", "Madness", "Morph", "Affinity", "Entwine", "Splice", "Ninjutsu", "Transute", "Replicate", "Recover", "Suspend", "Aura swap", "Fortify", "Transfigure", "Champion", "Evoke", "Prowl", "Reinforce", "Unearth", "Level up", "Miracle", "Overload", - "Scavenge", "Bestow"); + "Scavenge", "Bestow").build(); /** List of keyword endings of keywords that could be modified by text changes. */ - public static final List modifiableKeywordEndings = Lists.newArrayList("walk", "cycling", - "offering"); + public static final ImmutableList modifiableKeywordEndings = ImmutableList.builder().add( + "walk", "cycling", "offering").build(); + + /** + * Map of plural type names to the corresponding singular form. + * So Clerics maps to Cleric, Demons to Demon, etc. + */ + public static final ImmutableBiMap singularTypes = ImmutableBiMap.builder() + .put("Clerics", "Cleric") + .put("Demons", "Demon") + .put("Dragons", "Dragon") + .put("Goblins", "Goblin") + .put("Gorgons", "Gorgon") + .build(); + /** + * Map of singular type names to the corresponding plural form. + * So Cleric maps to Clerics, Demon to Demons, etc. + */ + public static final ImmutableBiMap pluralTypes = singularTypes.inverse(); public static final boolean isKeywordModifiable(final String kw) { for (final String modKw : modifiableKeywords) { @@ -72,6 +90,32 @@ public final class CardUtil { return false; } + /** + * If the input is a plural type, return the corresponding singular form. + * Otherwise, simply return the input. + * @param type a String. + * @return the corresponding type. + */ + public static final String getSingularType(final String type) { + if (singularTypes.containsKey(type)) { + return singularTypes.get(type); + } + return type; + } + + /** + * If the input is a singular type, return the corresponding plural form. + * Otherwise, simply return the input. + * @param type a String. + * @return the corresponding type. + */ + public static final String getPluralType(final String type) { + if (pluralTypes.containsKey(type)) { + return pluralTypes.get(type); + } + return type; + } + public static ColorSet getColors(final Card c) { return c.determineColor(); }