From dc54bb2ddf3d8d0ed8839d3c2201c5b8c9df2f41 Mon Sep 17 00:00:00 2001 From: jeffwadsworth Date: Fri, 5 Jun 2020 16:45:20 -0500 Subject: [PATCH] - Fixed #6605 --- Mage.Sets/src/mage/cards/d/DecoyGambit.java | 60 +++++++++++++++++---- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/Mage.Sets/src/mage/cards/d/DecoyGambit.java b/Mage.Sets/src/mage/cards/d/DecoyGambit.java index 99c29db62cd..d2385583793 100644 --- a/Mage.Sets/src/mage/cards/d/DecoyGambit.java +++ b/Mage.Sets/src/mage/cards/d/DecoyGambit.java @@ -6,7 +6,6 @@ import mage.cards.CardImpl; import mage.cards.CardSetInfo; import mage.constants.CardType; import mage.constants.Outcome; -import mage.constants.Zone; import mage.filter.FilterPermanent; import mage.filter.common.FilterCreaturePermanent; import mage.filter.predicate.permanent.ControllerIdPredicate; @@ -16,12 +15,14 @@ import mage.players.Player; import mage.target.Target; import mage.target.TargetPermanent; import mage.target.targetadjustment.TargetAdjuster; - import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Objects; import java.util.UUID; import java.util.stream.Collectors; +import mage.abilities.condition.Condition; +import mage.constants.Zone; /** * @author TheElk801 @@ -31,7 +32,8 @@ public final class DecoyGambit extends CardImpl { public DecoyGambit(UUID ownerId, CardSetInfo setInfo) { super(ownerId, setInfo, new CardType[]{CardType.INSTANT}, "{2}{U}"); - // For each opponent, choose up to one target creature that player controls, then return that creature to its owner's hand unless its controller has you draw a card. + // For each opponent, choose up to one target creature that player controls, + // then return that creature to its owner's hand unless its controller has you draw a card. this.getSpellAbility().addEffect(new DecoyGambitEffect()); this.getSpellAbility().setTargetAdjuster(DecoyGambitAdjuster.instance); } @@ -70,8 +72,8 @@ class DecoyGambitEffect extends OneShotEffect { DecoyGambitEffect() { super(Outcome.Benefit); - staticText = "For each opponent, choose up to one target creature that player controls, " + - "then return that creature to its owner's hand unless its controller has you draw a card."; + staticText = "For each opponent, choose up to one target creature that player controls, " + + "then return that creature to its owner's hand unless its controller has you draw a card."; } private DecoyGambitEffect(final DecoyGambitEffect effect) { @@ -86,6 +88,8 @@ class DecoyGambitEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); + HashSet permanentToHand = new HashSet(); + int numberOfCardsToDraw = 0; if (controller == null) { return false; } @@ -98,16 +102,54 @@ class DecoyGambitEffect extends OneShotEffect { .filter(Objects::nonNull) .collect(Collectors.toList()); for (Permanent permanent : permanents) { + // If a creature targeted by Decoy Gambit changes controller, it’s no longer a legal target. + new DecoyGambitCondition(permanent).apply(game, source); // save current controller Player player = game.getPlayer(permanent.getControllerId()); if (player == null) { continue; } - if (player.chooseUse(outcome, "Have " + controller.getName() + " draw a card? If you don't, " + - permanent.getName() + " will be returned to its owner's hand.", source, game) - && controller.drawCards(1, source.getSourceId(), game) > 0) { - player.moveCards(permanent, Zone.HAND, source, game); + if (player.chooseUse(outcome, "Have " + controller.getName() + " draw a card? If you don't, " + + permanent.getName() + " will be returned to its owner's hand.", source, game)) { + game.informPlayers(player.getName() + " chose to have " + controller.getName() + " draw a card."); + numberOfCardsToDraw += 1; + } else { + game.informPlayers(player.getName() + " chose to have their creature returned to their hand."); + permanentToHand.add(permanent); + } + } + /* + As the Decoy Gambit resolves, first the next opponent in turn order (or, if it’s an opponent’s + turn, the opponent whose turn it is) chooses whether you’ll draw a card or return their creature + that was targeted to its owner’s hand, then each other opponent in turn order does so knowing + the choices made before them. After all choices are made, you draw the appropriate number of + cards. After you’ve drawn, the appropriate creatures are all simultaneously returned to their owners’ hands. + */ + controller.drawCards(numberOfCardsToDraw, source.getSourceId(), game); + for (Permanent creature : permanentToHand) { + if (creature != null + && new DecoyGambitCondition(creature).apply(game, source)) { // same controller required + creature.moveToZone(Zone.HAND, source.getSourceId(), game, false); } } return true; } } + +class DecoyGambitCondition implements Condition { + + private UUID controllerId; + private final Permanent permanent; + + DecoyGambitCondition(Permanent permanent) { + this.permanent = permanent; + } + + @Override + public boolean apply(Game game, Ability source) { + if (controllerId == null) { // is the original controller set + controllerId = permanent.getControllerId(); // original controller set + } + return (permanent != null + && Objects.equals(controllerId, permanent.getControllerId())); + } +}