From 62345ce32c59672505f7fbcf4b39a4acce8bc119 Mon Sep 17 00:00:00 2001
From: Merlingilb <daniel.h.e@outlook.de>
Date: Fri, 31 Mar 2023 00:25:00 +0200
Subject: [PATCH] MOM: Added card "Chandra, Hope's Beacon" and its abilities
 and effects (#10042)

Co-authored-by: Daniel Eberhard <daniel.h.e@gmx.de>
---
 .../src/mage/cards/c/ChandraHopesBeacon.java  | 169 ++++++++++++++++++
 .../src/mage/sets/MarchOfTheMachine.java      |   2 +
 2 files changed, 171 insertions(+)
 create mode 100644 Mage.Sets/src/mage/cards/c/ChandraHopesBeacon.java

diff --git a/Mage.Sets/src/mage/cards/c/ChandraHopesBeacon.java b/Mage.Sets/src/mage/cards/c/ChandraHopesBeacon.java
new file mode 100644
index 0000000000..0a30517ec1
--- /dev/null
+++ b/Mage.Sets/src/mage/cards/c/ChandraHopesBeacon.java
@@ -0,0 +1,169 @@
+package mage.cards.c;
+
+import mage.MageItem;
+import mage.MageObjectReference;
+import mage.abilities.Ability;
+import mage.abilities.LoyaltyAbility;
+import mage.abilities.common.SpellCastControllerTriggeredAbility;
+import mage.abilities.dynamicvalue.common.GetXLoyaltyValue;
+import mage.abilities.effects.AsThoughEffectImpl;
+import mage.abilities.effects.OneShotEffect;
+import mage.abilities.effects.common.*;
+import mage.abilities.effects.mana.AddManaInAnyCombinationEffect;
+import mage.cards.*;
+import mage.constants.*;
+import mage.filter.StaticFilters;
+import mage.game.Game;
+import mage.game.events.GameEvent;
+import mage.game.stack.Spell;
+import mage.players.Player;
+import mage.target.common.TargetAnyTarget;
+import mage.util.CardUtil;
+import mage.watchers.Watcher;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+public class ChandraHopesBeacon extends CardImpl {
+    public ChandraHopesBeacon(UUID ownerId, CardSetInfo setInfo) {
+        super(ownerId, setInfo, new CardType[]{CardType.PLANESWALKER}, "{4}{R}{R}");
+        this.addSuperType(SuperType.LEGENDARY);
+        this.addSubType(SubType.CHANDRA);
+        this.startingLoyalty = 5;
+
+        //Whenever you cast an instant or sorcery spell, copy it. You may choose new targets for the copy. This ability
+        //triggers only once each turn.
+        this.addAbility(new SpellCastControllerTriggeredAbility(
+                new CopyTargetSpellEffect(true).withSpellName("it"),
+                StaticFilters.FILTER_SPELL_AN_INSTANT_OR_SORCERY, false, true
+        ).setTriggersOnce(true));
+
+        //+2: Add two mana in any combination of colors.
+        this.addAbility(new LoyaltyAbility(new AddManaInAnyCombinationEffect(2), 2));
+
+        //+1: Exile the top five cards of your library. Until the end of your next turn, you may cast an instant or
+        //    sorcery spell from among those exiled cards.
+        this.addAbility(new LoyaltyAbility(new ChandraHopesBeaconEffect(), 1), new ChandraHopesBeaconWatcher());
+
+        //−X: Chandra, Hope’s Beacon deals X damage to each of up to two targets.
+        LoyaltyAbility loyaltyAbility = new LoyaltyAbility(new DamageTargetEffect(
+                GetXLoyaltyValue.instance, true, "each of up to two targets"
+        ));
+        loyaltyAbility.addTarget(new TargetAnyTarget(0, 2));
+        this.addAbility(loyaltyAbility);
+    }
+
+    private ChandraHopesBeacon(final ChandraHopesBeacon card) {
+        super(card);
+    }
+
+    @Override
+    public ChandraHopesBeacon copy() {
+        return new ChandraHopesBeacon(this);
+    }
+}
+
+class ChandraHopesBeaconEffect extends OneShotEffect {
+
+    ChandraHopesBeaconEffect() {
+        super(Outcome.Benefit);
+        staticText = "exile the top five cards of your library. " +
+                "Until the end of your next turn, you may cast an instant or sorcery spell from among those exiled cards";
+    }
+
+    private ChandraHopesBeaconEffect(final ChandraHopesBeaconEffect effect) {
+        super(effect);
+    }
+
+    @Override
+    public ChandraHopesBeaconEffect copy() {
+        return new ChandraHopesBeaconEffect(this);
+    }
+
+    @Override
+    public boolean apply(Game game, Ability source) {
+        Player player = game.getPlayer(source.getControllerId());
+        if (player == null) {
+            return false;
+        }
+        Cards cards = new CardsImpl(player.getLibrary().getTopCards(game, 5));
+        player.moveCards(cards, Zone.EXILED, source, game);
+        Cards instantsOrSorceries = new CardsImpl(cards.stream()
+                .map(game::getCard)
+                .filter(card -> card.isInstantOrSorcery(game))
+                .map(MageItem::getId)
+                .collect(Collectors.toSet()));
+        game.addEffect(new ChandraHopesBeaconPlayEffect(instantsOrSorceries, game), source);
+        return true;
+    }
+}
+
+class ChandraHopesBeaconPlayEffect extends AsThoughEffectImpl {
+
+    private final Set<MageObjectReference> morSet = new HashSet<>();
+
+    ChandraHopesBeaconPlayEffect(Cards cards, Game game) {
+        super(AsThoughEffectType.PLAY_FROM_NOT_OWN_HAND_ZONE, Duration.UntilEndOfYourNextTurn, Outcome.Benefit);
+        cards.stream()
+                .map(uuid -> new MageObjectReference(uuid, game))
+                .forEach(morSet::add);
+    }
+
+    private ChandraHopesBeaconPlayEffect(final ChandraHopesBeaconPlayEffect effect) {
+        super(effect);
+        this.morSet.addAll(effect.morSet);
+    }
+
+    @Override
+    public ChandraHopesBeaconPlayEffect copy() {
+        return new ChandraHopesBeaconPlayEffect(this);
+    }
+
+    @Override
+    public boolean apply(Game game, Ability source) {
+        return true;
+    }
+
+    @Override
+    public boolean applies(UUID sourceId, Ability source, UUID affectedControllerId, Game game) {
+        UUID objectIdToCast = CardUtil.getMainCardId(game, sourceId);
+        return source.isControlledBy(affectedControllerId)
+                && morSet.stream().anyMatch(mor -> mor.refersTo(objectIdToCast, game))
+                && ChandraHopesBeaconWatcher.checkRef(source, morSet, game);
+    }
+}
+
+class ChandraHopesBeaconWatcher extends Watcher {
+
+    private final Map<MageObjectReference, Set<MageObjectReference>> morMap = new HashMap<>();
+    private static final Set<MageObjectReference> emptySet = new HashSet<>();
+
+    ChandraHopesBeaconWatcher() {
+        super(WatcherScope.GAME);
+    }
+
+    @Override
+    public void watch(GameEvent event, Game game) {
+        if (event.getType() != GameEvent.EventType.SPELL_CAST
+                || event.getAdditionalReference() == null) {
+            return;
+        }
+        MageObjectReference mor = event.getAdditionalReference().getApprovingMageObjectReference();
+        Spell spell = game.getSpell(event.getTargetId());
+        if (mor == null || spell == null) {
+            return;
+        }
+        morMap.computeIfAbsent(mor, x -> new HashSet<>())
+                .add(new MageObjectReference(spell.getMainCard(), game, -1));
+    }
+
+    static boolean checkRef(Ability source, Set<MageObjectReference> morSet, Game game) {
+        ChandraHopesBeaconWatcher watcher = game.getState().getWatcher(ChandraHopesBeaconWatcher.class);
+        return watcher != null
+                && watcher
+                .morMap
+                .getOrDefault(new MageObjectReference(source.getSourceObject(game), game), emptySet)
+                .stream()
+                .noneMatch(morSet::contains);
+    }
+}
diff --git a/Mage.Sets/src/mage/sets/MarchOfTheMachine.java b/Mage.Sets/src/mage/sets/MarchOfTheMachine.java
index 6da893cbdb..82a5ab2d8c 100644
--- a/Mage.Sets/src/mage/sets/MarchOfTheMachine.java
+++ b/Mage.Sets/src/mage/sets/MarchOfTheMachine.java
@@ -23,6 +23,8 @@ public final class MarchOfTheMachine extends ExpansionSet {
         cards.add(new SetCardInfo("Alabaster Host Sanctifier", 4, Rarity.COMMON, mage.cards.a.AlabasterHostSanctifier.class));
         cards.add(new SetCardInfo("Bloodfell Caves", 267, Rarity.COMMON, mage.cards.b.BloodfellCaves.class));
         cards.add(new SetCardInfo("Blossoming Sands", 268, Rarity.COMMON, mage.cards.b.BlossomingSands.class));
+        cards.add(new SetCardInfo("Chandra, Hope's Beacon", 134, Rarity.MYTHIC, mage.cards.c.ChandraHopesBeacon.class, NON_FULL_USE_VARIOUS));
+        cards.add(new SetCardInfo("Chandra, Hope's Beacon", 321, Rarity.MYTHIC, mage.cards.c.ChandraHopesBeacon.class, NON_FULL_USE_VARIOUS));
         cards.add(new SetCardInfo("Dismal Backwater", 269, Rarity.COMMON, mage.cards.d.DismalBackwater.class));
         cards.add(new SetCardInfo("Faerie Mastermind", 58, Rarity.RARE, mage.cards.f.FaerieMastermind.class, NON_FULL_USE_VARIOUS));
         cards.add(new SetCardInfo("Faerie Mastermind", 352, Rarity.RARE, mage.cards.f.FaerieMastermind.class, NON_FULL_USE_VARIOUS));