Skip to main content

manabrew_engine/ability/effects/
game_loss_effect.rs

1use super::{resolve_defined_player, EffectContext};
2use crate::replacement::replacement_handler::{apply_replacements, ReplacementEvent};
3use crate::replacement::GameLossReason;
4use crate::replacement::ReplacementResult;
5
6/// Resolve `SP$ GameLoss` — a player loses the game.
7///
8/// Mirrors Java `GameLossEffect.java`.
9///
10/// # Card script examples
11/// ```text
12/// A:SP$ GameLoss | Defined$ You
13/// A:SP$ GameLoss | Defined$ Opponent
14/// ```
15/// Struct form of this effect so it can participate in the
16/// `SpellAbilityEffect` trait hierarchy — mirrors Java's
17/// `GameLossEffect` class extending `SpellAbilityEffect`.
18#[manabrew_engine_macros::spell_effect(GameLossEffect)]
19fn resolve(ctx: &mut EffectContext, sa: &crate::spellability::SpellAbility) {
20    let controller = sa.activating_player;
21
22    let defined = sa.defined().unwrap_or("You");
23
24    let loser = resolve_defined_player(defined, controller, ctx.game).unwrap_or(controller);
25
26    if !ctx.game.player(loser).is_alive() {
27        return;
28    }
29
30    // Run GameLoss replacement effects (e.g. Platinum Angel).
31    let mut event = ReplacementEvent::GameLoss {
32        player: loser,
33        reason: GameLossReason::SpellEffect,
34    };
35    let result = apply_replacements(ctx.game, &mut event);
36    if result == ReplacementResult::Replaced {
37        return;
38    }
39
40    ctx.game
41        .player_mark_lost(loser, crate::replacement::GameLossReason::SpellEffect);
42
43    // SBA will determine if the game is over and set the winner
44    let alive = ctx.game.alive_players();
45    if alive.len() <= 1 {
46        ctx.game.game_over = true;
47        if alive.len() == 1 {
48            ctx.game.winner = Some(alive[0]);
49        }
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use crate::ability::spell_ability_effect::SpellAbilityEffect;
56    use std::collections::HashMap;
57
58    use crate::ability::effects::EffectContext;
59    use crate::agent::PassAgent;
60    use crate::game::GameState;
61    use crate::ids::PlayerId;
62    use crate::mana::ManaPool;
63    use crate::spellability::SpellAbility;
64    use crate::trigger::handler::TriggerHandler;
65
66    #[test]
67    fn game_loss_marks_player_lost() {
68        let mut game = GameState::new(&["Alice", "Bob"], 20);
69        let p0 = PlayerId(0);
70        let p1 = PlayerId(1);
71
72        let sa = SpellAbility::new_simple(None, p0, "SP$ GameLoss | Defined$ You");
73
74        let mut th = TriggerHandler::new();
75        let mut agents: Vec<Box<dyn crate::agent::PlayerAgent>> =
76            vec![Box::new(PassAgent), Box::new(PassAgent)];
77        let mut mp = vec![ManaPool::default(), ManaPool::default()];
78        let templates = HashMap::new();
79        let templates_variants = HashMap::new();
80        let token_fallback = HashMap::new();
81        let edition_dates: HashMap<String, String> = HashMap::new();
82        let mut rng_adapter = crate::game_rng::ThreadRngAdapter;
83        let mut ctx = EffectContext {
84            game: &mut game,
85            combat: None,
86            agents: &mut agents,
87            trigger_handler: &mut th,
88            token_templates: &templates,
89            token_art_variants: &templates_variants,
90            token_fallback: &token_fallback,
91            edition_dates: &edition_dates,
92            mana_pools: &mut mp,
93            parent_target_card: None,
94            rng: &mut rng_adapter,
95        };
96        super::GameLossEffect::resolve(&mut ctx, &sa);
97
98        assert!(ctx.game.player(p0).has_lost);
99        assert!(!ctx.game.player(p1).has_lost);
100        assert!(ctx.game.game_over);
101        assert_eq!(ctx.game.winner, Some(p1));
102    }
103}