From 28fe29854c3ae5b0ec3f09688b5c527755dc6bb5 Mon Sep 17 00:00:00 2001 From: North Date: Sun, 22 Jul 2012 22:36:03 +0300 Subject: [PATCH] [filters] converted FilterPlayer to Predicates --- .../src/mage/sets/magic2010/SafePassage.java | 3 +- Mage/src/mage/filter/FilterPlayer.java | 52 +----------- .../common/FilterPlaneswalkerOrPlayer.java | 73 +++++++++-------- .../predicate/other/PlayerIdPredicate.java | 56 +++++++++++++ .../predicate/other/PlayerPredicate.java | 82 +++++++++++++++++++ .../mage/target/common/TargetOpponent.java | 17 ++-- 6 files changed, 190 insertions(+), 93 deletions(-) create mode 100644 Mage/src/mage/filter/predicate/other/PlayerIdPredicate.java create mode 100644 Mage/src/mage/filter/predicate/other/PlayerPredicate.java diff --git a/Mage.Sets/src/mage/sets/magic2010/SafePassage.java b/Mage.Sets/src/mage/sets/magic2010/SafePassage.java index cd792dccc9..75ed4f2710 100644 --- a/Mage.Sets/src/mage/sets/magic2010/SafePassage.java +++ b/Mage.Sets/src/mage/sets/magic2010/SafePassage.java @@ -36,6 +36,7 @@ import mage.Constants.TargetController; import mage.abilities.effects.common.PreventAllDamageToEffect; import mage.cards.CardImpl; import mage.filter.common.FilterCreatureOrPlayer; +import mage.filter.predicate.other.PlayerPredicate; import mage.filter.predicate.permanent.ControllerPredicate; /** @@ -48,7 +49,7 @@ public class SafePassage extends CardImpl { static { filter.getCreatureFilter().add(new ControllerPredicate(TargetController.YOU)); - filter.getPlayerFilter().setPlayerTarget(TargetController.YOU); + filter.getPlayerFilter().add(new PlayerPredicate(TargetController.YOU)); } public SafePassage(UUID ownerId) { diff --git a/Mage/src/mage/filter/FilterPlayer.java b/Mage/src/mage/filter/FilterPlayer.java index b14d1a2c7c..ef4890477c 100644 --- a/Mage/src/mage/filter/FilterPlayer.java +++ b/Mage/src/mage/filter/FilterPlayer.java @@ -31,7 +31,6 @@ package mage.filter; import java.util.ArrayList; import java.util.List; import java.util.UUID; -import mage.Constants.TargetController; import mage.filter.predicate.ObjectPlayer; import mage.filter.predicate.ObjectPlayerPredicate; import mage.filter.predicate.ObjectSourcePlayer; @@ -47,9 +46,6 @@ import mage.players.Player; public class FilterPlayer extends FilterImpl { protected List>> extraPredicates = new ArrayList>>(); - protected List playerId = new ArrayList(); - protected boolean notPlayer; - protected TargetController playerTarget = TargetController.ANY; public FilterPlayer() { this("player"); @@ -62,58 +58,18 @@ public class FilterPlayer extends FilterImpl { public FilterPlayer(final FilterPlayer filter) { super(filter); this.extraPredicates = new ArrayList>>(filter.extraPredicates); - this.playerId.addAll(filter.playerId); - this.notPlayer = filter.notPlayer; - this.playerTarget = filter.playerTarget; } public void add(ObjectPlayerPredicate predicate) { extraPredicates.add(predicate); } - @Override - public boolean match(Player player, Game game) { - - if (playerId.size() > 0 && playerId.contains(player.getId()) == notPlayer) - return notFilter; - - return !notFilter; - } - - public boolean match(Player player, UUID sourceId, UUID controllerId, Game game) { - if (!this.match(player, game)) - return notFilter; - - if (playerTarget != TargetController.ANY && controllerId != null) { - switch(playerTarget) { - case YOU: - if (!player.getId().equals(controllerId)) - return notFilter; - break; - case OPPONENT: - if (!game.getOpponents(controllerId).contains(player.getId())) - return notFilter; - break; - case NOT_YOU: - if (player.getId().equals(controllerId)) - return notFilter; - break; - } + public boolean match(Player player, UUID sourceId, UUID playerId, Game game) { + if (!this.match(player, game)) { + return false; } - return Predicates.and(extraPredicates).apply(new ObjectSourcePlayer(player, sourceId, controllerId), game); - } - - public List getPlayerId() { - return playerId; - } - - public void setNotPlayer(boolean notPlayer) { - this.notPlayer = notPlayer; - } - - public void setPlayerTarget(TargetController playerTarget) { - this.playerTarget = playerTarget; + return Predicates.and(extraPredicates).apply(new ObjectSourcePlayer(player, sourceId, playerId), game); } @Override diff --git a/Mage/src/mage/filter/common/FilterPlaneswalkerOrPlayer.java b/Mage/src/mage/filter/common/FilterPlaneswalkerOrPlayer.java index 1bb5f97dc1..933002e745 100644 --- a/Mage/src/mage/filter/common/FilterPlaneswalkerOrPlayer.java +++ b/Mage/src/mage/filter/common/FilterPlaneswalkerOrPlayer.java @@ -1,31 +1,30 @@ /* -* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. -* -* Redistribution and use in source and binary forms, with or without modification, are -* permitted provided that the following conditions are met: -* -* 1. Redistributions of source code must retain the above copyright notice, this list of -* conditions and the following disclaimer. -* -* 2. Redistributions in binary form must reproduce the above copyright notice, this list -* of conditions and the following disclaimer in the documentation and/or other materials -* provided with the distribution. -* -* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED -* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR -* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -* -* The views and conclusions contained in the software and documentation are those of the -* authors and should not be interpreted as representing official policies, either expressed -* or implied, of BetaSteward_at_googlemail.com. -*/ - + * Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of BetaSteward_at_googlemail.com. + */ package mage.filter.common; import java.util.ArrayList; @@ -35,6 +34,7 @@ import mage.filter.FilterImpl; import mage.filter.FilterPlayer; import mage.filter.predicate.Predicate; import mage.filter.predicate.Predicates; +import mage.filter.predicate.other.PlayerIdPredicate; import mage.filter.predicate.permanent.ControllerIdPredicate; import mage.game.Game; import mage.game.permanent.Permanent; @@ -51,14 +51,20 @@ public class FilterPlaneswalkerOrPlayer extends FilterImpl { public FilterPlaneswalkerOrPlayer(Set defenders) { super("planeswalker or player"); + ArrayList> permanentPredicates = new ArrayList>(); for (UUID defenderId : defenders) { permanentPredicates.add(new ControllerIdPredicate(defenderId)); } planeswalkerFilter = new FilterPlaneswalkerPermanent(); planeswalkerFilter.add(Predicates.or(permanentPredicates)); + + ArrayList> playerPredicates = new ArrayList>(); + for (UUID defenderId : defenders) { + playerPredicates.add(new PlayerIdPredicate(defenderId)); + } playerFilter = new FilterPlayer(); - playerFilter.getPlayerId().addAll(defenders); + planeswalkerFilter.add(Predicates.or(playerPredicates)); } public FilterPlaneswalkerOrPlayer(final FilterPlaneswalkerOrPlayer filter) { @@ -70,16 +76,15 @@ public class FilterPlaneswalkerOrPlayer extends FilterImpl { @Override public boolean match(Object o, Game game) { if (o instanceof Player) { - return playerFilter.match((Player)o, game); + return playerFilter.match((Player) o, game); + } else if (o instanceof Permanent) { + return planeswalkerFilter.match((Permanent) o, game); } - else if (o instanceof Permanent) { - return planeswalkerFilter.match((Permanent)o, game); - } - return notFilter; + return false; } @Override public FilterPlaneswalkerOrPlayer copy() { return new FilterPlaneswalkerOrPlayer(this); } -} \ No newline at end of file +} diff --git a/Mage/src/mage/filter/predicate/other/PlayerIdPredicate.java b/Mage/src/mage/filter/predicate/other/PlayerIdPredicate.java new file mode 100644 index 0000000000..104b5620a3 --- /dev/null +++ b/Mage/src/mage/filter/predicate/other/PlayerIdPredicate.java @@ -0,0 +1,56 @@ +/* + * Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of BetaSteward_at_googlemail.com. + */ +package mage.filter.predicate.other; + +import java.util.UUID; +import mage.filter.predicate.Predicate; +import mage.game.Game; +import mage.players.Player; + +/** + * + * @author North + */ +public class PlayerIdPredicate implements Predicate { + + private final UUID playerId; + + public PlayerIdPredicate(UUID playerId) { + this.playerId = playerId; + } + + @Override + public boolean apply(Player input, Game game) { + return playerId.equals(input.getId()); + } + + @Override + public String toString() { + return "PlayerId(" + playerId + ')'; + } +} diff --git a/Mage/src/mage/filter/predicate/other/PlayerPredicate.java b/Mage/src/mage/filter/predicate/other/PlayerPredicate.java new file mode 100644 index 0000000000..e12133e7e4 --- /dev/null +++ b/Mage/src/mage/filter/predicate/other/PlayerPredicate.java @@ -0,0 +1,82 @@ +/* + * Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of BetaSteward_at_googlemail.com. + */ +package mage.filter.predicate.other; + +import java.util.UUID; +import mage.Constants.TargetController; +import mage.filter.predicate.ObjectSourcePlayer; +import mage.filter.predicate.ObjectSourcePlayerPredicate; +import mage.game.Game; +import mage.players.Player; + +/** + * + * @author North + */ +public class PlayerPredicate implements ObjectSourcePlayerPredicate> { + + private TargetController targetPlayer; + + public PlayerPredicate(TargetController player) { + this.targetPlayer = player; + } + + @Override + public boolean apply(ObjectSourcePlayer input, Game game) { + Player player = input.getObject(); + UUID playerId = input.getPlayerId(); + if (player == null || playerId == null) { + return false; + } + + switch (targetPlayer) { + case YOU: + if (player.getId().equals(playerId)) { + return true; + } + break; + case OPPONENT: + if (game.getOpponents(playerId).contains(player.getId())) { + return true; + } + break; + case NOT_YOU: + if (!player.getId().equals(playerId)) { + return true; + } + break; + } + + return false; + } + + @Override + public String toString() { + return "Player(" + targetPlayer + ')'; + } +} diff --git a/Mage/src/mage/target/common/TargetOpponent.java b/Mage/src/mage/target/common/TargetOpponent.java index fecf38eb98..a0b4b2a82d 100644 --- a/Mage/src/mage/target/common/TargetOpponent.java +++ b/Mage/src/mage/target/common/TargetOpponent.java @@ -25,18 +25,20 @@ * authors and should not be interpreted as representing official policies, either expressed * or implied, of BetaSteward_at_googlemail.com. */ - package mage.target.common; +import java.util.UUID; +import mage.Constants.TargetController; import mage.abilities.Ability; +import mage.filter.FilterPlayer; +import mage.filter.predicate.other.PlayerPredicate; import mage.game.Game; import mage.target.TargetPlayer; -import java.util.UUID; - /** * * @author BetaSteward_at_googlemail.com + * @author North */ public class TargetOpponent extends TargetPlayer { @@ -45,8 +47,8 @@ public class TargetOpponent extends TargetPlayer { } public TargetOpponent(boolean required) { - super(); - this.targetName = "opponent"; + super(1, 1, false, new FilterPlayer("opponent")); + this.filter.add(new PlayerPredicate(TargetController.OPPONENT)); setRequired(required); } @@ -56,15 +58,11 @@ public class TargetOpponent extends TargetPlayer { @Override public boolean canChoose(UUID sourceId, UUID sourceControllerId, Game game) { - filter.getPlayerId().clear(); - filter.getPlayerId().addAll(game.getOpponents(sourceControllerId)); return super.canChoose(sourceId, sourceControllerId, game); } @Override public boolean canTarget(UUID id, Ability source, Game game) { - filter.getPlayerId().clear(); - filter.getPlayerId().addAll(game.getOpponents(source.getControllerId())); return super.canTarget(id, source, game); } @@ -72,5 +70,4 @@ public class TargetOpponent extends TargetPlayer { public TargetOpponent copy() { return new TargetOpponent(this); } - }