[filters] converted FilterCard conditions to Predicates

This commit is contained in:
North 2012-07-23 21:53:54 +03:00
parent c2c04e6a56
commit 1542ba9ab6
13 changed files with 410 additions and 215 deletions

View file

@ -52,6 +52,8 @@ import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.CardTypePredicate; import mage.filter.predicate.mageobject.CardTypePredicate;
import mage.filter.predicate.mageobject.ColorPredicate; import mage.filter.predicate.mageobject.ColorPredicate;
import mage.filter.predicate.mageobject.ColorlessPredicate; import mage.filter.predicate.mageobject.ColorlessPredicate;
import mage.filter.predicate.other.CardTextPredicate;
import mage.filter.predicate.other.ExpansionSetPredicate;
import mage.sets.Sets; import mage.sets.Sets;
import mage.view.CardsView; import mage.view.CardsView;
@ -212,12 +214,16 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
filter.add(Predicates.or(predicates)); filter.add(Predicates.or(predicates));
String name = jTextFieldSearch.getText().trim(); String name = jTextFieldSearch.getText().trim();
filter.setText(name); filter.add(new CardTextPredicate(name));
if (this.cbExpansionSet.getSelectedItem() instanceof ExpansionSet) { if (this.cbExpansionSet.getSelectedItem() instanceof ExpansionSet) {
filter.getExpansionSetCode().add(((ExpansionSet) this.cbExpansionSet.getSelectedItem()).getCode()); filter.add(new ExpansionSetPredicate(((ExpansionSet) this.cbExpansionSet.getSelectedItem()).getCode()));
} else if (this.cbExpansionSet.getSelectedItem().equals("-- Standard")) { } else if (this.cbExpansionSet.getSelectedItem().equals("-- Standard")) {
filter.getExpansionSetCode().addAll(ConstructedFormats.getSetsByFormat("Standard")); ArrayList<Predicate<Card>> expansionPredicates = new ArrayList<Predicate<Card>>();
for (String setCode : ConstructedFormats.getSetsByFormat("Standard")) {
expansionPredicates.add(new ExpansionSetPredicate(setCode));
}
filter.add(Predicates.or(expansionPredicates));
} }
} }
@ -228,16 +234,18 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
setCursor(new Cursor(Cursor.WAIT_CURSOR)); setCursor(new Cursor(Cursor.WAIT_CURSOR));
if (limited) { if (limited) {
for (Card card: cards) { for (Card card: cards) {
if (filter.match(card, null)) if (filter.match(card, null)) {
filteredCards.add(card); filteredCards.add(card);
} }
} }
}
else { else {
for (Card card: CardsStorage.getAllCards()) { for (Card card: CardsStorage.getAllCards()) {
if (filter.match(card, null)) if (filter.match(card, null)) {
filteredCards.add(card); filteredCards.add(card);
} }
} }
}
this.currentView.loadCards(new CardsView(filteredCards), (SortBy) cbSortBy.getSelectedItem(), chkPiles.isSelected(), bigCard, null); this.currentView.loadCards(new CardsView(filteredCards), (SortBy) cbSortBy.getSelectedItem(), chkPiles.isSelected(), bigCard, null);
this.cardCount.setText(String.valueOf(filteredCards.size())); this.cardCount.setText(String.valueOf(filteredCards.size()));
} }

View file

@ -46,6 +46,8 @@ import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.CardTypePredicate; import mage.filter.predicate.mageobject.CardTypePredicate;
import mage.filter.predicate.mageobject.ColorPredicate; import mage.filter.predicate.mageobject.ColorPredicate;
import mage.filter.predicate.mageobject.ColorlessPredicate; import mage.filter.predicate.mageobject.ColorlessPredicate;
import mage.filter.predicate.other.CardTextPredicate;
import mage.filter.predicate.other.ExpansionSetPredicate;
import mage.sets.Sets; import mage.sets.Sets;
import mage.view.CardsView; import mage.view.CardsView;
@ -142,12 +144,16 @@ public class CardTableSelector extends javax.swing.JPanel implements ComponentLi
filter.add(Predicates.or(predicates)); filter.add(Predicates.or(predicates));
String name = jTextFieldSearch.getText().trim(); String name = jTextFieldSearch.getText().trim();
filter.setText(name); filter.add(new CardTextPredicate(name));
if (this.cbExpansionSet.getSelectedItem() instanceof ExpansionSet) { if (this.cbExpansionSet.getSelectedItem() instanceof ExpansionSet) {
filter.getExpansionSetCode().add(((ExpansionSet) this.cbExpansionSet.getSelectedItem()).getCode()); filter.add(new ExpansionSetPredicate(((ExpansionSet) this.cbExpansionSet.getSelectedItem()).getCode()));
} else if (this.cbExpansionSet.getSelectedItem().equals("-- Standard")) { } else if (this.cbExpansionSet.getSelectedItem().equals("-- Standard")) {
filter.getExpansionSetCode().addAll(ConstructedFormats.getSetsByFormat("Standard")); ArrayList<Predicate<Card>> expansionPredicates = new ArrayList<Predicate<Card>>();
for(String setCode : ConstructedFormats.getSetsByFormat("Standard")) {
expansionPredicates.add(new ExpansionSetPredicate(setCode));
}
filter.add(Predicates.or(expansionPredicates));
} }
} }

View file

@ -51,6 +51,7 @@ import mage.cards.CardImpl;
import mage.counters.CounterType; import mage.counters.CounterType;
import mage.filter.FilterCard; import mage.filter.FilterCard;
import mage.filter.predicate.mageobject.CardTypePredicate; import mage.filter.predicate.mageobject.CardTypePredicate;
import mage.filter.predicate.other.OwnerPredicate;
import mage.target.common.TargetCardInHand; import mage.target.common.TargetCardInHand;
import mage.watchers.common.CastFromHandWatcher; import mage.watchers.common.CastFromHandWatcher;
@ -62,7 +63,7 @@ public class MyojinOfLifesWeb extends CardImpl<MyojinOfLifesWeb> {
private static final FilterCard filter = new FilterCard("any number of creature cards from your hand"); private static final FilterCard filter = new FilterCard("any number of creature cards from your hand");
static { static {
filter.add(new CardTypePredicate(CardType.CREATURE)); filter.add(new CardTypePredicate(CardType.CREATURE));
filter.setTargetOwner(TargetController.YOU); filter.add(new OwnerPredicate(TargetController.YOU));
} }
public MyojinOfLifesWeb(UUID ownerId) { public MyojinOfLifesWeb(UUID ownerId) {
@ -80,7 +81,8 @@ public class MyojinOfLifesWeb extends CardImpl<MyojinOfLifesWeb> {
// Myojin of Life's Web enters the battlefield with a divinity counter on it if you cast it from your hand. // Myojin of Life's Web enters the battlefield with a divinity counter on it if you cast it from your hand.
this.addAbility(new EntersBattlefieldAbility(new ConditionalOneShotEffect(new AddCountersSourceEffect(CounterType.DIVINITY.createInstance()), new CastFromHandCondition(), ""), "{this} enters the battlefield with a divinity counter on it if you cast it from your hand")); this.addAbility(new EntersBattlefieldAbility(new ConditionalOneShotEffect(new AddCountersSourceEffect(CounterType.DIVINITY.createInstance()), new CastFromHandCondition(), ""), "{this} enters the battlefield with a divinity counter on it if you cast it from your hand"));
// Myojin of Life's Web is indestructible as long as it has a divinity counter on it. // Myojin of Life's Web is indestructible as long as it has a divinity counter on it.
this.addAbility(new SimpleStaticAbility(Constants.Zone.BATTLEFIELD, new ConditionalContinousEffect(new GainAbilitySourceEffect(IndestructibleAbility.getInstance(), Constants.Duration.WhileOnBattlefield), this.addAbility(new SimpleStaticAbility(Constants.Zone.BATTLEFIELD,
new ConditionalContinousEffect(new GainAbilitySourceEffect(IndestructibleAbility.getInstance(), Constants.Duration.WhileOnBattlefield),
new HasCounterCondition(CounterType.DIVINITY), "{this} is indestructible as long as it has a divinity counter on it"))); new HasCounterCondition(CounterType.DIVINITY), "{this} is indestructible as long as it has a divinity counter on it")));
// Remove a divinity counter from Myojin of Life's Web: Put any number of creature cards from your hand onto the battlefield. // Remove a divinity counter from Myojin of Life's Web: Put any number of creature cards from your hand onto the battlefield.
Ability ability = new SimpleActivatedAbility(Constants.Zone.BATTLEFIELD, new PutOntoBattlefieldTargetEffect(false), new RemoveCountersSourceCost(CounterType.DIVINITY.createInstance())); Ability ability = new SimpleActivatedAbility(Constants.Zone.BATTLEFIELD, new PutOntoBattlefieldTargetEffect(false), new RemoveCountersSourceCost(CounterType.DIVINITY.createInstance()));

View file

@ -36,6 +36,7 @@ import mage.abilities.keyword.FlashbackAbility;
import mage.cards.CardImpl; import mage.cards.CardImpl;
import mage.filter.FilterCard; import mage.filter.FilterCard;
import mage.filter.predicate.mageobject.AbilityPredicate; import mage.filter.predicate.mageobject.AbilityPredicate;
import mage.filter.predicate.other.OwnerPredicate;
import mage.target.common.TargetCardInExile; import mage.target.common.TargetCardInExile;
/** /**
@ -47,7 +48,7 @@ public class RunicRepetition extends CardImpl<RunicRepetition> {
private static final FilterCard filter = new FilterCard("exiled card with flashback you own"); private static final FilterCard filter = new FilterCard("exiled card with flashback you own");
static { static {
filter.setTargetOwner(TargetController.YOU); filter.add(new OwnerPredicate(TargetController.YOU));
filter.add(new AbilityPredicate(FlashbackAbility.class)); filter.add(new AbilityPredicate(FlashbackAbility.class));
} }

View file

@ -25,34 +25,29 @@
* authors and should not be interpreted as representing official policies, either expressed * authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com. * or implied, of BetaSteward_at_googlemail.com.
*/ */
package mage.filter; package mage.filter;
import mage.Constants.TargetController; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import mage.cards.Card; import mage.cards.Card;
import mage.filter.predicate.ObjectPlayer;
import mage.filter.predicate.ObjectPlayerPredicate;
import mage.filter.predicate.Predicates;
import mage.game.Game; import mage.game.Game;
import mage.game.permanent.Permanent;
import java.util.*;
/** /**
* *
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
* @author North
*/ */
public class FilterCard extends FilterObject<Card> { public class FilterCard extends FilterObject<Card> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
protected List<ObjectPlayerPredicate<ObjectPlayer<Permanent>>> extraPredicates = new ArrayList<ObjectPlayerPredicate<ObjectPlayer<Permanent>>>();
protected List<UUID> ownerId = new ArrayList<UUID>();
protected boolean notOwner;
protected List<String> expansionSetCode = new ArrayList<String>();
protected boolean notExpansionSetCode;
protected TargetController owner = TargetController.ANY;
/**
* Text that appears on card.
* At the moment only card name and rules are checked.
*/
protected String text = "";
public FilterCard() { public FilterCard() {
super("card"); super("card");
@ -64,116 +59,19 @@ public class FilterCard extends FilterObject<Card> {
public FilterCard(FilterCard filter) { public FilterCard(FilterCard filter) {
super(filter); super(filter);
this.ownerId.addAll(filter.ownerId); this.extraPredicates = new ArrayList<ObjectPlayerPredicate<ObjectPlayer<Permanent>>>(filter.extraPredicates);
this.notOwner = filter.notOwner;
this.expansionSetCode.addAll(filter.expansionSetCode);
this.notExpansionSetCode = filter.notExpansionSetCode;
this.owner = filter.owner;
}
@Override
public boolean match(Card card, Game game) {
if (!super.match(card, game))
return notFilter;
if (ownerId.size() > 0 && ownerId.contains(card.getOwnerId()) == notOwner)
return notFilter;
if (expansionSetCode.size() > 0 && expansionSetCode.contains(card.getExpansionSetCode()) == notExpansionSetCode)
return notFilter;
if (text.length() > 0) {
// first check in card name
boolean filterOut = !card.getName().toLowerCase().contains(text.toLowerCase());
// if couldn't find
if (filterOut) {
//separate by spaces
String[] tokens = text.toLowerCase().split(" ");
int count = 0;
int found = 0;
for (String token : tokens) {
if (!token.isEmpty()) {
count++;
// then try to find in rules
for (String rule : card.getRules()) {
if (rule.toLowerCase().contains(token)) {
found++;
break;
}
}
if (filterOut) {
for (String subType : card.getSubtype()) {
if (subType.equalsIgnoreCase(token)) {
found++;
break;
}
}
}
}
}
if (found < count)
return notFilter;
}
}
return !notFilter;
} }
public boolean match(Card card, UUID playerId, Game game) { public boolean match(Card card, UUID playerId, Game game) {
if (!this.match(card, game)) if (!this.match(card, game)) {
return notFilter;
if (owner != TargetController.ANY && playerId != null) {
switch(owner) {
case YOU:
if (!card.getOwnerId().equals(playerId))
return notFilter;
break;
case OPPONENT:
if (!game.getOpponents(playerId).contains(card.getOwnerId()))
return notFilter;
break;
case NOT_YOU:
if (card.getOwnerId().equals(playerId))
return notFilter;
break;
}
}
return !notFilter;
}
public List<UUID> getOwnerId() {
return ownerId;
}
public void setNotOwner(boolean notOwner) {
this.notOwner = notOwner;
}
public List<String> getExpansionSetCode() {
return expansionSetCode;
}
public void setNotExpansionSetCode(boolean notExpansionSetCode) {
this.notExpansionSetCode = notExpansionSetCode;
}
public void setText(String text) {
this.text = text;
}
public void setTargetOwner(TargetController owner) {
this.owner = owner;
}
public boolean matchOwner(UUID testOwnerId) {
if (ownerId.size() > 0 && ownerId.contains(testOwnerId) == notOwner)
return false; return false;
return true; }
return Predicates.and(extraPredicates).apply(new ObjectPlayer(card, playerId), game);
}
public void add(ObjectPlayerPredicate predicate) {
extraPredicates.add(predicate);
} }
public Set<Card> filter(Set<Card> cards, Game game) { public Set<Card> filter(Set<Card> cards, Game game) {

View file

@ -43,7 +43,6 @@ public abstract class FilterImpl<E> implements Filter<E> {
protected List<Predicate<Object>> predicates = new ArrayList<Predicate<Object>>(); protected List<Predicate<Object>> predicates = new ArrayList<Predicate<Object>>();
protected String message; protected String message;
protected boolean notFilter = false;
@Override @Override
public abstract FilterImpl<E> copy(); public abstract FilterImpl<E> copy();
@ -54,7 +53,6 @@ public abstract class FilterImpl<E> implements Filter<E> {
public FilterImpl(FilterImpl filter) { public FilterImpl(FilterImpl filter) {
this.message = filter.message; this.message = filter.message;
this.notFilter = filter.notFilter;
this.predicates = new ArrayList<Predicate<Object>>(filter.predicates); this.predicates = new ArrayList<Predicate<Object>>(filter.predicates);
} }
@ -77,9 +75,4 @@ public abstract class FilterImpl<E> implements Filter<E> {
public void setMessage(String message) { public void setMessage(String message) {
this.message = message; this.message = message;
} }
public void setNotFilter(boolean notFilter) {
this.notFilter = notFilter;
}
} }

View file

@ -0,0 +1,88 @@
/*
* 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 mage.cards.Card;
import mage.filter.predicate.Predicate;
import mage.game.Game;
/**
*
* @author North
*/
public class CardTextPredicate implements Predicate<Card> {
private final String text;
public CardTextPredicate(String text) {
this.text = text;
}
@Override
public boolean apply(Card input, Game game) {
if (text.isEmpty()) {
return true;
}
// first check in card name
if (input.getName().toLowerCase().contains(text.toLowerCase())) {
return true;
}
//separate by spaces
String[] tokens = text.toLowerCase().split(" ");
for (String token : tokens) {
boolean found = false;
if (!token.isEmpty()) {
// then try to find in rules
for (String rule : input.getRules()) {
if (rule.toLowerCase().contains(token)) {
found = true;
break;
}
}
for (String subType : input.getSubtype()) {
if (subType.equalsIgnoreCase(token)) {
found = true;
break;
}
}
}
if (!found) {
return false;
}
}
return true;
}
@Override
public String toString() {
return "CardText(" + text + ')';
}
}

View file

@ -0,0 +1,55 @@
/*
* 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 mage.cards.Card;
import mage.filter.predicate.Predicate;
import mage.game.Game;
/**
*
* @author North
*/
public class ExpansionSetPredicate implements Predicate<Card> {
private final String setCode;
public ExpansionSetPredicate(String setCode) {
this.setCode = setCode;
}
@Override
public boolean apply(Card input, Game game) {
return input.getExpansionSetCode().equals(setCode);
}
@Override
public String toString() {
return "ExpansionSet(" + setCode + ')';
}
}

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.cards.Card;
import mage.filter.predicate.Predicate;
import mage.game.Game;
/**
*
* @author North
*/
public class OwnerIdPredicate implements Predicate<Card> {
private final UUID ownerId;
public OwnerIdPredicate(UUID ownerId) {
this.ownerId = ownerId;
}
@Override
public boolean apply(Card input, Game game) {
return ownerId.equals(input.getOwnerId());
}
@Override
public String toString() {
return "OwnerId(" + ownerId + ')';
}
}

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.cards.Card;
import mage.filter.predicate.ObjectPlayer;
import mage.filter.predicate.ObjectPlayerPredicate;
import mage.game.Game;
/**
*
* @author North
*/
public class OwnerPredicate implements ObjectPlayerPredicate<ObjectPlayer<Card>> {
private TargetController targetOwner;
public OwnerPredicate(TargetController targetOwner) {
this.targetOwner = targetOwner;
}
@Override
public boolean apply(ObjectPlayer<Card> input, Game game) {
Card card = input.getObject();
UUID playerId = input.getPlayerId();
if (card == null || playerId == null) {
return false;
}
switch (targetOwner) {
case YOU:
if (card.getOwnerId().equals(playerId)) {
return true;
}
break;
case OPPONENT:
if (game.getOpponents(playerId).contains(card.getOwnerId())) {
return true;
}
break;
case NOT_YOU:
if (!card.getOwnerId().equals(playerId)) {
return true;
}
break;
}
return false;
}
@Override
public String toString() {
return "Owner(" + targetOwner + ')';
}
}

View file

@ -100,27 +100,29 @@ public class TargetCard<T extends TargetCard<T>> extends TargetObject<TargetCard
@Override @Override
public boolean canChoose(UUID sourceControllerId, Game game) { public boolean canChoose(UUID sourceControllerId, Game game) {
for (UUID playerId: game.getPlayer(sourceControllerId).getInRange()) { for (UUID playerId: game.getPlayer(sourceControllerId).getInRange()) {
if (filter.matchOwner(playerId)) {
Player player = game.getPlayer(playerId); Player player = game.getPlayer(playerId);
if (player != null) { if (player != null) {
switch (zone) { switch (zone) {
case HAND: case HAND:
if (player.getHand().count(filter, player.getId(), game) >= this.minNumberOfTargets) if (player.getHand().count(filter, player.getId(), game) >= this.minNumberOfTargets) {
return true; return true;
}
break; break;
case GRAVEYARD: case GRAVEYARD:
if (player.getGraveyard().count(filter, player.getId(), game) >= this.minNumberOfTargets) if (player.getGraveyard().count(filter, player.getId(), game) >= this.minNumberOfTargets) {
return true; return true;
}
break; break;
case LIBRARY: case LIBRARY:
if (player.getLibrary().count(filter, game) >= this.minNumberOfTargets) if (player.getLibrary().count(filter, game) >= this.minNumberOfTargets) {
return true; return true;
}
break; break;
case EXILED: case EXILED:
if (game.getExile().getPermanentExile().count(filter, player.getId(), game) >= this.minNumberOfTargets) if (game.getExile().getPermanentExile().count(filter, player.getId(), game) >= this.minNumberOfTargets) {
return true; return true;
break;
} }
break;
} }
} }
} }
@ -144,7 +146,6 @@ public class TargetCard<T extends TargetCard<T>> extends TargetObject<TargetCard
public Set<UUID> possibleTargets(UUID sourceControllerId, Game game) { public Set<UUID> possibleTargets(UUID sourceControllerId, Game game) {
Set<UUID> possibleTargets = new HashSet<UUID>(); Set<UUID> possibleTargets = new HashSet<UUID>();
for (UUID playerId : game.getPlayer(sourceControllerId).getInRange()) { for (UUID playerId : game.getPlayer(sourceControllerId).getInRange()) {
if (filter.matchOwner(playerId)) {
Player player = game.getPlayer(playerId); Player player = game.getPlayer(playerId);
if (player != null) { if (player != null) {
switch (zone) { switch (zone) {
@ -160,17 +161,18 @@ public class TargetCard<T extends TargetCard<T>> extends TargetObject<TargetCard
break; break;
case LIBRARY: case LIBRARY:
for (Card card : player.getLibrary().getUniqueCards(game)) { for (Card card : player.getLibrary().getUniqueCards(game)) {
if (filter.match(card, game)) if (filter.match(card, game)) {
possibleTargets.add(card.getId()); possibleTargets.add(card.getId());
} }
}
break; break;
case EXILED: case EXILED:
for (Card card : game.getExile().getPermanentExile().getUniqueCards(game)) { for (Card card : game.getExile().getPermanentExile().getUniqueCards(game)) {
if (filter.match(card, player.getId(), game)) if (filter.match(card, player.getId(), game)) {
possibleTargets.add(card.getId()); possibleTargets.add(card.getId());
} }
break;
} }
break;
} }
} }
} }
@ -179,8 +181,9 @@ public class TargetCard<T extends TargetCard<T>> extends TargetObject<TargetCard
public boolean canTarget(UUID id, Cards cards, Game game) { public boolean canTarget(UUID id, Cards cards, Game game) {
Card card = cards.get(id, game); Card card = cards.get(id, game);
if (card != null) if (card != null) {
return filter.match(card, game); return filter.match(card, game);
}
return false; return false;
} }

View file

@ -66,8 +66,9 @@ public class TargetCardInHand extends TargetCard<TargetCardInHand> {
@Override @Override
public boolean canTarget(UUID id, Ability source, Game game) { public boolean canTarget(UUID id, Ability source, Game game) {
Card card = game.getPlayer(source.getControllerId()).getHand().get(id, game); Card card = game.getPlayer(source.getControllerId()).getHand().get(id, game);
if (card != null) if (card != null) {
return filter.match(card, game); return filter.match(card, source.getControllerId(), game);
}
return false; return false;
} }

View file

@ -36,6 +36,7 @@ import mage.game.Game;
import mage.target.TargetCard; import mage.target.TargetCard;
import java.util.UUID; import java.util.UUID;
import mage.filter.predicate.other.OwnerIdPredicate;
/** /**
* *
@ -59,7 +60,7 @@ public class TargetDiscard extends TargetCard<TargetDiscard> {
public TargetDiscard(int minNumTargets, int maxNumTargets, FilterCard filter, UUID playerId) { public TargetDiscard(int minNumTargets, int maxNumTargets, FilterCard filter, UUID playerId) {
super(minNumTargets, maxNumTargets, Zone.HAND, filter); super(minNumTargets, maxNumTargets, Zone.HAND, filter);
this.filter.getOwnerId().add(playerId); this.filter.add(new OwnerIdPredicate(playerId));
this.playerId = playerId; this.playerId = playerId;
this.required = true; this.required = true;
this.targetName = "card to discard"; this.targetName = "card to discard";
@ -73,8 +74,9 @@ public class TargetDiscard extends TargetCard<TargetDiscard> {
@Override @Override
public boolean canTarget(UUID id, Ability source, Game game) { public boolean canTarget(UUID id, Ability source, Game game) {
Card card = game.getPlayer(playerId).getHand().get(id, game); Card card = game.getPlayer(playerId).getHand().get(id, game);
if (card != null) if (card != null) {
return filter.match(card, game); return filter.match(card, source.getControllerId(), game);
}
return false; return false;
} }