From e770189af3b433eb08b8b88a1a7e17cef483bab5 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Sat, 15 Nov 2014 12:25:02 +0100 Subject: [PATCH] * Player - Reworked player discard method. --- .../mage/sets/apocalypse/GerrardsVerdict.java | 78 ++----------------- Mage.Sets/src/mage/sets/homelands/Forget.java | 7 +- Mage/src/mage/players/Player.java | 3 + Mage/src/mage/players/PlayerImpl.java | 39 ++++++++-- 4 files changed, 41 insertions(+), 86 deletions(-) diff --git a/Mage.Sets/src/mage/sets/apocalypse/GerrardsVerdict.java b/Mage.Sets/src/mage/sets/apocalypse/GerrardsVerdict.java index b371a6c895..141b1094b7 100644 --- a/Mage.Sets/src/mage/sets/apocalypse/GerrardsVerdict.java +++ b/Mage.Sets/src/mage/sets/apocalypse/GerrardsVerdict.java @@ -27,27 +27,17 @@ */ package mage.sets.apocalypse; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; import java.util.UUID; import mage.abilities.Ability; import mage.abilities.effects.OneShotEffect; -import mage.abilities.effects.common.discard.DiscardTargetEffect; -import mage.cards.Card; import mage.cards.CardImpl; -import mage.cards.Cards; -import mage.cards.CardsImpl; import mage.constants.CardType; import mage.constants.Outcome; import mage.constants.Rarity; -import mage.constants.WatcherScope; import mage.filter.common.FilterLandCard; import mage.game.Game; -import mage.game.events.GameEvent; import mage.players.Player; import mage.target.TargetPlayer; -import mage.watchers.Watcher; /** * @@ -63,10 +53,8 @@ public class GerrardsVerdict extends CardImpl { this.color.setWhite(true); // Target player discards two cards. You gain 3 life for each land card discarded this way. - this.getSpellAbility().addEffect(new DiscardTargetEffect(2)); this.getSpellAbility().addEffect(new GerrardsVerdictEffect()); this.getSpellAbility().addTarget(new TargetPlayer()); - this.addWatcher(new GerrardsVerdictWatcher()); } @@ -84,7 +72,7 @@ class GerrardsVerdictEffect extends OneShotEffect { public GerrardsVerdictEffect() { super(Outcome.Benefit); - this.staticText = "You gain 3 life for each land card discarded this way"; + this.staticText = "Target player discards two cards. You gain 3 life for each land card discarded this way"; } public GerrardsVerdictEffect(final GerrardsVerdictEffect effect) { @@ -99,67 +87,11 @@ class GerrardsVerdictEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - if (controller != null) { - GerrardsVerdictWatcher watcher = (GerrardsVerdictWatcher) game.getState().getWatchers().get("GerrardsVerdictWatcher"); - if (watcher != null) { - Cards cards = watcher.getCardsDiscardedBySource(source.getSourceId()); - int life = cards.count(new FilterLandCard(), game) * 3; - controller.gainLife(life, game); - return true; - } + Player targetPlayer = game.getPlayer(getTargetPointer().getFirst(game, source)); + if (controller != null && targetPlayer != null) { + controller.gainLife(targetPlayer.discard(2, false, source, game).count(new FilterLandCard(), game) * 3, game); + return true; } return false; } } - -class GerrardsVerdictWatcher extends Watcher { - - private final Map cardsDiscardedBySource = new HashMap<>(); - - public GerrardsVerdictWatcher() { - super("GerrardsVerdictWatcher", WatcherScope.GAME); - } - - public GerrardsVerdictWatcher(final GerrardsVerdictWatcher watcher) { - super(watcher); - for (Entry entry : watcher.cardsDiscardedBySource.entrySet()) { - cardsDiscardedBySource.put(entry.getKey(), entry.getValue()); - } - } - - @Override - public void watch(GameEvent event, Game game) { - if (event.getType() == GameEvent.EventType.DISCARDED_CARD) { - Card card = game.getCard(event.getTargetId()); - if (card != null) { - Cards cards = cardsDiscardedBySource.get(event.getSourceId()); - if (cards == null) { - cards = new CardsImpl(); - cardsDiscardedBySource.put(event.getSourceId(), cards); - } - cards.add(card); - } - } - } - - /** - * get and remove the cards discarded by a sourceId - * @param sourceId - * @return - */ - public Cards getCardsDiscardedBySource(UUID sourceId) { - Cards cards = cardsDiscardedBySource.get(sourceId); - cardsDiscardedBySource.remove(sourceId); - return cards; - } - - @Override - public void reset() { - cardsDiscardedBySource.clear(); - } - - @Override - public GerrardsVerdictWatcher copy() { - return new GerrardsVerdictWatcher(this); - } -} diff --git a/Mage.Sets/src/mage/sets/homelands/Forget.java b/Mage.Sets/src/mage/sets/homelands/Forget.java index fadef6df16..c620e05f6b 100644 --- a/Mage.Sets/src/mage/sets/homelands/Forget.java +++ b/Mage.Sets/src/mage/sets/homelands/Forget.java @@ -85,12 +85,9 @@ class ForgetEffect extends OneShotEffect { public boolean apply(Game game, Ability source) { Player targetPlayer = game.getPlayer(source.getFirstTarget()); if (targetPlayer != null) { - int count = Math.min(2, targetPlayer.getHand().size()); - targetPlayer.discard(2, source, game); - targetPlayer.drawCards(count, game); + targetPlayer.drawCards(targetPlayer.discard(2, false, source, game).size(), game); return true; } return false; - } - + } } diff --git a/Mage/src/mage/players/Player.java b/Mage/src/mage/players/Player.java index 53ee3fffd0..b52be777e2 100644 --- a/Mage/src/mage/players/Player.java +++ b/Mage/src/mage/players/Player.java @@ -252,7 +252,10 @@ public interface Player extends MageItem, Copyable { boolean hasProtectionFrom(MageObject source, Game game); boolean flipCoin(Game game); boolean flipCoin(Game game, ArrayList appliedEffects); + @Deprecated void discard(int amount, Ability source, Game game); + Card discardOne(boolean random, Ability source, Game game); + Cards discard(int amount, boolean random, Ability source, Game game); void discardToMax(Game game); boolean discard(Card card, Ability source, Game game); void lost(Game game); diff --git a/Mage/src/mage/players/PlayerImpl.java b/Mage/src/mage/players/PlayerImpl.java index 6d4e88b98e..7bc077b121 100644 --- a/Mage/src/mage/players/PlayerImpl.java +++ b/Mage/src/mage/players/PlayerImpl.java @@ -644,25 +644,48 @@ public abstract class PlayerImpl implements Player, Serializable { @Override public void discard(int amount, Ability source, Game game) { - if (amount >= hand.size()) { - while (hand.size() > 0) { - discard(hand.get(hand.iterator().next(), game), source, game); + discard(amount, false, source, game); + } + + @Override + public Card discardOne(boolean random, Ability source, Game game) { + Cards cards = discard(1, random, source, game); + if (cards.isEmpty()) { + return null; + } + return cards.getRandom(game); + } + + @Override + public Cards discard(int amount, boolean random, Ability source, Game game) { + Cards discardedCards = new CardsImpl(); + if (amount >= this.getHand().size()) { + discardedCards.addAll(this.getHand()); + while (this.getHand().size() > 0) { + discard(this.getHand().get(this.getHand().iterator().next(), game), source, game); } - return; + return discardedCards; } int numDiscarded = 0; while (isInGame() && numDiscarded < amount) { - if (hand.size() == 0) { + if (this.getHand().size() == 0) { break; } - TargetDiscard target = new TargetDiscard(playerId); - choose(Outcome.Discard, target, source.getSourceId(), game); - Card card = hand.get(target.getFirstTarget(), game); + Card card; + if (random) { + card = this.getHand().getRandom(game); + } else { + TargetDiscard target = new TargetDiscard(playerId); + choose(Outcome.Discard, target, source.getSourceId(), game); + card = this.getHand().get(target.getFirstTarget(), game); + } if (card != null) { numDiscarded++; + discardedCards.add(card); discard(card, source, game); } } + return discardedCards; } @Override