Skip to main content

manabrew_engine/
deck_analysis.rs

1use std::collections::BTreeSet;
2
3use forge_card_script::raw_get;
4use forge_carddb::{CardFace, CardRules};
5
6fn classify_line(line: &str, roles: &mut BTreeSet<&'static str>) {
7    let api = raw_get(line, "SP")
8        .or_else(|| raw_get(line, "AB"))
9        .or_else(|| raw_get(line, "DB"));
10    let Some(api) = api else { return };
11
12    match api {
13        "Mana" => {
14            roles.insert("ramp");
15        }
16        "Draw" => {
17            roles.insert("card-draw");
18        }
19        "Counter" => {
20            roles.insert("counterspell");
21            roles.insert("interaction");
22        }
23        "Destroy" | "Damage" | "Exile" | "Sacrifice" => {
24            roles.insert("interaction");
25            roles.insert("removal");
26        }
27        "Discard" => {
28            roles.insert("discard");
29            roles.insert("interaction");
30        }
31        "GainLife" => {
32            roles.insert("lifegain");
33        }
34        "Token" | "CopyPermanent" => {
35            roles.insert("token-maker");
36        }
37        "PutCounter" | "PutCounterAll" => {
38            roles.insert("counters");
39        }
40        "Pump" | "PumpAll" | "PreventDamage" | "Regenerate" => {
41            roles.insert("protection");
42        }
43        "ChangeZone" | "ChangeZoneAll" => {
44            let origin = raw_get(line, "Origin").unwrap_or_default();
45            let destination = raw_get(line, "Destination").unwrap_or_default();
46            if origin.contains("Library")
47                && (destination.contains("Hand") || destination.contains("Battlefield"))
48            {
49                roles.insert("tutor");
50            } else if origin.contains("Graveyard") {
51                roles.insert("recursion");
52            } else if destination.contains("Exile")
53                || destination.contains("Graveyard")
54                || destination.contains("Hand")
55            {
56                roles.insert("interaction");
57                roles.insert("removal");
58            }
59        }
60        _ => {}
61    }
62}
63
64fn classify_face(face: &CardFace, roles: &mut BTreeSet<&'static str>) {
65    for line in face
66        .abilities
67        .iter()
68        .chain(&face.static_abilities)
69        .chain(&face.triggers)
70        .chain(&face.replacements)
71        .chain(face.svars.values())
72    {
73        classify_line(line, roles);
74    }
75}
76
77pub fn classify_card_roles(card: &CardRules) -> Vec<String> {
78    let mut roles = BTreeSet::new();
79    classify_face(&card.main_part, &mut roles);
80    if let Some(face) = &card.other_part {
81        classify_face(face, &mut roles);
82    }
83    roles.into_iter().map(str::to_string).collect()
84}