Fix ShuffleIntoLibraryTargetEffect to support multiple target cards with possibly different owners

This commit is contained in:
Alex W. Jackson 2022-09-05 19:44:09 -04:00
parent 30aafb7672
commit b5b4b38cc6
6 changed files with 86 additions and 75 deletions

View file

@ -1,17 +1,22 @@
package mage.abilities.effects.common;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.Mode;
import mage.abilities.effects.OneShotEffect;
import mage.cards.Card;
import mage.cards.Cards;
import mage.cards.CardsImpl;
import mage.constants.Outcome;
import mage.game.Game;
import mage.players.Player;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
*
* @author Styxo
* @author awjackson
*/
public class ShuffleIntoLibraryTargetEffect extends OneShotEffect {
@ -38,30 +43,46 @@ public class ShuffleIntoLibraryTargetEffect extends OneShotEffect {
@Override
public boolean apply(Game game, Ability source) {
MageObject cardObject = game.getObject(getTargetPointer().getFirst(game, source));
Player controller = game.getPlayer(source.getControllerId());
if (cardObject != null && controller != null && cardObject instanceof Card) {
if (!optional
|| controller.chooseUse(Outcome.Benefit, "Shuffle " + cardObject.getIdName() + " into "
+ (((Card) cardObject).getOwnerId().equals(source.getControllerId()) ? "your" : "its owners")
+ " library?", source, game)) {
Player owner = game.getPlayer(((Card) cardObject).getOwnerId());
if (owner != null) {
return owner.shuffleCardsToLibrary(((Card) cardObject), game, source);
}
return true;
if (controller == null) {
return false;
}
Cards cards = new CardsImpl(getTargetPointer().getTargets(game, source));
if (cards.isEmpty()) {
return true;
}
if (optional && !controller.chooseUse(Outcome.Benefit, "Shuffle the target card" + (cards.size() > 1 ? "s" : "") + " into your library?", source, game)) {
return true;
}
// sort the target cards by owner
Map<UUID, Cards> cardsByOwner = new HashMap<>();
for (Card card : cards.getCards(game)) {
cardsByOwner.computeIfAbsent(card.getOwnerId(), x -> new CardsImpl()).add(card);
}
// then each player shuffles the cards they own
for (Map.Entry<UUID, Cards> entry : cardsByOwner.entrySet()) {
Player owner = game.getPlayer(entry.getKey());
if (owner != null) {
owner.shuffleCardsToLibrary(entry.getValue(), game, source);
}
}
return false;
return true;
}
@Override
public String getText(Mode mode
) {
public String getText(Mode mode)
{
if (staticText != null && !staticText.isEmpty()) {
return staticText;
} else {
return "choose target " + mode.getTargets().get(0).getTargetName() + ". Its owner shuffles it into their library";
}
String targetDescription = getTargetPointer().describeTargets(mode.getTargets(), "");
if (targetDescription.contains("your graveyard")) {
return (optional ? "you may shuffle " : "shuffle ") + targetDescription + " into your library";
}
return "choose " + targetDescription + (
getTargetPointer().isPlural(mode.getTargets()) ?
". The owners of those cards shuffle them into their libraries" :
". Its owner shuffles it into their library"
);
}
}

View file

@ -33,7 +33,7 @@ public class TargetCardInYourGraveyard extends TargetCard {
}
public TargetCardInYourGraveyard(int minNumTargets, int maxNumTargets) {
this(minNumTargets, maxNumTargets, StaticFilters.FILTER_CARD_FROM_YOUR_GRAVEYARD);
this(minNumTargets, maxNumTargets, maxNumTargets > 1 ? StaticFilters.FILTER_CARDS_FROM_YOUR_GRAVEYARD : StaticFilters.FILTER_CARD_FROM_YOUR_GRAVEYARD);
}
public TargetCardInYourGraveyard(int minNumTargets, int maxNumTargets, FilterCard filter) {