fix melee ability counting planeswalkers

This commit is contained in:
Evan Kranzler 2022-11-27 17:28:33 -05:00
parent 7259fa9af1
commit 2d2438ba66

View file

@ -1,7 +1,5 @@
package mage.abilities.keyword; package mage.abilities.keyword;
import java.util.*;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.common.AttacksTriggeredAbility; import mage.abilities.common.AttacksTriggeredAbility;
import mage.abilities.dynamicvalue.DynamicValue; import mage.abilities.dynamicvalue.DynamicValue;
@ -11,17 +9,17 @@ import mage.constants.Duration;
import mage.constants.WatcherScope; import mage.constants.WatcherScope;
import mage.game.Game; import mage.game.Game;
import mage.game.events.GameEvent; import mage.game.events.GameEvent;
import mage.game.events.GameEvent.EventType;
import mage.watchers.Watcher; import mage.watchers.Watcher;
import java.util.*;
/** /**
*
* @author emerald000 * @author emerald000
*/ */
public class MeleeAbility extends AttacksTriggeredAbility { public class MeleeAbility extends AttacksTriggeredAbility {
public MeleeAbility() { public MeleeAbility() {
super(new BoostSourceEffect(new MeleeDynamicValue(), new MeleeDynamicValue(), Duration.EndOfTurn), false); super(new BoostSourceEffect(MeleeDynamicValue.instance, MeleeDynamicValue.instance, Duration.EndOfTurn), false);
this.addWatcher(new MeleeWatcher()); this.addWatcher(new MeleeWatcher());
} }
@ -42,7 +40,7 @@ public class MeleeAbility extends AttacksTriggeredAbility {
class MeleeWatcher extends Watcher { class MeleeWatcher extends Watcher {
private Map<UUID, Set<UUID>> playersAttacked = new HashMap<>(0); private final Map<UUID, Set<UUID>> playersAttacked = new HashMap<>(0);
public MeleeWatcher() { public MeleeWatcher() {
super(WatcherScope.GAME); super(WatcherScope.GAME);
@ -50,55 +48,41 @@ class MeleeWatcher extends Watcher {
@Override @Override
public void watch(GameEvent event, Game game) { public void watch(GameEvent event, Game game) {
if (event.getType() == GameEvent.EventType.BEGIN_COMBAT_STEP_PRE) { switch (event.getType()) {
this.playersAttacked.clear(); case BEGIN_COMBAT_STEP_PRE:
} this.playersAttacked.clear();
else if (event.getType() == GameEvent.EventType.ATTACKER_DECLARED) { return;
Set<UUID> attackedPlayers = this.playersAttacked.getOrDefault(event.getPlayerId(), new HashSet<>(1)); case ATTACKER_DECLARED:
attackedPlayers.add(event.getTargetId()); if (game.getPlayer(event.getTargetId()) == null) {
this.playersAttacked.put(event.getPlayerId(), attackedPlayers); return;
}
this.playersAttacked
.computeIfAbsent(event.getPlayerId(), x -> new HashSet<>())
.add(event.getTargetId());
} }
} }
public int getNumberOfAttackedPlayers(UUID attackerId) { public static int getNumberOfAttackedPlayers(UUID attackerId, Game game) {
if (this.playersAttacked.get(attackerId) != null) { return game
return this.playersAttacked.get(attackerId).size(); .getState()
} .getWatcher(MeleeWatcher.class)
return 0; .playersAttacked
.getOrDefault(attackerId, Collections.emptySet())
.size();
} }
} }
class MeleeDynamicValue implements DynamicValue { enum MeleeDynamicValue implements DynamicValue {
instance;
private boolean valueChecked;
private int lockedInValue;
public MeleeDynamicValue() {
super();
}
protected MeleeDynamicValue(final MeleeDynamicValue dynamicValue) {
super();
valueChecked = dynamicValue.valueChecked;
lockedInValue = dynamicValue.lockedInValue;
}
@Override @Override
public int calculate(Game game, Ability sourceAbility, Effect effect) { public int calculate(Game game, Ability sourceAbility, Effect effect) {
MeleeWatcher watcher = game.getState().getWatcher(MeleeWatcher.class); return MeleeWatcher.getNumberOfAttackedPlayers(sourceAbility.getSourceId(), game);
if (watcher != null) {
if (!valueChecked) {
this.lockedInValue = watcher.getNumberOfAttackedPlayers(sourceAbility.getControllerId());
valueChecked = true;
}
return this.lockedInValue;
}
return 0;
} }
@Override @Override
public MeleeDynamicValue copy() { public MeleeDynamicValue copy() {
return new MeleeDynamicValue(this); return this;
} }
@Override @Override