cleanup discard effects (#10924)

* change text generation to use target

* remove a constructor

* condense another constructor

* condense numberCardsToDiscard constructors

* fully remove TargetController

* chaining for optional parameter

* new LookTargetHandChooseDiscardEffect
This commit is contained in:
xenohedron 2023-08-20 13:30:44 -04:00 committed by GitHub
parent 7c554d7dc0
commit e02df1353a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
44 changed files with 160 additions and 285 deletions

View file

@ -1,6 +1,7 @@
package mage.abilities.effects.common.discard;
import mage.abilities.Ability;
import mage.abilities.Mode;
import mage.abilities.dynamicvalue.DynamicValue;
import mage.abilities.dynamicvalue.common.StaticValue;
import mage.abilities.effects.OneShotEffect;
@ -8,7 +9,6 @@ import mage.cards.Card;
import mage.cards.Cards;
import mage.cards.CardsImpl;
import mage.constants.Outcome;
import mage.constants.TargetController;
import mage.constants.Zone;
import mage.filter.FilterCard;
import mage.filter.StaticFilters;
@ -26,7 +26,6 @@ import java.util.UUID;
public class DiscardCardYouChooseTargetEffect extends OneShotEffect {
private final FilterCard filter;
private final TargetController targetController;
private DynamicValue numberCardsToReveal;
private final DynamicValue numberCardsToDiscard;
private final boolean revealAllCards;
@ -36,76 +35,56 @@ public class DiscardCardYouChooseTargetEffect extends OneShotEffect {
this(StaticFilters.FILTER_CARD_A);
}
public DiscardCardYouChooseTargetEffect(TargetController targetController) {
this(StaticFilters.FILTER_CARD_A, targetController);
}
public DiscardCardYouChooseTargetEffect(FilterCard filter) {
this(filter, TargetController.OPPONENT);
this(1, filter);
}
public DiscardCardYouChooseTargetEffect(FilterCard filter, TargetController targetController) {
this(StaticValue.get(1), filter, targetController);
public DiscardCardYouChooseTargetEffect(int numberCardsToDiscard, FilterCard filter) {
this(StaticValue.get(numberCardsToDiscard), filter);
}
public DiscardCardYouChooseTargetEffect(int numberCardsToDiscard, TargetController targetController) {
this(StaticValue.get(numberCardsToDiscard), targetController);
}
public DiscardCardYouChooseTargetEffect(DynamicValue numberCardsToDiscard, TargetController targetController) {
this(numberCardsToDiscard, StaticFilters.FILTER_CARD_CARDS, targetController);
}
public DiscardCardYouChooseTargetEffect(DynamicValue numberCardsToDiscard,
FilterCard filter, TargetController targetController) {
public DiscardCardYouChooseTargetEffect(DynamicValue numberCardsToDiscard, FilterCard filter) {
super(Outcome.Discard);
this.targetController = targetController;
this.filter = filter;
this.numberCardsToDiscard = numberCardsToDiscard;
this.numberCardsToReveal = null;
this.revealAllCards = true;
staticText = this.setText();
}
public DiscardCardYouChooseTargetEffect(TargetController targetController, int numberCardsToReveal) {
this(targetController, StaticValue.get(numberCardsToReveal));
public DiscardCardYouChooseTargetEffect(int numberCardsToReveal) {
this(StaticValue.get(numberCardsToReveal));
}
public DiscardCardYouChooseTargetEffect(TargetController targetController, DynamicValue numberCardsToReveal) {
this(StaticValue.get(1), StaticFilters.FILTER_CARD_A, targetController, numberCardsToReveal);
public DiscardCardYouChooseTargetEffect(DynamicValue numberCardsToReveal) {
this(StaticValue.get(1), StaticFilters.FILTER_CARD_A, numberCardsToReveal);
}
public DiscardCardYouChooseTargetEffect(int numberCardsToDiscard, TargetController targetController, int numberCardsToReveal) {
this(StaticValue.get(numberCardsToDiscard), StaticFilters.FILTER_CARD_CARDS, targetController, StaticValue.get(numberCardsToReveal));
public DiscardCardYouChooseTargetEffect(int numberCardsToDiscard, int numberCardsToReveal) {
this(StaticValue.get(numberCardsToDiscard), StaticFilters.FILTER_CARD_CARDS, StaticValue.get(numberCardsToReveal));
}
public DiscardCardYouChooseTargetEffect(DynamicValue numberCardsToDiscard, FilterCard filter, TargetController targetController, DynamicValue numberCardsToReveal) {
public DiscardCardYouChooseTargetEffect(DynamicValue numberCardsToDiscard, FilterCard filter, DynamicValue numberCardsToReveal) {
super(Outcome.Discard);
this.targetController = targetController;
this.filter = filter;
this.revealAllCards = false;
this.numberCardsToReveal = numberCardsToReveal;
this.numberCardsToDiscard = numberCardsToDiscard;
staticText = this.setText();
}
protected DiscardCardYouChooseTargetEffect(final DiscardCardYouChooseTargetEffect effect) {
super(effect);
this.filter = effect.filter;
this.targetController = effect.targetController;
this.numberCardsToDiscard = effect.numberCardsToDiscard;
this.numberCardsToReveal = effect.numberCardsToReveal;
this.revealAllCards = effect.revealAllCards;
this.optional = effect.optional;
}
public void setOptional(boolean optional) {
public DiscardCardYouChooseTargetEffect setOptional(boolean optional) {
this.optional = optional;
staticText = this.setText();
return this;
}
@Override
@ -165,19 +144,13 @@ public class DiscardCardYouChooseTargetEffect extends OneShotEffect {
return new DiscardCardYouChooseTargetEffect(this);
}
private String setText() {
boolean discardMultipleCards = !numberCardsToDiscard.toString().equals("1");
StringBuilder sb = new StringBuilder("target ");
switch (targetController) {
case OPPONENT:
sb.append("opponent");
break;
case ANY:
sb.append("player");
break;
default:
throw new UnsupportedOperationException("target controller not supported");
@Override
public String getText(Mode mode) {
if (staticText != null && !staticText.isEmpty()) {
return staticText;
}
boolean discardMultipleCards = !numberCardsToDiscard.toString().equals("1");
StringBuilder sb = new StringBuilder(getTargetPointer().describeTargets(mode.getTargets(), "that player"));
sb.append(" reveals ");
if (revealAllCards) {
sb.append("their hand. You ");

View file

@ -0,0 +1,85 @@
package mage.abilities.effects.common.discard;
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.CardsImpl;
import mage.constants.Outcome;
import mage.filter.StaticFilters;
import mage.game.Game;
import mage.players.Player;
import mage.target.TargetCard;
import mage.target.common.TargetCardInHand;
import mage.util.CardUtil;
/**
* @author xenohedron
*/
public class LookTargetHandChooseDiscardEffect extends OneShotEffect {
private final boolean upTo;
private final DynamicValue numberToDiscard;
public LookTargetHandChooseDiscardEffect() {
this(false, 1);
}
public LookTargetHandChooseDiscardEffect(boolean upTo, int numberToDiscard) {
this(upTo, StaticValue.get(numberToDiscard));
}
public LookTargetHandChooseDiscardEffect(boolean upTo, DynamicValue numberToDiscard) {
super(Outcome.Discard);
this.upTo = upTo;
this.numberToDiscard = numberToDiscard;
}
protected LookTargetHandChooseDiscardEffect(final LookTargetHandChooseDiscardEffect effect) {
super(effect);
this.upTo = effect.upTo;
this.numberToDiscard = effect.numberToDiscard;
}
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(getTargetPointer().getFirst(game, source));
Player controller = game.getPlayer(source.getControllerId());
if (player == null || controller == null) {
return false;
}
int num = numberToDiscard.calculate(game, source, this);
if (num == 0) {
if (!player.getHand().isEmpty()) {
controller.lookAtCards("Looking at hand", player.getHand(), game);
}
return true;
}
TargetCard target = new TargetCardInHand(upTo ? 0 : num, num, num > 1 ? StaticFilters.FILTER_CARD_CARDS : StaticFilters.FILTER_CARD);
if (controller.choose(Outcome.Discard, player.getHand(), target, source, game)) {
player.discard(new CardsImpl(target.getTargets()), false, source, game);
}
return true;
}
@Override
public LookTargetHandChooseDiscardEffect copy() {
return new LookTargetHandChooseDiscardEffect(this);
}
@Override
public String getText(Mode mode) {
if (staticText != null && !staticText.isEmpty()) {
return staticText;
}
String numberValue = numberToDiscard instanceof StaticValue ?
CardUtil.numberToText(((StaticValue) numberToDiscard).getValue(), "a") : "X";
boolean plural = !numberValue.equals("a");
String targetDescription = getTargetPointer().describeTargets(mode.getTargets(), "that player");
return "look at " + targetDescription + "'s hand and choose " + (upTo ? "up to " : "") + numberValue
+ (plural ? " cards" : " card") + " from it. "
+ (targetDescription.equals("that player") ? "The" : "That")
+ " player discards " + (plural ? "those cards." : "that card.");
}
}