use of mage.filter.predicate.mageobject.NamePredicate in project mage by magefree.
the class ReapIntellectEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player targetPlayer = game.getPlayer(source.getFirstTarget());
Player controller = game.getPlayer(source.getControllerId());
MageObject sourceObject = game.getObject(source.getSourceId());
if (targetPlayer != null && sourceObject != null && controller != null) {
// reveal hand of target player
targetPlayer.revealCards(sourceObject.getName(), targetPlayer.getHand(), game);
// Chose cards to exile from hand
Cards exiledCards = new CardsImpl();
int xCost = Math.min(source.getManaCostsToPay().getX(), targetPlayer.getHand().size());
TargetCard target = new TargetCard(0, xCost, Zone.HAND, filterNonLands);
target.setNotTarget(true);
controller.chooseTarget(Outcome.Benefit, targetPlayer.getHand(), target, source, game);
for (UUID cardId : target.getTargets()) {
Card chosenCard = game.getCard(cardId);
if (chosenCard != null) {
controller.moveCardToExileWithInfo(chosenCard, null, "", source, game, Zone.HAND, true);
exiledCards.add(chosenCard);
}
}
// 4/15/2013 If you don't exile any cards from the player's hand, you don't search that player's library
if (!exiledCards.isEmpty()) {
// Building a card filter with all names
List<NamePredicate> names = new ArrayList<>();
FilterCard filterNamedCards = new FilterCard();
for (Card card : exiledCards.getCards(game)) {
String nameToSearch = CardUtil.getCardNameForSameNameSearch(card);
if (exiledCards.size() == 1) {
filterNamedCards.add(new NamePredicate(nameToSearch));
} else {
names.add(new NamePredicate(nameToSearch));
}
}
if (exiledCards.size() > 1) {
filterNamedCards.add(Predicates.or(names));
}
// search cards in graveyard
TargetCardInGraveyard targetCardsGraveyard = new TargetCardInGraveyard(0, Integer.MAX_VALUE, filterNamedCards);
controller.chooseTarget(outcome, targetPlayer.getGraveyard(), targetCardsGraveyard, source, game);
for (UUID cardId : targetCardsGraveyard.getTargets()) {
Card card = game.getCard(cardId);
if (card != null) {
controller.moveCardToExileWithInfo(card, null, "", source, game, Zone.GRAVEYARD, true);
}
}
// search cards in hand
TargetCard targetCardsHand = new TargetCard(0, Integer.MAX_VALUE, Zone.HAND, filterNamedCards);
controller.chooseTarget(Outcome.Benefit, targetPlayer.getGraveyard(), targetCardsHand, source, game);
for (UUID cardId : targetCardsHand.getTargets()) {
Card card = game.getCard(cardId);
if (card != null) {
controller.moveCardToExileWithInfo(card, null, "", source, game, Zone.HAND, true);
}
}
// search cards in Library
TargetCardInLibrary targetCardsLibrary = new TargetCardInLibrary(0, Integer.MAX_VALUE, filterNamedCards);
controller.searchLibrary(targetCardsLibrary, source, game, targetPlayer.getId());
for (UUID cardId : targetCardsLibrary.getTargets()) {
Card card = game.getCard(cardId);
if (card != null) {
controller.moveCardToExileWithInfo(card, null, "", source, game, Zone.LIBRARY, true);
}
}
}
targetPlayer.shuffleLibrary(source, game);
return true;
}
return false;
}
use of mage.filter.predicate.mageobject.NamePredicate in project mage by magefree.
the class TargetTwoNonLandCardsWithSameNameInHand method canChoose.
@Override
public boolean canChoose(UUID sourceControllerId, Game game) {
Cards cardsToCheck = new CardsImpl();
Player player = game.getPlayer(sourceControllerId);
if (player == null) {
return false;
}
for (Card card : player.getHand().getCards(filter, game)) {
cardsToCheck.add(card.getId());
}
int possibleCards = 0;
for (Card card : cardsToCheck.getCards(game)) {
String nameToSearch = CardUtil.getCardNameForSameNameSearch(card);
FilterCard nameFilter = new FilterCard();
nameFilter.add(new NamePredicate(nameToSearch));
if (cardsToCheck.count(nameFilter, game) > 1) {
++possibleCards;
}
}
return possibleCards > 0;
}
use of mage.filter.predicate.mageobject.NamePredicate in project mage by magefree.
the class TargetTwoNonLandCardsWithSameNameInHand method possibleTargets.
@Override
public Set<UUID> possibleTargets(UUID sourceControllerId, Game game) {
Set<UUID> newPossibleTargets = new HashSet<>();
Set<UUID> possibleTargets = new HashSet<>();
Player player = game.getPlayer(sourceControllerId);
if (player == null) {
return newPossibleTargets;
}
for (Card card : player.getHand().getCards(filter, game)) {
possibleTargets.add(card.getId());
}
Cards cardsToCheck = new CardsImpl();
cardsToCheck.addAll(possibleTargets);
if (targets.size() == 1) {
// first target is laready choosen, now only targets with the same name are selectable
for (Map.Entry<UUID, Integer> entry : targets.entrySet()) {
Card chosenCard = cardsToCheck.get(entry.getKey(), game);
if (chosenCard != null) {
for (UUID cardToCheck : cardsToCheck) {
if (!cardToCheck.equals(chosenCard.getId()) && chosenCard.getName().equals(game.getCard(cardToCheck).getName())) {
newPossibleTargets.add(cardToCheck);
}
}
}
}
} else {
for (UUID cardToCheck : cardsToCheck) {
Card card = game.getCard(cardToCheck);
if (card != null) {
String nameToSearch = CardUtil.getCardNameForSameNameSearch(card);
FilterCard nameFilter = new FilterCard();
nameFilter.add(new NamePredicate(nameToSearch));
if (cardsToCheck.count(nameFilter, game) > 1) {
newPossibleTargets.add(cardToCheck);
}
}
}
}
return newPossibleTargets;
}
use of mage.filter.predicate.mageobject.NamePredicate in project mage by magefree.
the class TargetTwoNonLandCardsWithSameNameInHand method canTarget.
@Override
public boolean canTarget(UUID id, Game game) {
if (super.canTarget(id, game)) {
Card card = game.getCard(id);
if (card != null) {
if (targets.size() == 1) {
Card card2 = game.getCard(targets.entrySet().iterator().next().getKey());
return CardUtil.haveSameNames(card2, card);
} else {
String nameToSearch = CardUtil.getCardNameForSameNameSearch(card);
FilterCard nameFilter = new FilterCard();
nameFilter.add(new NamePredicate(nameToSearch));
Player player = game.getPlayer(card.getOwnerId());
return player != null && player.getHand().getCards(nameFilter, game).size() > 1;
}
}
}
return false;
}
use of mage.filter.predicate.mageobject.NamePredicate in project mage by magefree.
the class SurgicalExtractionEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
// 6/1/2011 "Any number of cards" means just that. If you wish, you can choose to
// leave some or all of the cards with the same name as the targeted card,
// including that card, in the zone they're in.
Card chosenCard = game.getCard(source.getFirstTarget());
Player controller = game.getPlayer(source.getControllerId());
if (chosenCard != null && controller != null) {
Player owner = game.getPlayer(chosenCard.getOwnerId());
if (owner != null) {
String nameToSearch = CardUtil.getCardNameForSameNameSearch(chosenCard);
FilterCard filterNamedCard = new FilterCard("card named " + nameToSearch);
filterNamedCard.add(new NamePredicate(nameToSearch));
// cards in Graveyard
int cardsCount = owner.getGraveyard().count(filterNamedCard, game);
if (cardsCount > 0) {
filterNamedCard.setMessage("card named " + nameToSearch + " in the graveyard of " + owner.getName());
TargetCardInGraveyard target = new TargetCardInGraveyard(0, cardsCount, filterNamedCard);
if (controller.chooseTarget(Outcome.Exile, owner.getGraveyard(), target, source, game)) {
List<UUID> targets = target.getTargets();
for (UUID targetId : targets) {
Card targetCard = owner.getGraveyard().get(targetId, game);
if (targetCard != null) {
controller.moveCardToExileWithInfo(targetCard, null, "", source, game, Zone.GRAVEYARD, true);
}
}
}
}
// cards in Hand
filterNamedCard.setMessage("card named " + nameToSearch + " in the hand of " + owner.getName());
TargetCard targetCardInHand = new TargetCard(0, Integer.MAX_VALUE, Zone.HAND, filterNamedCard);
targetCardInHand.setNotTarget(true);
if (controller.chooseTarget(Outcome.Exile, owner.getHand(), targetCardInHand, source, game)) {
List<UUID> targets = targetCardInHand.getTargets();
for (UUID targetId : targets) {
Card targetCard = owner.getHand().get(targetId, game);
if (targetCard != null) {
controller.moveCardToExileWithInfo(targetCard, null, "", source, game, Zone.HAND, true);
}
}
}
// cards in Library
filterNamedCard.setMessage("card named " + nameToSearch + " in the library of " + owner.getName());
TargetCardInLibrary targetCardInLibrary = new TargetCardInLibrary(0, Integer.MAX_VALUE, filterNamedCard);
if (controller.searchLibrary(targetCardInLibrary, source, game, owner.getId())) {
List<UUID> targets = targetCardInLibrary.getTargets();
for (UUID targetId : targets) {
Card targetCard = owner.getLibrary().getCard(targetId, game);
if (targetCard != null) {
controller.moveCardToExileWithInfo(targetCard, null, "", source, game, Zone.LIBRARY, true);
}
}
}
owner.shuffleLibrary(source, game);
return true;
}
}
return false;
}
Aggregations