Skip to main content

manabrew_engine/trigger/
trigger_exploited.rs

1use serde::{Deserialize, Serialize};
2
3use crate::event::RunParams;
4use crate::game::GameState;
5use crate::parsing::{keys, Params};
6use crate::spellability::SpellAbility;
7use crate::trigger::TriggerType;
8
9use super::trigger::{Trigger, TriggerBehavior};
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct TriggerExploited {
13    pub valid_card: Option<crate::parsing::CompiledSelector>,
14    pub valid_source: Option<crate::parsing::CompiledSelector>,
15}
16
17impl TriggerExploited {
18    pub fn parse(params: &Params) -> Box<dyn TriggerBehavior> {
19        Box::new(Self {
20            valid_card: params.selector_cloned(keys::VALID_CARD),
21            valid_source: params.selector_cloned(keys::VALID_SOURCE),
22        })
23    }
24}
25
26#[typetag::serde]
27impl TriggerBehavior for TriggerExploited {
28    fn trigger_type(&self) -> TriggerType {
29        TriggerType::Exploited
30    }
31
32    fn perform_test(&self, trigger: &Trigger, params: &RunParams, game: &GameState) -> bool {
33        if !trigger.matches_optional_valid_card_filter(
34            &self.valid_card,
35            params.exploited_card,
36            game,
37        ) {
38            return false;
39        }
40        trigger.matches_optional_valid_card_filter(&self.valid_source, params.card, game)
41    }
42
43    fn set_triggering_objects(
44        &self,
45        _trigger: &Trigger,
46        sa: &mut SpellAbility,
47        params: &RunParams,
48        _game: &GameState,
49    ) {
50        if let Some(exploited) = params.exploited_card {
51            sa.set_triggering_object(
52                crate::ability::AbilityKey::Exploited,
53                exploited.0.to_string(),
54            );
55        }
56        if let Some(card) = params.card {
57            sa.set_triggering_object(crate::ability::AbilityKey::Card, card.0.to_string());
58        }
59    }
60
61    fn get_important_stack_objects(&self, _trigger: &Trigger, sa: &SpellAbility) -> String {
62        // Java: "Exploited: " + Exploited + ", Exploiter: " + Card
63        format!(
64            "Exploited: {}, Exploiter: {}",
65            sa.get_triggering_object(crate::ability::AbilityKey::Exploited)
66                .unwrap_or_default(),
67            sa.get_triggering_object(crate::ability::AbilityKey::Card)
68                .unwrap_or_default()
69        )
70    }
71}