Fixed DiscardEachPlayerEffect so that all players discard simultaneously (Fixes #286).

This commit is contained in:
LevelX2 2013-07-18 13:24:21 +02:00
parent 7c6666062e
commit 36d07416b9

View file

@ -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<DiscardEachPlayerEffect> {
protected DynamicValue amount;
@ -41,19 +45,46 @@ public class DiscardEachPlayerEffect extends OneShotEffect<DiscardEachPlayerEffe
@Override
public boolean apply(Game game, Ability source) {
for (UUID playerId : game.getPlayerList()) {
Player player = game.getPlayer(playerId);
if (player != null) {
if (randomDiscard) {
int maxAmount = Math.min(amount.calculate(game, source), player.getHand().size());
for (int i = 0; i < maxAmount; i++) {
Card card = player.getHand().getRandom(game);
if (card != null) {
player.discard(card, source, game);
Player controller = game.getPlayer(source.getControllerId());
// Store for each player the cards to discard, that's important because all discard shall happen at the same time
HashMap<UUID, Cards> cardsToDiscard = new HashMap<UUID, Cards>();
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);
}
}
}