From ea513386fb9fafd3fac8dadf2169d4f3418d0d36 Mon Sep 17 00:00:00 2001 From: Susucre <34709007+Susucre@users.noreply.github.com> Date: Fri, 18 Aug 2023 21:17:59 +0200 Subject: [PATCH] [WOE] Implement Johann, Apprentice Sorcerer (#10877) --- .../cards/j/JohannApprenticeSorcerer.java | 165 ++++++++++++++++++ Mage.Sets/src/mage/sets/WildsOfEldraine.java | 1 + Mage/src/main/java/mage/MageIdentifier.java | 3 +- 3 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 Mage.Sets/src/mage/cards/j/JohannApprenticeSorcerer.java diff --git a/Mage.Sets/src/mage/cards/j/JohannApprenticeSorcerer.java b/Mage.Sets/src/mage/cards/j/JohannApprenticeSorcerer.java new file mode 100644 index 00000000000..ac0a9e31e43 --- /dev/null +++ b/Mage.Sets/src/mage/cards/j/JohannApprenticeSorcerer.java @@ -0,0 +1,165 @@ +package mage.cards.j; + +import mage.MageIdentifier; +import mage.MageInt; +import mage.MageObjectReference; +import mage.abilities.Ability; +import mage.abilities.common.SimpleStaticAbility; +import mage.abilities.effects.AsThoughEffectImpl; +import mage.abilities.effects.common.continuous.LookAtTopCardOfLibraryAnyTimeEffect; +import mage.abilities.hint.Hint; +import mage.cards.Card; +import mage.cards.CardImpl; +import mage.cards.CardSetInfo; +import mage.constants.*; +import mage.game.Game; +import mage.game.events.GameEvent; +import mage.game.permanent.Permanent; +import mage.players.Player; +import mage.watchers.Watcher; + +import java.util.*; + +/** + * @author Susucr + */ +public final class JohannApprenticeSorcerer extends CardImpl { + + public JohannApprenticeSorcerer(UUID ownerId, CardSetInfo setInfo) { + super(ownerId, setInfo, new CardType[]{CardType.CREATURE}, "{2}{U}{R}"); + + this.supertype.add(SuperType.LEGENDARY); + this.subtype.add(SubType.HUMAN); + this.subtype.add(SubType.WIZARD); + this.power = new MageInt(2); + this.toughness = new MageInt(5); + + // You may look at the top card of your library any time. + this.addAbility(new SimpleStaticAbility(new LookAtTopCardOfLibraryAnyTimeEffect())); + + // Once each turn, you may cast an instant or sorcery spell from the top of your library. + this.addAbility( + new SimpleStaticAbility(new JohannApprenticeSorcererPlayTopEffect()) + .setIdentifier(MageIdentifier.JohannApprenticeSorcererWatcher) + .addHint(JohannApprenticeSorcererHint.instance), + new JohannApprenticeSorcererWatcher() + ); + } + + private JohannApprenticeSorcerer(final JohannApprenticeSorcerer card) { + super(card); + } + + @Override + public JohannApprenticeSorcerer copy() { + return new JohannApprenticeSorcerer(this); + } +} + +enum JohannApprenticeSorcererHint implements Hint { + instance; + + @Override + public String getText(Game game, Ability ability) { + JohannApprenticeSorcererWatcher watcher = game.getState().getWatcher(JohannApprenticeSorcererWatcher.class); + if (watcher != null) { + boolean used = watcher.isAbilityUsed(ability.getControllerId(), new MageObjectReference(ability.getSourceId(), game)); + if (used) { + Player player = game.getPlayer(ability.getControllerId()); + if (player != null) { + return "A spell has been cast by " + player.getLogName() + " with {this} this turn."; + } + } + } + + return ""; + } + + @Override + public JohannApprenticeSorcererHint copy() { + return this; + } +} + +class JohannApprenticeSorcererPlayTopEffect extends AsThoughEffectImpl { + + public JohannApprenticeSorcererPlayTopEffect() { + super(AsThoughEffectType.PLAY_FROM_NOT_OWN_HAND_ZONE, Duration.WhileOnBattlefield, Outcome.Benefit, true); + staticText = "Once each turn, you may cast an instant or sorcery spell from the top of your library"; + } + + private JohannApprenticeSorcererPlayTopEffect(final JohannApprenticeSorcererPlayTopEffect effect) { + super(effect); + } + + @Override + public JohannApprenticeSorcererPlayTopEffect copy() { + return new JohannApprenticeSorcererPlayTopEffect(this); + } + + @Override + public boolean apply(Game game, Ability source) { + return true; + } + + @Override + public boolean applies(UUID objectId, Ability source, UUID affectedControllerId, Game game) { + // Only applies for the controller of the ability. + if (!affectedControllerId.equals(source.getControllerId())) { + return false; + } + + Player controller = game.getPlayer(source.getControllerId()); + JohannApprenticeSorcererWatcher watcher = game.getState().getWatcher(JohannApprenticeSorcererWatcher.class); + Permanent sourceObject = game.getPermanent(source.getSourceId()); + if (controller == null || watcher == null || sourceObject == null) { + return false; + } + + // Has the ability already been used this turn by the player? + if (watcher.isAbilityUsed(controller.getId(), new MageObjectReference(sourceObject, game))) { + return false; + } + + Card card = game.getCard(objectId); + Card topCard = controller.getLibrary().getFromTop(game); + // Is the card attempted to be played the top card of the library? + if (card == null || topCard == null || !topCard.getId().equals(card.getMainCard().getId())) { + return false; + } + + // Only works for instant & sorcery. + return card.isInstantOrSorcery(game); + } +} + +class JohannApprenticeSorcererWatcher extends Watcher { + + // player -> set of all permanent's mor that already used their once per turn Approval. + private final Map> usedFrom = new HashMap<>(); + + public JohannApprenticeSorcererWatcher() { + super(WatcherScope.GAME); + } + + @Override + public void watch(GameEvent event, Game game) { + UUID playerId = event.getPlayerId(); + if (event.getType() == GameEvent.EventType.SPELL_CAST + && event.hasApprovingIdentifier(MageIdentifier.JohannApprenticeSorcererWatcher) + && playerId != null) { + usedFrom.computeIfAbsent(playerId, k -> new HashSet<>()) + .add(event.getAdditionalReference().getApprovingMageObjectReference()); + } + } + + @Override + public void reset() { + super.reset(); + usedFrom.clear(); + } + + public boolean isAbilityUsed(UUID playerId, MageObjectReference mor) { + return usedFrom.getOrDefault(playerId, new HashSet<>()).contains(mor); + } +} diff --git a/Mage.Sets/src/mage/sets/WildsOfEldraine.java b/Mage.Sets/src/mage/sets/WildsOfEldraine.java index e836b940e22..0b4c576f603 100644 --- a/Mage.Sets/src/mage/sets/WildsOfEldraine.java +++ b/Mage.Sets/src/mage/sets/WildsOfEldraine.java @@ -52,6 +52,7 @@ public final class WildsOfEldraine extends ExpansionSet { cards.add(new SetCardInfo("Hopeless Nightmare", 95, Rarity.COMMON, mage.cards.h.HopelessNightmare.class)); cards.add(new SetCardInfo("Howling Galefang", 175, Rarity.UNCOMMON, mage.cards.h.HowlingGalefang.class)); cards.add(new SetCardInfo("Island", 263, Rarity.LAND, mage.cards.basiclands.Island.class, NON_FULL_USE_VARIOUS)); + cards.add(new SetCardInfo("Johann, Apprentice Sorcerer", 207, Rarity.UNCOMMON, mage.cards.j.JohannApprenticeSorcerer.class)); cards.add(new SetCardInfo("Kellan, the Fae-Blooded", 230, Rarity.MYTHIC, mage.cards.k.KellanTheFaeBlooded.class)); cards.add(new SetCardInfo("Knight of Doves", 19, Rarity.UNCOMMON, mage.cards.k.KnightOfDoves.class)); cards.add(new SetCardInfo("Lich-Knights' Conquest", 96, Rarity.RARE, mage.cards.l.LichKnightsConquest.class)); diff --git a/Mage/src/main/java/mage/MageIdentifier.java b/Mage/src/main/java/mage/MageIdentifier.java index 3830d8085db..63af34c517a 100644 --- a/Mage/src/main/java/mage/MageIdentifier.java +++ b/Mage/src/main/java/mage/MageIdentifier.java @@ -19,5 +19,6 @@ public enum MageIdentifier { WishWatcher, GlimpseTheCosmosWatcher, SerraParagonWatcher, - OneWithTheMultiverseWatcher + OneWithTheMultiverseWatcher, + JohannApprenticeSorcererWatcher }