Fix #9562 and add test for Vazi

This commit is contained in:
Alex W. Jackson 2022-09-24 09:57:22 -04:00
parent 3f09836c64
commit 17f6ca6b22
3 changed files with 47 additions and 10 deletions

View file

@ -50,6 +50,7 @@ public class VaziKeenNegotiator extends CardImpl {
// {T}: Target opponent creates X Treasure tokens, where X is the number of Treasure tokens you created this turn. // {T}: Target opponent creates X Treasure tokens, where X is the number of Treasure tokens you created this turn.
Ability tapAbility = new SimpleActivatedAbility(new CreateTokenTargetEffect(new TreasureToken(), VaziKeenNegotiatorNumberOfTokensCreated.instance), new TapSourceCost()); Ability tapAbility = new SimpleActivatedAbility(new CreateTokenTargetEffect(new TreasureToken(), VaziKeenNegotiatorNumberOfTokensCreated.instance), new TapSourceCost());
tapAbility.addTarget(new TargetOpponent()); tapAbility.addTarget(new TargetOpponent());
tapAbility.addWatcher(new CreatedTokenWatcher());
this.addAbility(tapAbility); this.addAbility(tapAbility);
// Whenever an opponent casts a spell or activates an ability, // Whenever an opponent casts a spell or activates an ability,
@ -113,7 +114,7 @@ enum VaziKeenNegotiatorNumberOfTokensCreated implements DynamicValue {
@Override @Override
public int calculate(Game game, Ability sourceAbility, Effect effect) { public int calculate(Game game, Ability sourceAbility, Effect effect) {
return CreatedTokenWatcher.getTypeCreatedCountByPlayer(sourceAbility.getControllerId(), TreasureToken.class, game); return CreatedTokenWatcher.getTypeCreatedCountByPlayer(sourceAbility.getControllerId(), SubType.TREASURE, game);
} }
@Override @Override
@ -130,4 +131,4 @@ enum VaziKeenNegotiatorNumberOfTokensCreated implements DynamicValue {
public String toString() { public String toString() {
return "X"; return "X";
} }
} }

View file

@ -0,0 +1,31 @@
package org.mage.test.cards.single.ncc;
import mage.constants.PhaseStep;
import mage.constants.Zone;
import org.junit.Test;
import org.mage.test.serverside.base.CardTestPlayerBase;
/**
* @author awjackson
*/
public class VaziKeenNegotiatorTest extends CardTestPlayerBase {
@Test
public void testVaziActivatedAbility() {
addCard(Zone.BATTLEFIELD, playerA, "Mountain", 3);
addCard(Zone.BATTLEFIELD, playerA, "Vazi, Keen Negotiator");
addCard(Zone.HAND, playerA, "Krenko's Command"); // Create two 1/1 red Goblin creature tokens
addCard(Zone.HAND, playerA, "Strike It Rich"); // Create a Treasure token
setStrictChooseMode(true);
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Krenko's Command", true);
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Strike It Rich", true);
activateAbility(1, PhaseStep.PRECOMBAT_MAIN, playerA, "{T}: Target opponent creates", playerB);
setStopAt(1, PhaseStep.POSTCOMBAT_MAIN);
execute();
assertPermanentCount(playerA, "Goblin Token", 2);
assertPermanentCount(playerA, "Treasure Token", 1);
assertPermanentCount(playerB, "Treasure Token", 1);
}
}

View file

@ -4,10 +4,12 @@ import mage.cards.Card;
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.permanent.token.Token; import mage.game.permanent.Permanent;
import mage.constants.SubType;
import mage.util.CardUtil; import mage.util.CardUtil;
import mage.watchers.Watcher; import mage.watchers.Watcher;
import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -19,7 +21,7 @@ public class CreatedTokenWatcher extends Watcher {
// Player ID to Number of tokens created // Player ID to Number of tokens created
private final Map<UUID, Integer> playerMap = new HashMap<>(); private final Map<UUID, Integer> playerMap = new HashMap<>();
private final Map<UUID, Map<Class<? extends Token>, Integer>> tokenCreatedMap = new HashMap<>(); private final Map<UUID, Map<SubType, Integer>> tokenCreatedMap = new HashMap<>();
public CreatedTokenWatcher() { public CreatedTokenWatcher() {
super(WatcherScope.GAME); super(WatcherScope.GAME);
@ -31,9 +33,12 @@ public class CreatedTokenWatcher extends Watcher {
playerMap.compute(event.getPlayerId(), CardUtil::setOrIncrementValue); playerMap.compute(event.getPlayerId(), CardUtil::setOrIncrementValue);
tokenCreatedMap.putIfAbsent(event.getPlayerId(), new HashMap<>()); tokenCreatedMap.putIfAbsent(event.getPlayerId(), new HashMap<>());
Class<? extends Token> tokenClazz = ((Token) game.getPermanent(event.getTargetId())).getClass(); Permanent token = game.getPermanent(event.getTargetId());
Map<Class<? extends Token>, Integer> playersTokens = tokenCreatedMap.getOrDefault(event.getPlayerId(), new HashMap<>()); Map<SubType, Integer> playersTokens = tokenCreatedMap.getOrDefault(event.getPlayerId(), new EnumMap<>(SubType.class));
playersTokens.compute(tokenClazz, CardUtil::setOrIncrementValue); // TODO: this doesn't handle tokens that are all creature types
for (SubType subType : token.getSubtype(game)) {
playersTokens.compute(subType, CardUtil::setOrIncrementValue);
}
tokenCreatedMap.put(event.getPlayerId(), playersTokens); tokenCreatedMap.put(event.getPlayerId(), playersTokens);
} }
} }
@ -56,12 +61,12 @@ public class CreatedTokenWatcher extends Watcher {
.getOrDefault(playerId, 0); .getOrDefault(playerId, 0);
} }
public static int getTypeCreatedCountByPlayer(UUID playerId, Class<? extends Token> tokenCLazz, Game game) { public static int getTypeCreatedCountByPlayer(UUID playerId, SubType tokenType, Game game) {
return game return game
.getState() .getState()
.getWatcher(CreatedTokenWatcher.class) .getWatcher(CreatedTokenWatcher.class)
.tokenCreatedMap .tokenCreatedMap
.getOrDefault(playerId, new HashMap<>()) .getOrDefault(playerId, new EnumMap<>(SubType.class))
.getOrDefault(tokenCLazz, 0); .getOrDefault(tokenType, 0);
} }
} }