forge_carddb/
card_rules.rs1use forge_foundation::{CardSplitType, ColorSet, ManaCost};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5use crate::card_face::CardFace;
6
7#[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 #[serde(default)]
20 pub tokens: Vec<String>,
21}
22
23impl CardRules {
24 pub fn name(&self) -> String {
25 match self.split_type.aggregation_method() {
26 forge_foundation::FaceSelectionMethod::Combine => {
27 if let Some(ref other) = self.other_part {
28 format!("{} // {}", self.main_part.name, other.name)
29 } else {
30 self.main_part.name.clone()
31 }
32 }
33 _ => self.main_part.name.clone(),
34 }
35 }
36
37 pub fn mana_cost(&self) -> ManaCost {
38 match self.split_type.aggregation_method() {
39 forge_foundation::FaceSelectionMethod::Combine => {
40 if let Some(ref other) = self.other_part {
41 ManaCost::combine(&self.main_part.mana_cost, &other.mana_cost)
42 } else {
43 self.main_part.mana_cost.clone()
44 }
45 }
46 _ => self.main_part.mana_cost.clone(),
47 }
48 }
49
50 pub fn color(&self) -> ColorSet {
51 match self.split_type.aggregation_method() {
52 forge_foundation::FaceSelectionMethod::Combine => {
53 let c1 = self.main_part.resolved_color();
54 if let Some(ref other) = self.other_part {
55 c1.union(other.resolved_color())
56 } else {
57 c1
58 }
59 }
60 _ => self.main_part.resolved_color(),
61 }
62 }
63
64 pub fn cmc(&self) -> i32 {
65 self.mana_cost().cmc()
66 }
67
68 pub(crate) fn calculate_color_identity(face: &CardFace) -> u8 {
70 let mut mask = face.resolved_color().mask();
71
72 for sa in &face.static_abilities {
74 if sa.contains("CharacteristicDefining$ True") && sa.contains("SetColor$ All") {
75 return 0b11111; }
77 }
78
79 if mask == 0b11111 {
80 return mask;
81 }
82
83 let oracle = &face.oracle_text;
85 let mut in_reminder = false;
86 let mut in_symbol = false;
87 let bytes = oracle.as_bytes();
88 let len = bytes.len();
89 let mut i = 0;
90
91 while i < len {
92 match bytes[i] {
93 b'(' if i > 0 => in_reminder = true,
94 b')' => in_reminder = false,
95 b'{' => in_symbol = true,
96 b'}' => in_symbol = false,
97 c if in_symbol && !in_reminder => match c {
98 b'W' => mask |= 1,
99 b'U' => mask |= 2,
100 b'B' => mask |= 4,
101 b'R' => mask |= 8,
102 b'G' => mask |= 16,
103 _ => {}
104 },
105 _ => {}
106 }
107 i += 1;
108 }
109
110 mask
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 #[test]
119 fn color_identity_basic() {
120 let mut face = CardFace::new("Lightning Bolt".to_string());
121 face.mana_cost = ManaCost::parse("R");
122 face.oracle_text = "Lightning Bolt deals 3 damage to any target.".to_string();
123 face.assign_missing_fields();
124
125 let ci = CardRules::calculate_color_identity(&face);
126 assert_eq!(ci, 8); }
128
129 #[test]
130 fn color_identity_with_symbols() {
131 let mut face = CardFace::new("Test".to_string());
132 face.mana_cost = ManaCost::parse("1 R");
133 face.oracle_text = "Add {G} or {W}.".to_string();
134 face.assign_missing_fields();
135
136 let ci = CardRules::calculate_color_identity(&face);
137 assert!(ci & 8 != 0); assert!(ci & 16 != 0); assert!(ci & 1 != 0); }
141}