[filters] converted FilterPlayer to Predicates

This commit is contained in:
North 2012-07-22 22:36:03 +03:00
parent b230fc883a
commit 28fe29854c
6 changed files with 190 additions and 93 deletions

View file

@ -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<SafePassage> {
static {
filter.getCreatureFilter().add(new ControllerPredicate(TargetController.YOU));
filter.getPlayerFilter().setPlayerTarget(TargetController.YOU);
filter.getPlayerFilter().add(new PlayerPredicate(TargetController.YOU));
}
public SafePassage(UUID ownerId) {

View file

@ -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<Player> {
protected List<ObjectPlayerPredicate<ObjectPlayer<Player>>> extraPredicates = new ArrayList<ObjectPlayerPredicate<ObjectPlayer<Player>>>();
protected List<UUID> playerId = new ArrayList<UUID>();
protected boolean notPlayer;
protected TargetController playerTarget = TargetController.ANY;
public FilterPlayer() {
this("player");
@ -62,58 +58,18 @@ public class FilterPlayer extends FilterImpl<Player> {
public FilterPlayer(final FilterPlayer filter) {
super(filter);
this.extraPredicates = new ArrayList<ObjectPlayerPredicate<ObjectPlayer<Player>>>(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 playerId, Game game) {
if (!this.match(player, game)) {
return false;
}
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;
}
}
return Predicates.and(extraPredicates).apply(new ObjectSourcePlayer(player, sourceId, controllerId), game);
}
public List<UUID> 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

View file

@ -25,7 +25,6 @@
* 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<Object> {
public FilterPlaneswalkerOrPlayer(Set<UUID> defenders) {
super("planeswalker or player");
ArrayList<Predicate<Permanent>> permanentPredicates = new ArrayList<Predicate<Permanent>>();
for (UUID defenderId : defenders) {
permanentPredicates.add(new ControllerIdPredicate(defenderId));
}
planeswalkerFilter = new FilterPlaneswalkerPermanent();
planeswalkerFilter.add(Predicates.or(permanentPredicates));
ArrayList<Predicate<Player>> playerPredicates = new ArrayList<Predicate<Player>>();
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) {
@ -71,11 +77,10 @@ public class FilterPlaneswalkerOrPlayer extends FilterImpl<Object> {
public boolean match(Object o, Game game) {
if (o instanceof Player) {
return playerFilter.match((Player) o, game);
}
else if (o instanceof Permanent) {
} else if (o instanceof Permanent) {
return planeswalkerFilter.match((Permanent) o, game);
}
return notFilter;
return false;
}
@Override

View file

@ -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<Player> {
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 + ')';
}
}

View file

@ -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<ObjectSourcePlayer<Player>> {
private TargetController targetPlayer;
public PlayerPredicate(TargetController player) {
this.targetPlayer = player;
}
@Override
public boolean apply(ObjectSourcePlayer<Player> 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 + ')';
}
}

View file

@ -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<TargetOpponent> {
@ -45,8 +47,8 @@ public class TargetOpponent extends TargetPlayer<TargetOpponent> {
}
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<TargetOpponent> {
@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<TargetOpponent> {
public TargetOpponent copy() {
return new TargetOpponent(this);
}
}