Skip to main content

manabrew_engine/ability/effects/
end_turn_effect.rs

1use super::EffectContext;
2
3/// Resolve `SP$ EndTurn` — end the current turn.
4///
5/// Mirrors Java `EndTurnEffect.java`.
6/// Sets the `end_turn_requested` flag on `GameState`. The game loop checks
7/// this flag in the turn state machine to skip remaining phases and jump
8/// directly to the Cleanup step.
9///
10/// # Card script examples
11/// ```text
12/// A:SP$ EndTurn
13/// ```
14/// Struct form of this effect so it can participate in the
15/// `SpellAbilityEffect` trait hierarchy — mirrors Java's
16/// `EndTurnEffect` class extending `SpellAbilityEffect`.
17#[manabrew_engine_macros::spell_effect(EndTurnEffect)]
18fn resolve(ctx: &mut EffectContext, _sa: &crate::spellability::SpellAbility) {
19    // Clear the stack (exile all spells/abilities)
20    while ctx.game.stack.pop().is_some() {}
21    // Signal the game loop to skip to cleanup
22    ctx.game.end_turn_requested = true;
23}
24
25#[cfg(test)]
26mod tests {
27    use crate::ability::spell_ability_effect::SpellAbilityEffect;
28    use std::collections::HashMap;
29
30    use crate::ability::effects::EffectContext;
31    use crate::agent::PassAgent;
32    use crate::game::GameState;
33    use crate::ids::PlayerId;
34    use crate::mana::ManaPool;
35    use crate::spellability::SpellAbility;
36    use crate::trigger::handler::TriggerHandler;
37
38    #[test]
39    fn end_turn_sets_flag() {
40        let mut game = GameState::new(&["Alice", "Bob"], 20);
41        let p0 = PlayerId(0);
42
43        let sa = SpellAbility::new_simple(None, p0, "SP$ EndTurn");
44
45        let mut th = TriggerHandler::new();
46        let mut agents: Vec<Box<dyn crate::agent::PlayerAgent>> =
47            vec![Box::new(PassAgent), Box::new(PassAgent)];
48        let mut mp = vec![ManaPool::default(), ManaPool::default()];
49        let templates = HashMap::new();
50        let templates_variants = HashMap::new();
51        let token_fallback = HashMap::new();
52        let edition_dates: HashMap<String, String> = HashMap::new();
53        let mut rng_adapter = crate::game_rng::ThreadRngAdapter;
54        let mut ctx = EffectContext {
55            game: &mut game,
56            combat: None,
57            agents: &mut agents,
58            trigger_handler: &mut th,
59            token_templates: &templates,
60            token_art_variants: &templates_variants,
61            token_fallback: &token_fallback,
62            edition_dates: &edition_dates,
63            mana_pools: &mut mp,
64            parent_target_card: None,
65            rng: &mut rng_adapter,
66        };
67        super::EndTurnEffect::resolve(&mut ctx, &sa);
68
69        assert!(ctx.game.end_turn_requested);
70        // Stack should be empty after EndTurn
71        assert!(ctx.game.stack.is_empty());
72    }
73}