* Player - Reworked player discard method.

This commit is contained in:
LevelX2 2014-11-15 12:25:02 +01:00
parent f75ff0efe3
commit e770189af3
4 changed files with 41 additions and 86 deletions

View file

@ -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<UUID, Cards> cardsDiscardedBySource = new HashMap<>();
public GerrardsVerdictWatcher() {
super("GerrardsVerdictWatcher", WatcherScope.GAME);
}
public GerrardsVerdictWatcher(final GerrardsVerdictWatcher watcher) {
super(watcher);
for (Entry<UUID, Cards> 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);
}
}

View file

@ -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;
}
}
}

View file

@ -252,7 +252,10 @@ public interface Player extends MageItem, Copyable<Player> {
boolean hasProtectionFrom(MageObject source, Game game);
boolean flipCoin(Game game);
boolean flipCoin(Game game, ArrayList<UUID> 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);

View file

@ -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