Skip to main content

forge_carddb/
card_rules.rs

1use forge_foundation::{CardSplitType, ColorSet, ManaCost};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5use crate::card_face::CardFace;
6
7/// Complete card definition with all faces. Mirrors Java `CardRules`.
8/// This is the immutable definition loaded from a script file.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct CardRules {
11    pub split_type: CardSplitType,
12    pub main_part: CardFace,
13    pub other_part: Option<CardFace>,
14    pub specialized_parts: HashMap<String, CardFace>,
15    pub color_identity: ColorSet,
16    pub normalized_name: String,
17    pub meld_with: Option<String>,
18    pub partner_with: Option<String>,
19}
20
21impl CardRules {
22    pub fn name(&self) -> String {
23        match self.split_type.aggregation_method() {
24            forge_foundation::FaceSelectionMethod::Combine => {
25                if let Some(ref other) = self.other_part {
26                    format!("{} // {}", self.main_part.name, other.name)
27                } else {
28                    self.main_part.name.clone()
29                }
30            }
31            _ => self.main_part.name.clone(),
32        }
33    }
34
35    pub fn mana_cost(&self) -> ManaCost {
36        match self.split_type.aggregation_method() {
37            forge_foundation::FaceSelectionMethod::Combine => {
38                if let Some(ref other) = self.other_part {
39                    ManaCost::combine(&self.main_part.mana_cost, &other.mana_cost)
40                } else {
41                    self.main_part.mana_cost.clone()
42                }
43            }
44            _ => self.main_part.mana_cost.clone(),
45        }
46    }
47
48    pub fn color(&self) -> ColorSet {
49        match self.split_type.aggregation_method() {
50            forge_foundation::FaceSelectionMethod::Combine => {
51                let c1 = self.main_part.resolved_color();
52                if let Some(ref other) = self.other_part {
53                    c1.union(other.resolved_color())
54                } else {
55                    c1
56                }
57            }
58            _ => self.main_part.resolved_color(),
59        }
60    }
61
62    pub fn cmc(&self) -> i32 {
63        self.mana_cost().cmc()
64    }
65
66    /// Calculate color identity from face colors + mana symbols in oracle text.
67    pub(crate) fn calculate_color_identity(face: &CardFace) -> u8 {
68        let mut mask = face.resolved_color().mask();
69
70        // Check static abilities for characteristic-defining "SetColor$ All"
71        for sa in &face.static_abilities {
72            if sa.contains("CharacteristicDefining$ True") && sa.contains("SetColor$ All") {
73                return 0b11111; // all colors
74            }
75        }
76
77        if mask == 0b11111 {
78            return mask;
79        }
80
81        // Scan oracle text for mana symbols {W}, {U}, {B}, {R}, {G}
82        let oracle = &face.oracle_text;
83        let mut in_reminder = false;
84        let mut in_symbol = false;
85        let bytes = oracle.as_bytes();
86        let len = bytes.len();
87        let mut i = 0;
88
89        while i < len {
90            match bytes[i] {
91                b'(' if i > 0 => in_reminder = true,
92                b')' => in_reminder = false,
93                b'{' => in_symbol = true,
94                b'}' => in_symbol = false,
95                c if in_symbol && !in_reminder => match c {
96                    b'W' => mask |= 1,
97                    b'U' => mask |= 2,
98                    b'B' => mask |= 4,
99                    b'R' => mask |= 8,
100                    b'G' => mask |= 16,
101                    _ => {}
102                },
103                _ => {}
104            }
105            i += 1;
106        }
107
108        mask
109    }
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115
116    #[test]
117    fn color_identity_basic() {
118        let mut face = CardFace::new("Lightning Bolt".to_string());
119        face.mana_cost = ManaCost::parse("R");
120        face.oracle_text = "Lightning Bolt deals 3 damage to any target.".to_string();
121        face.assign_missing_fields();
122
123        let ci = CardRules::calculate_color_identity(&face);
124        assert_eq!(ci, 8); // Red only
125    }
126
127    #[test]
128    fn color_identity_with_symbols() {
129        let mut face = CardFace::new("Test".to_string());
130        face.mana_cost = ManaCost::parse("1 R");
131        face.oracle_text = "Add {G} or {W}.".to_string();
132        face.assign_missing_fields();
133
134        let ci = CardRules::calculate_color_identity(&face);
135        assert!(ci & 8 != 0); // Red from cost
136        assert!(ci & 16 != 0); // Green from oracle
137        assert!(ci & 1 != 0); // White from oracle
138    }
139}