use of mage.watchers.common.SpellsCastWatcher in project mage by magefree.
the class FirstSpellOpponentsTurnTriggeredAbility method checkTrigger.
@Override
public boolean checkTrigger(GameEvent event, Game game) {
if (// ignore controller turn
game.isActivePlayer(this.getControllerId()) || !super.checkTrigger(event, game) || !game.getOpponents(this.getControllerId()).contains(game.getActivePlayerId())) {
return false;
}
SpellsCastWatcher watcher = game.getState().getWatcher(SpellsCastWatcher.class);
if (watcher == null) {
return false;
}
List<Spell> spells = watcher.getSpellsCastThisTurn(event.getPlayerId());
return spells != null && spells.size() == 1;
}
use of mage.watchers.common.SpellsCastWatcher in project mage by magefree.
the class PlaneswalkersMischiefCondition method apply.
@Override
public boolean apply(Game game, Ability source) {
Player opponent = game.getPlayer(source.getFirstTarget());
if (opponent != null && opponent.getHand().size() > 0) {
Card revealedCard = opponent.getHand().getRandom(game);
if (revealedCard == null) {
return false;
}
Cards cards = new CardsImpl(revealedCard);
opponent.revealCards(source, cards, game);
if (revealedCard.isInstant(game) || revealedCard.isSorcery(game)) {
opponent.moveCardToExileWithInfo(revealedCard, source.getSourceId(), "Planeswalker's Mischief", source, game, Zone.HAND, true);
AsThoughEffect effect = new PlaneswalkersMischiefCastFromExileEffect();
effect.setTargetPointer(new FixedTarget(revealedCard.getId()));
game.addEffect(effect, source);
OneShotEffect effect2 = new ReturnFromExileEffect(Zone.HAND);
Condition condition = new PlaneswalkersMischiefCondition(source.getSourceId(), revealedCard.getId());
ConditionalOneShotEffect effect3 = new ConditionalOneShotEffect(effect2, condition, "if you haven't cast it, return it to its owner's hand.");
DelayedTriggeredAbility delayedAbility = new AtTheBeginOfNextEndStepDelayedTriggeredAbility(effect3);
delayedAbility.addWatcher(new SpellsCastWatcher());
game.addDelayedTriggeredAbility(delayedAbility, source);
return true;
}
}
return false;
}
use of mage.watchers.common.SpellsCastWatcher in project mage by magefree.
the class TwinningGlassEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
List<Spell> spells = new ArrayList<>();
Permanent twinningGlass = game.getPermanent(source.getSourceId());
Player controller = game.getPlayer(source.getControllerId());
SpellsCastWatcher watcher = game.getState().getWatcher(SpellsCastWatcher.class);
if (twinningGlass == null) {
twinningGlass = (Permanent) game.getLastKnownInformation(source.getSourceId(), Zone.BATTLEFIELD);
}
if (twinningGlass != null && controller != null && watcher != null) {
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
if (watcher.getSpellsCastThisTurn(playerId) != null) {
for (Spell spell : watcher.getSpellsCastThisTurn(playerId)) {
spells.add(spell);
}
}
}
if (spells.isEmpty()) {
return false;
}
List<NamePredicate> predicates = spells.stream().map(Spell::getName).filter(Objects::nonNull).filter(s -> !s.isEmpty()).map(NamePredicate::new).collect(Collectors.toList());
FilterNonlandCard filterCard = new FilterNonlandCard("nonland card that was cast this turn");
filterCard.add(Predicates.or(predicates));
TargetCard target = new TargetCard(0, 1, Zone.HAND, filterCard);
target.withChooseHint("free cast");
if (controller.choose(Outcome.PlayForFree, controller.getHand(), target, game)) {
Card chosenCard = game.getCard(target.getFirstTarget());
if (chosenCard != null) {
game.getState().setValue("PlayFromNotOwnHandZone" + chosenCard.getId(), Boolean.TRUE);
Boolean cardWasCast = controller.cast(controller.chooseAbilityForCast(chosenCard, game, true), game, true, new ApprovingObject(source, game));
game.getState().setValue("PlayFromNotOwnHandZone" + chosenCard.getId(), null);
return cardWasCast;
}
}
}
return false;
}
use of mage.watchers.common.SpellsCastWatcher in project mage by magefree.
the class ShowOfConfidenceTest method test_SpellsCastWatcher.
@Test
public void test_SpellsCastWatcher() {
// When you cast this spell, copy it for each other instant or sorcery spell you've cast this turn. You may choose new targets for the copies.
// Put a +1/+1 counter on target creature. It gains vigilance until end of turn.
// {1}{W}
addCard(Zone.HAND, playerA, "Show of Confidence");
addCard(Zone.BATTLEFIELD, playerA, "Plains", 2);
//
addCard(Zone.BATTLEFIELD, playerA, "Balduvian Bears", 1);
addCard(Zone.HAND, playerA, "Lightning Bolt", 1);
addCard(Zone.GRAVEYARD, playerA, "Lightning Bolt", 1);
addCard(Zone.BATTLEFIELD, playerA, "Mountain", 2);
//
// Each instant and sorcery card in your graveyard gains flashback until end of turn. The flashback cost is equal to its mana cost.
// {3}{R}
addCard(Zone.HAND, playerA, "Past in Flames", 1);
addCard(Zone.BATTLEFIELD, playerA, "Mountain", 4);
// prepare NPE error for watcher
runCode("prepare watcher's NPE", 1, PhaseStep.PRECOMBAT_MAIN, playerA, (info, player, game) -> {
// possible bug: NPE after wrong computeIfAbsent usage (lists desync)
SpellsCastWatcher watcher = game.getState().getWatcher(SpellsCastWatcher.class);
watcher.getSpellsCastThisTurn(player.getId());
});
// prepare flashback
activateManaAbility(1, PhaseStep.PRECOMBAT_MAIN, playerA, "{T}: Add {R}", 4);
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Past in Flames");
waitStackResolved(1, PhaseStep.PRECOMBAT_MAIN);
// prepare spells count (1x from hand, 1x from grave)
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Lightning Bolt", playerB);
activateAbility(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Flashback {R}", playerB);
waitStackResolved(1, PhaseStep.PRECOMBAT_MAIN);
// cast and copy 3x
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Show of Confidence", "Balduvian Bears");
// no change target (copy 1)
setChoice(playerA, false);
// no change target (copy 2)
setChoice(playerA, false);
// no change target (copy 3)
setChoice(playerA, false);
// test watcher's copy
waitStackResolved(1, PhaseStep.PRECOMBAT_MAIN);
runCode("test watcher's copy", 1, PhaseStep.PRECOMBAT_MAIN, playerA, (info, player, game) -> {
SpellsCastWatcher watcher = game.getState().getWatcher(SpellsCastWatcher.class);
SpellsCastWatcher copiedWatcher = watcher.copy();
Assert.assertEquals("original watcher must see 4 spells", 4, watcher.getSpellsCastThisTurn(player.getId()).size());
Assert.assertEquals("copied watcher must see 4 spells", 4, copiedWatcher.getSpellsCastThisTurn(player.getId()).size());
});
setStrictChooseMode(true);
setStopAt(1, PhaseStep.END_TURN);
execute();
assertAllCommandsUsed();
assertCounterCount(playerA, "Balduvian Bears", CounterType.P1P1, 4);
assertAbility(playerA, "Balduvian Bears", VigilanceAbility.getInstance(), true);
}
use of mage.watchers.common.SpellsCastWatcher in project mage by magefree.
the class MonkClassCastEffect method applies.
@Override
public boolean applies(UUID sourceId, Ability source, UUID affectedControllerId, Game game) {
UUID mainCardId = CardUtil.getMainCardId(game, sourceId);
if (!mainCardId.equals(source.getSourceId()) || !source.isControlledBy(affectedControllerId)) {
return false;
}
Card card = game.getCard(sourceId);
SpellsCastWatcher watcher = game.getState().getWatcher(SpellsCastWatcher.class);
return card != null && watcher != null && !card.isLand(game) && watcher.getSpellsCastThisTurn(affectedControllerId).size() > 0;
}
Aggregations