updated implementation of abilities which trigger on the second card in a turn being drawn

This commit is contained in:
Evan Kranzler 2020-09-23 19:52:30 -04:00
parent f450de0d51
commit b7e5d44b9d
2 changed files with 60 additions and 34 deletions

View file

@ -1,58 +1,46 @@
package mage.abilities.common;
import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.dynamicvalue.common.CardsDrawnThisTurnDynamicValue;
import mage.abilities.effects.Effect;
import mage.abilities.hint.Hint;
import mage.abilities.hint.ValueHint;
import mage.constants.WatcherScope;
import mage.constants.Zone;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.watchers.common.CardsAmountDrawnThisTurnWatcher;
import mage.watchers.Watcher;
import java.util.*;
/**
* @author TheElk801
*/
public class DrawSecondCardTriggeredAbility extends TriggeredAbilityImpl {
private boolean triggeredOnce = false;
private static final Hint hint = new ValueHint(
"Cards drawn this turn", CardsDrawnThisTurnDynamicValue.instance
);
public DrawSecondCardTriggeredAbility(Effect effect, boolean optional) {
super(Zone.ALL, effect, optional);
this.addWatcher(new CardsAmountDrawnThisTurnWatcher());
super(Zone.BATTLEFIELD, effect, optional);
this.addWatcher(new DrawSecondCardWatcher());
this.addHint(hint);
}
private DrawSecondCardTriggeredAbility(final DrawSecondCardTriggeredAbility ability) {
super(ability);
this.triggeredOnce = ability.triggeredOnce;
}
@Override
public boolean checkEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.DREW_CARD
|| event.getType() == GameEvent.EventType.END_PHASE_POST;
return event.getType() == GameEvent.EventType.DREW_CARD;
}
@Override
public boolean checkTrigger(GameEvent event, Game game) {
if (event.getType() == GameEvent.EventType.END_PHASE_POST) {
triggeredOnce = false;
return false;
}
if (event.getType() != GameEvent.EventType.DREW_CARD
|| !event.getPlayerId().equals(controllerId)
|| game.getPermanent(sourceId) == null) {
return false;
}
if (triggeredOnce) {
return false;
}
CardsAmountDrawnThisTurnWatcher watcher = game.getState().getWatcher(CardsAmountDrawnThisTurnWatcher.class);
if (watcher == null) {
return false;
}
if (watcher.getAmountCardsDrawn(controllerId) > 1) {
triggeredOnce = true;
return true;
}
return false;
DrawSecondCardWatcher watcher = game.getState().getWatcher(DrawSecondCardWatcher.class);
return watcher != null && watcher.checkEvent(getControllerId(), event);
}
@Override
@ -65,3 +53,36 @@ public class DrawSecondCardTriggeredAbility extends TriggeredAbilityImpl {
return new DrawSecondCardTriggeredAbility(this);
}
}
class DrawSecondCardWatcher extends Watcher {
private final Set<UUID> drewOnce = new HashSet<>();
private final Map<UUID, UUID> secondDrawMap = new HashMap<>();
DrawSecondCardWatcher() {
super(WatcherScope.GAME);
}
@Override
public void watch(GameEvent event, Game game) {
if (event.getType() != GameEvent.EventType.DREW_CARD) {
return;
}
if (drewOnce.contains(event.getPlayerId())) {
secondDrawMap.putIfAbsent(event.getPlayerId(), event.getId());
} else {
drewOnce.add(event.getPlayerId());
}
}
@Override
public void reset() {
super.reset();
drewOnce.clear();
secondDrawMap.clear();
}
boolean checkEvent(UUID playerId, GameEvent event) {
return event.getId().equals(secondDrawMap.getOrDefault(playerId, null));
}
}

View file

@ -1,14 +1,13 @@
package mage.game.events;
import mage.ApprovingObject;
import mage.MageIdentifier;
import mage.constants.Zone;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import mage.ApprovingObject;
import mage.MageIdentifier;
/**
* @author BetaSteward_at_googlemail.com
@ -16,6 +15,7 @@ import mage.MageIdentifier;
public class GameEvent implements Serializable {
protected EventType type;
protected UUID id;
protected UUID targetId;
protected UUID sourceId;
protected UUID playerId;
@ -426,12 +426,12 @@ public class GameEvent implements Serializable {
}
private GameEvent(EventType type, UUID customEventType,
UUID targetId, UUID sourceId, UUID playerId, int amount, boolean flag) {
UUID targetId, UUID sourceId, UUID playerId, int amount, boolean flag) {
this(type, customEventType, targetId, sourceId, playerId, amount, flag, null);
}
private GameEvent(EventType type, UUID customEventType,
UUID targetId, UUID sourceId, UUID playerId, int amount, boolean flag, ApprovingObject approvingObject) {
UUID targetId, UUID sourceId, UUID playerId, int amount, boolean flag, ApprovingObject approvingObject) {
this.type = type;
this.customEventType = customEventType;
this.targetId = targetId;
@ -440,6 +440,7 @@ public class GameEvent implements Serializable {
this.playerId = playerId;
this.flag = flag;
this.approvingObject = approvingObject;
this.id = UUID.randomUUID();
}
public EventType getType() {
@ -450,6 +451,10 @@ public class GameEvent implements Serializable {
return customEventType;
}
public UUID getId() {
return id;
}
public UUID getTargetId() {
return targetId;
}
@ -559,7 +564,7 @@ public class GameEvent implements Serializable {
}
}
}
public boolean hasApprovingIdentifier(MageIdentifier identifier) {
if (approvingObject == null) {
return false;