introduce concept of MagicAmount to determine a number from a MagicSource and associated MagicAmountFactory and MagicAmountParser
parent
ce604aa124
commit
1ac3a6edde
|
@ -611,12 +611,12 @@ public enum MagicAbility {
|
|||
card.add(MagicAtYourUpkeepTrigger.kinship(effect, MagicRuleEventAction.create(effect).getAction()));
|
||||
}
|
||||
},
|
||||
CDAPT("SN's power and toughness are each equal to (" + ARG.NUMBER + " plus )?the number of " + ARG.ANY + "\\.", 0) {
|
||||
CDAPT("SN's power and toughness are each equal to( " + ARG.NUMBER + " plus)? " + ARG.ANY + "\\.", 0) {
|
||||
protected void addAbilityImpl(final MagicAbilityStore card, final Matcher arg) {
|
||||
final int base = (arg.group("number") != null) ? ARG.number(arg) : 0;
|
||||
card.add(MagicCDA.setPT(
|
||||
base,
|
||||
MagicTargetFilterFactory.Target(ARG.any(arg))
|
||||
MagicAmountParser.build(ARG.any(arg))
|
||||
));
|
||||
}
|
||||
},
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package magic.model;
|
||||
|
||||
public interface MagicAmount {
|
||||
int getAmount(final MagicSource source);
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package magic.model;
|
||||
|
||||
import magic.model.target.MagicTargetFilter;
|
||||
import magic.model.target.MagicTarget;
|
||||
|
||||
public class MagicAmountFactory {
|
||||
|
||||
public static MagicAmount FromFilter(final MagicTargetFilter<MagicTarget> filter) {
|
||||
return new MagicAmount() {
|
||||
@Override
|
||||
public int getAmount(final MagicSource source) {
|
||||
return filter.filter(source).size();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static MagicAmount CounterOnSource(final MagicCounterType type) {
|
||||
return new MagicAmount() {
|
||||
@Override
|
||||
public int getAmount(final MagicSource source) {
|
||||
final MagicPermanent perm = (MagicPermanent)source;
|
||||
return perm.getCounters(type);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package magic.model;
|
||||
|
||||
import magic.model.ARG;
|
||||
import magic.model.MagicCounterType;
|
||||
import magic.model.MagicAbility;
|
||||
import magic.model.target.MagicTargetFilterFactory;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public enum MagicAmountParser {
|
||||
|
||||
YourLife("your life total") {
|
||||
public MagicAmount toAmount(final Matcher arg) {
|
||||
return new MagicAmount() {
|
||||
@Override
|
||||
public int getAmount(final MagicSource source) {
|
||||
return source.getController().getLife();
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
FromFilter("the number of " + ARG.ANY) {
|
||||
public MagicAmount toAmount(final Matcher arg) {
|
||||
return MagicAmountFactory.FromFilter(
|
||||
MagicTargetFilterFactory.Target(ARG.any(arg))
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
private final Pattern pattern;
|
||||
|
||||
private MagicAmountParser(final String regex) {
|
||||
pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
|
||||
}
|
||||
|
||||
public Matcher matcher(final String rule) {
|
||||
return pattern.matcher(rule);
|
||||
}
|
||||
|
||||
public abstract MagicAmount toAmount(final Matcher arg);
|
||||
|
||||
public static final MagicAmount build(final String text) {
|
||||
for (final MagicAmountParser rule : values()) {
|
||||
final Matcher matcher = rule.matcher(text);
|
||||
if (matcher.matches()) {
|
||||
return rule.toAmount(matcher);
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("unknown amount \"" + text + "\"");
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ import magic.model.target.MagicTarget;
|
|||
import magic.model.target.MagicTargetFilter;
|
||||
import magic.model.target.MagicTargetType;
|
||||
|
||||
public class MagicPlayer extends MagicObjectImpl implements MagicTarget, MagicMappable<MagicPlayer> {
|
||||
public class MagicPlayer extends MagicObjectImpl implements MagicSource, MagicTarget, MagicMappable<MagicPlayer> {
|
||||
|
||||
public static final MagicPlayer NONE = new MagicPlayer(-1, null, -1) {
|
||||
@Override
|
||||
|
|
|
@ -7,6 +7,7 @@ import magic.model.MagicPermanent;
|
|||
import magic.model.MagicPlayer;
|
||||
import magic.model.MagicPowerToughness;
|
||||
import magic.model.MagicSubType;
|
||||
import magic.model.MagicAmount;
|
||||
import magic.model.target.MagicTargetFilter;
|
||||
import magic.model.target.MagicTarget;
|
||||
|
||||
|
@ -42,6 +43,16 @@ public abstract class MagicCDA implements MagicChangeCardDefinition {
|
|||
};
|
||||
}
|
||||
|
||||
public static MagicCDA setPT(final int base, final MagicAmount count) {
|
||||
return new MagicCDA() {
|
||||
@Override
|
||||
public void modPowerToughness(final MagicGame game,final MagicPlayer player,final MagicPowerToughness pt) {
|
||||
final int amount = count.getAmount(player);
|
||||
pt.set(base + amount, base + amount);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static MagicCDA setPower(final int base, final MagicTargetFilter<MagicTarget> filter) {
|
||||
return new MagicCDA() {
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue