consolidate duplicate parsing regex using named capturing groups

master
melvin 2013-10-15 16:38:09 +08:00
parent f14edf5be6
commit dfe5891df6
6 changed files with 71 additions and 149 deletions

View File

@ -103,7 +103,7 @@ public abstract class MagicPermanentActivation extends MagicActivation<MagicPerm
final String effect = text.toLowerCase();
final MagicRuleEventAction ruleAction = MagicRuleEventAction.build(effect);
final MagicEventAction action = ruleAction.action;
final MagicEventAction action = ruleAction.getAction(effect);
final MagicTargetPicker<?> picker = ruleAction.picker;
final MagicChoice choice = ruleAction.getChoice(effect);

View File

@ -98,150 +98,48 @@ public enum MagicRuleEventAction {
}
}
),
Deals1(
"sn deals 1 damage to (?<choice>[^\\.]*).",
Deals(
"sn deals (?<amount>[0-9]+) damage to (?<choice>[^\\.]*).",
MagicTargetHint.Negative,
new MagicDamageTargetPicker(1),
MagicTiming.Removal,
"Damage",
new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
event.processTarget(game,new MagicTargetAction() {
public void doAction(final MagicTarget target) {
final MagicDamage damage=new MagicDamage(event.getSource(),target,1);
game.doAction(new MagicDealDamageAction(damage));
}
});
}
null
) {
public MagicEventAction getAction(final String rule) {
final Matcher matcher = matched(rule);
final int amount = Integer.parseInt(matcher.group("amount"));
return new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
event.processTarget(game,new MagicTargetAction() {
public void doAction(final MagicTarget target) {
final MagicDamage damage=new MagicDamage(event.getSource(),target,amount);
game.doAction(new MagicDealDamageAction(damage));
}
});
}
};
}
),
Deals2(
"sn deals 2 damage to (?<choice>[^\\.]*).",
MagicTargetHint.Negative,
new MagicDamageTargetPicker(2),
MagicTiming.Removal,
"Damage",
new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
event.processTarget(game,new MagicTargetAction() {
public void doAction(final MagicTarget target) {
final MagicDamage damage=new MagicDamage(event.getSource(),target,2);
game.doAction(new MagicDealDamageAction(damage));
}
});
}
},
Draw("(pn )?draw(s)? (?<amount>[a-z]+) card(s)?.", MagicTiming.Draw, "Draw", null) {
public MagicEventAction getAction(final String rule) {
final Matcher matcher = matched(rule);
final int amount = MagicRuleEventAction.englishNumToInt(matcher.group("amount"));
return new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
game.doAction(new MagicDrawAction(event.getPlayer(), amount));
}
};
}
),
Deals3(
"sn deals 3 damage to (?<choice>[^\\.]*).",
MagicTargetHint.Negative,
new MagicDamageTargetPicker(3),
MagicTiming.Removal,
"Damage",
new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
event.processTarget(game,new MagicTargetAction() {
public void doAction(final MagicTarget target) {
final MagicDamage damage=new MagicDamage(event.getSource(),target,3);
game.doAction(new MagicDealDamageAction(damage));
}
});
}
}
),
Deals4(
"sn deals 4 damage to (?<choice>[^\\.]*).",
MagicTargetHint.Negative,
new MagicDamageTargetPicker(4),
MagicTiming.Removal,
"Damage",
new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
event.processTarget(game,new MagicTargetAction() {
public void doAction(final MagicTarget target) {
final MagicDamage damage=new MagicDamage(event.getSource(),target,4);
game.doAction(new MagicDealDamageAction(damage));
}
});
}
}
),
Deals5(
"sn deals 5 damage to (?<choice>[^\\.]*).",
MagicTargetHint.Negative,
new MagicDamageTargetPicker(5),
MagicTiming.Removal,
"Damage",
new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
event.processTarget(game,new MagicTargetAction() {
public void doAction(final MagicTarget target) {
final MagicDamage damage=new MagicDamage(event.getSource(),target,5);
game.doAction(new MagicDealDamageAction(damage));
}
});
}
}
),
DrawsACard("pn draws a card.", MagicTiming.Draw, "Draw", new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
game.doAction(new MagicDrawAction(event.getPlayer(), 1));
}
}),
DrawsTwoCards("pn draws two cards.", MagicTiming.Draw, "Draw", new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
game.doAction(new MagicDrawAction(event.getPlayer(), 2));
}
}),
DrawsThreeCards("pn draws three cards.", MagicTiming.Draw, "Draw", new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
game.doAction(new MagicDrawAction(event.getPlayer(), 3));
}
}),
DrawsFourCards("pn draws four cards.", MagicTiming.Draw, "Draw", new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
game.doAction(new MagicDrawAction(event.getPlayer(), 4));
}
}),
DrawACard("draw a card.", MagicTiming.Draw, "Draw", new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
game.doAction(new MagicDrawAction(event.getPlayer(), 1));
}
}),
DrawTwoCards("draw two cards.", MagicTiming.Draw, "Draw", new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
game.doAction(new MagicDrawAction(event.getPlayer(), 2));
}
}),
DrawThreeCards("draw three cards.", MagicTiming.Draw, "Draw", new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
game.doAction(new MagicDrawAction(event.getPlayer(), 3));
}
}),
DrawFourCards("draw four cards.", MagicTiming.Draw, "Draw", new MagicEventAction() {
@Override
public void executeEvent(final MagicGame game, final MagicEvent event) {
game.doAction(new MagicDrawAction(event.getPlayer(), 4));
}
}),
},
;
private final Pattern pattern;
private final MagicTargetHint hint;
private final MagicEventAction action;
public final MagicEventAction action;
public final MagicTargetPicker<?> picker;
public final MagicTiming timing;
public final String description;
@ -269,15 +167,17 @@ public enum MagicRuleEventAction {
return pattern.matcher(rule).matches();
}
public MagicEventAction getAction(final String rule) {
return action;
}
public MagicChoice getChoice(final String rule) {
final Matcher matcher = pattern.matcher(rule);
final boolean matches = matcher.matches();
if (!matches) {
throw new RuntimeException("unknown rule: " + rule);
final Matcher matcher = matched(rule);
try {
return new MagicTargetChoice(hint, matcher.group("choice"));
} catch (IllegalArgumentException e) {
return MagicChoice.NONE;
}
return (matcher.groupCount() > 0) ?
new MagicTargetChoice(hint, matcher.group("choice")) :
MagicChoice.NONE;
}
public static MagicRuleEventAction build(final String rule) {
@ -288,4 +188,26 @@ public enum MagicRuleEventAction {
}
throw new RuntimeException("unknown rule: " + rule);
}
protected Matcher matched(final String rule) {
final Matcher matcher = pattern.matcher(rule);
final boolean matches = matcher.matches();
if (!matches) {
throw new RuntimeException("unknown rule: " + rule);
}
return matcher;
}
public static int englishNumToInt(String num) {
switch (num) {
case "a": return 1;
case "two": return 2;
case "three" : return 3;
case "four" : return 4;
case "five" : return 5;
case "six" : return 6;
case "seven" : return 7;
default: throw new RuntimeException("Unknown word " + num);
}
}
}

View File

@ -18,7 +18,7 @@ public abstract class MagicSpellCardEvent implements MagicCardEvent,MagicEventAc
public static MagicSpellCardEvent create(final String rule) {
final String effect = rule.toLowerCase();
final MagicRuleEventAction ruleAction = MagicRuleEventAction.build(effect);
final MagicEventAction action = ruleAction.action;
final MagicEventAction action = ruleAction.getAction(effect);
final MagicTargetPicker<?> picker = ruleAction.picker;
final MagicChoice choice = ruleAction.getChoice(effect);

View File

@ -37,7 +37,7 @@ public abstract class MagicWhenComesIntoPlayTrigger extends MagicTrigger<MagicPa
public static MagicWhenComesIntoPlayTrigger createMay(final String rule) {
final String effect = rule.toLowerCase();
final MagicRuleEventAction ruleAction = MagicRuleEventAction.build(effect);
final MagicEventAction action = ruleAction.action;
final MagicEventAction action = ruleAction.getAction(effect);
final MagicTargetPicker<?> picker = ruleAction.picker;
final MagicChoice choice = ruleAction.getChoice(effect);
@ -64,7 +64,7 @@ public abstract class MagicWhenComesIntoPlayTrigger extends MagicTrigger<MagicPa
public static MagicWhenComesIntoPlayTrigger create(final String rule) {
final String effect = rule.toLowerCase();
final MagicRuleEventAction ruleAction = MagicRuleEventAction.build(effect);
final MagicEventAction action = ruleAction.action;
final MagicEventAction action = ruleAction.getAction(effect);
final MagicTargetPicker<?> picker = ruleAction.picker;
final MagicChoice choice = ruleAction.getChoice(effect);

View File

@ -31,7 +31,7 @@ public abstract class MagicWhenDiesTrigger extends MagicWhenPutIntoGraveyardTrig
public static MagicWhenDiesTrigger createMay(final String rule) {
final String effect = rule.toLowerCase();
final MagicRuleEventAction ruleAction = MagicRuleEventAction.build(effect);
final MagicEventAction action = ruleAction.action;
final MagicEventAction action = ruleAction.getAction(effect);
final MagicTargetPicker<?> picker = ruleAction.picker;
final MagicChoice choice = ruleAction.getChoice(effect);
@ -58,7 +58,7 @@ public abstract class MagicWhenDiesTrigger extends MagicWhenPutIntoGraveyardTrig
public static MagicWhenDiesTrigger create(final String rule) {
final String effect = rule.toLowerCase();
final MagicRuleEventAction ruleAction = MagicRuleEventAction.build(effect);
final MagicEventAction action = ruleAction.action;
final MagicEventAction action = ruleAction.getAction(effect);
final MagicTargetPicker<?> picker = ruleAction.picker;
final MagicChoice choice = ruleAction.getChoice(effect);

View File

@ -36,7 +36,7 @@ public abstract class MagicWhenLeavesPlayTrigger extends MagicTrigger<MagicRemov
public static MagicWhenLeavesPlayTrigger createMay(final String rule) {
final String effect = rule.toLowerCase();
final MagicRuleEventAction ruleAction = MagicRuleEventAction.build(effect);
final MagicEventAction action = ruleAction.action;
final MagicEventAction action = ruleAction.getAction(effect);
final MagicTargetPicker<?> picker = ruleAction.picker;
final MagicChoice choice = ruleAction.getChoice(effect);
@ -63,7 +63,7 @@ public abstract class MagicWhenLeavesPlayTrigger extends MagicTrigger<MagicRemov
public static MagicWhenLeavesPlayTrigger create(final String rule) {
final String effect = rule.toLowerCase();
final MagicRuleEventAction ruleAction = MagicRuleEventAction.build(effect);
final MagicEventAction action = ruleAction.action;
final MagicEventAction action = ruleAction.getAction(effect);
final MagicTargetPicker<?> picker = ruleAction.picker;
final MagicChoice choice = ruleAction.getChoice(effect);