move ARG regex helper into separate class, create MagicConditionParser to convert text into MagicCondition
parent
737880c06d
commit
484eeadd11
|
@ -0,0 +1,57 @@
|
||||||
|
package magic.model;
|
||||||
|
|
||||||
|
import magic.data.EnglishToInt;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
public class ARG {
|
||||||
|
public static final String NUMBER = "(?<number>[0-9]+)";
|
||||||
|
public static int number(final Matcher m) {
|
||||||
|
return Integer.parseInt(m.group("number"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String AMOUNT = "(?<amount>.+)";
|
||||||
|
public static int amount(final Matcher m) {
|
||||||
|
return EnglishToInt.convert(m.group("amount"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String COST = "(?<cost>.+)";
|
||||||
|
public static String cost(final Matcher m) {
|
||||||
|
return m.group("cost");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String EFFECT = "(?<effect>.+)";
|
||||||
|
public static String effect(final Matcher m) {
|
||||||
|
return m.group("effect");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String ANY = "(?<any>.+)";
|
||||||
|
public static String any(final Matcher m) {
|
||||||
|
return m.group("any");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String MANA = "(?<mana>[^\\.]+)";
|
||||||
|
public static String mana(final Matcher m) {
|
||||||
|
return m.group("mana");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String WORD1 = "(?<word1>[^ ]+)";
|
||||||
|
public static String word1(final Matcher m) {
|
||||||
|
return m.group("word1");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String WORD2 = "(?<word2>[^ ]+)";
|
||||||
|
public static String word2(final Matcher m) {
|
||||||
|
return m.group("word2");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String WORDRUN = "(?<wordrun>[^\\.]+)";
|
||||||
|
public static String wordrun(final Matcher m) {
|
||||||
|
return m.group("wordrun");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String PT = "(?<pt>[+-][0-9]+/[+-][0-9]+)";
|
||||||
|
public static String pt(final Matcher m) {
|
||||||
|
return m.group("pt");
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,7 +8,7 @@ import magic.model.target.MagicTargetFilter;
|
||||||
import magic.model.target.MagicTargetFilterFactory;
|
import magic.model.target.MagicTargetFilterFactory;
|
||||||
import magic.model.trigger.*;
|
import magic.model.trigger.*;
|
||||||
import magic.model.condition.MagicCondition;
|
import magic.model.condition.MagicCondition;
|
||||||
import magic.model.condition.MagicConditionFactory;
|
import magic.model.condition.MagicConditionParser;
|
||||||
import magic.model.trigger.MagicThiefTrigger.Player;
|
import magic.model.trigger.MagicThiefTrigger.Player;
|
||||||
import magic.model.trigger.MagicThiefTrigger.Type;
|
import magic.model.trigger.MagicThiefTrigger.Type;
|
||||||
|
|
||||||
|
@ -747,15 +747,9 @@ public enum MagicAbility {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ControlPumpGain("SN (gets " + ARG.PT + " )?(and )?(has " + ARG.ANY + " )?as long as you control (a(n)?|(?<another>another)) " + ARG.WORDRUN + "\\.", 0) {
|
ControlPumpGain("SN (gets " + ARG.PT + " )?(and )?(has " + ARG.ANY + " )?as long as " + ARG.WORDRUN + "\\.", 0) {
|
||||||
protected void addAbilityImpl(final MagicAbilityStore card, final Matcher arg) {
|
protected void addAbilityImpl(final MagicAbilityStore card, final Matcher arg) {
|
||||||
final MagicCondition condition = arg.group("another") != null ?
|
final MagicCondition condition = MagicConditionParser.build(ARG.wordrun(arg));
|
||||||
MagicConditionFactory.YouControlAnother(
|
|
||||||
MagicTargetFilterFactory.singlePermanent(ARG.wordrun(arg))
|
|
||||||
):
|
|
||||||
MagicConditionFactory.YouControl(
|
|
||||||
MagicTargetFilterFactory.singlePermanent(ARG.wordrun(arg))
|
|
||||||
);
|
|
||||||
if (arg.group("pt") != null) {
|
if (arg.group("pt") != null) {
|
||||||
final String[] pt = ARG.pt(arg).replace("+","").split("/");
|
final String[] pt = ARG.pt(arg).replace("+","").split("/");
|
||||||
final int power = Integer.parseInt(pt[0]);
|
final int power = Integer.parseInt(pt[0]);
|
||||||
|
@ -772,7 +766,7 @@ public enum MagicAbility {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ControlPumpGainAlt("As long as you control (a(n)?|(?<another>another)) " + ARG.WORDRUN + ", SN (gets " + ARG.PT + " )?(and )?(has " + ARG.ANY + ")?\\.", 0) {
|
ControlPumpGainAlt("As long as " + ARG.WORDRUN + ", SN (gets " + ARG.PT + " )?(and )?(has " + ARG.ANY + ")?\\.", 0) {
|
||||||
protected void addAbilityImpl(final MagicAbilityStore card, final Matcher arg) {
|
protected void addAbilityImpl(final MagicAbilityStore card, final Matcher arg) {
|
||||||
ControlPumpGain.addAbilityImpl(card, arg);
|
ControlPumpGain.addAbilityImpl(card, arg);
|
||||||
}
|
}
|
||||||
|
@ -1030,58 +1024,6 @@ public enum MagicAbility {
|
||||||
public static final Set<MagicAbility> LANDWALK_FLAGS = EnumSet.range(Plainswalk, Forestwalk);
|
public static final Set<MagicAbility> LANDWALK_FLAGS = EnumSet.range(Plainswalk, Forestwalk);
|
||||||
|
|
||||||
|
|
||||||
private static class ARG {
|
|
||||||
private static final String NUMBER = "(?<number>[0-9]+)";
|
|
||||||
private static int number(final Matcher m) {
|
|
||||||
return Integer.parseInt(m.group("number"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String AMOUNT = "(?<amount>.+)";
|
|
||||||
private static int amount(final Matcher m) {
|
|
||||||
return EnglishToInt.convert(m.group("amount"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String COST = "(?<cost>.+)";
|
|
||||||
private static String cost(final Matcher m) {
|
|
||||||
return m.group("cost");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String EFFECT = "(?<effect>.+)";
|
|
||||||
private static String effect(final Matcher m) {
|
|
||||||
return m.group("effect");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String ANY = "(?<any>.+)";
|
|
||||||
private static String any(final Matcher m) {
|
|
||||||
return m.group("any");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String MANA = "(?<mana>[^\\.]+)";
|
|
||||||
private static String mana(final Matcher m) {
|
|
||||||
return m.group("mana");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String WORD1 = "(?<word1>[^ ]+)";
|
|
||||||
private static String word1(final Matcher m) {
|
|
||||||
return m.group("word1");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String WORD2 = "(?<word2>[^ ]+)";
|
|
||||||
private static String word2(final Matcher m) {
|
|
||||||
return m.group("word2");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String WORDRUN = "(?<wordrun>[^\\.]+)";
|
|
||||||
private static String wordrun(final Matcher m) {
|
|
||||||
return m.group("wordrun");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final String PT = "(?<pt>[+-][0-9]+/[+-][0-9]+)";
|
|
||||||
private static String pt(final Matcher m) {
|
|
||||||
return m.group("pt");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private final Pattern pattern;
|
private final Pattern pattern;
|
||||||
private final String name;
|
private final String name;
|
||||||
private final int score;
|
private final int score;
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
package magic.model.condition;
|
||||||
|
|
||||||
|
import magic.model.ARG;
|
||||||
|
import magic.model.MagicSource;
|
||||||
|
import magic.model.target.MagicTargetFilterFactory;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public enum MagicConditionParser {
|
||||||
|
|
||||||
|
YouControl("you control a(n)? " + ARG.WORDRUN) {
|
||||||
|
public MagicCondition toCondition(final Matcher arg) {
|
||||||
|
return MagicConditionFactory.YouControl(
|
||||||
|
MagicTargetFilterFactory.singlePermanent(ARG.wordrun(arg))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
YouControlAnother("you control another " + ARG.WORDRUN) {
|
||||||
|
public MagicCondition toCondition(final Matcher arg) {
|
||||||
|
return MagicConditionFactory.YouControlAnother(
|
||||||
|
MagicTargetFilterFactory.singlePermanent(ARG.wordrun(arg))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Threshold("seven or more cards are in your graveyard") {
|
||||||
|
public MagicCondition toCondition(final Matcher arg) {
|
||||||
|
return MagicCondition.THRESHOLD_CONDITION;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Metalcraft("you control three or more artifacts") {
|
||||||
|
public MagicCondition toCondition(final Matcher arg) {
|
||||||
|
return MagicCondition.METALCRAFT_CONDITION;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private final Pattern pattern;
|
||||||
|
|
||||||
|
private MagicConditionParser(final String regex) {
|
||||||
|
pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Matcher matcher(final String rule) {
|
||||||
|
return pattern.matcher(rule);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract MagicCondition toCondition(final Matcher arg);
|
||||||
|
|
||||||
|
public static final MagicCondition build(final String cost) {
|
||||||
|
for (final MagicConditionParser rule : values()) {
|
||||||
|
final Matcher matcher = rule.matcher(cost);
|
||||||
|
if (matcher.matches()) {
|
||||||
|
return rule.toCondition(matcher);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new RuntimeException("Unable to match " + cost + " to a condition");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue