From 36d07416b9e5199eab8ab87bf72468f336482509 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Thu, 18 Jul 2013 13:24:21 +0200 Subject: [PATCH] Fixed DiscardEachPlayerEffect so that all players discard simultaneously (Fixes #286). --- .../common/DiscardEachPlayerEffect.java | 57 ++++++++++++++----- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/Mage/src/mage/abilities/effects/common/DiscardEachPlayerEffect.java b/Mage/src/mage/abilities/effects/common/DiscardEachPlayerEffect.java index 4df0f5d66a..10575192f5 100644 --- a/Mage/src/mage/abilities/effects/common/DiscardEachPlayerEffect.java +++ b/Mage/src/mage/abilities/effects/common/DiscardEachPlayerEffect.java @@ -1,19 +1,23 @@ package mage.abilities.effects.common; +import java.util.HashMap; import java.util.UUID; - import mage.abilities.Ability; import mage.abilities.Mode; import mage.abilities.dynamicvalue.DynamicValue; import mage.abilities.dynamicvalue.common.StaticValue; import mage.abilities.effects.OneShotEffect; import mage.cards.Card; +import mage.cards.Cards; +import mage.cards.CardsImpl; import mage.constants.Outcome; +import mage.filter.FilterCard; import mage.game.Game; import mage.players.Player; +import mage.target.Target; +import mage.target.common.TargetDiscard; import mage.util.CardUtil; - public class DiscardEachPlayerEffect extends OneShotEffect { protected DynamicValue amount; @@ -41,19 +45,46 @@ public class DiscardEachPlayerEffect extends OneShotEffect cardsToDiscard = new HashMap(); + if (controller != null) { + // choose cards to discard + for (UUID playerId : controller.getInRange()) { + Player player = game.getPlayer(playerId); + if (player != null) { + int numberOfCardsToDiscard = Math.min(amount.calculate(game, source), player.getHand().size()); + Cards cards = new CardsImpl(); + if (randomDiscard) { + while (cards.size() < numberOfCardsToDiscard) { + Card card = player.getHand().getRandom(game); + if (!cards.contains(card.getId())) { + cards.add(card); + } + } + } else { + Target target = new TargetDiscard(numberOfCardsToDiscard, numberOfCardsToDiscard, new FilterCard(), playerId); + target.setRequired(true); + player.chooseTarget(outcome, target, source, game); + cards.addAll(target.getTargets()); + } + cardsToDiscard.put(playerId, cards); + } + } + // discard all choosen cards + for (UUID playerId : controller.getInRange()) { + Player player = game.getPlayer(playerId); + if (player != null) { + Cards cardsPlayer = cardsToDiscard.get(playerId); + if (cardsPlayer != null) { + for (UUID cardId : cardsPlayer) { + Card card = game.getCard(cardId); + if (card != null) { + player.discard(card, source, game); + } } + game.informPlayers(new StringBuilder(player.getName()).append(" discards ").append(Integer.toString(cardsPlayer.size())).append(" card").append(cardsPlayer.size() > 1?"s":"").toString()); } - } else { - player.discard(amount.calculate(game, source), source, game); } } }