1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::{
    parse::{Censor, Token},
    prebuilt::{ver1_46, ConditionConfig, CONDITION_SCHEME},
    Scenario,
};

pub struct ConditionTweak;

impl ConditionTweak {
    pub fn translate(
        scenario: &Scenario,
        condition: &Token,
    ) -> Result<(String, String), &'static str> {
        match scenario.version() {
            "1.46" | "1.47" => {
                Self::is_condition(condition, scenario.version())?;

                let type_id = *condition.get_by_path("/condition_type").try_i32();
                if CONDITION_SCHEME.contains_key(&type_id) {
                    let scheme = &CONDITION_SCHEME[&type_id];
                    let name = scheme.name.to_string();
                    let attrs: Vec<String> = scheme
                        .attrs
                        .iter()
                        .map(|&path| match path {
                            "xs_function" => {
                                format!(
                                    "{}: {:?}",
                                    path,
                                    condition.get_by_path(path).try_str32().content()
                                )
                            }
                            _ => {
                                format!("{}: {:?}", path, condition.get_by_path(path).try_i32())
                            }
                        })
                        .collect();

                    Ok((name, attrs.join(" ")))
                } else {
                    Ok(("Unknown Condition!".to_string(), "".to_string()))
                }
            }
            _ => Err("Incompatible version!"),
        }
    }

    pub fn scheme(version: &str, condition: &Token) -> Result<ConditionConfig, &'static str> {
        match version {
            "1.46" | "1.47" => {
                Self::is_condition(condition, version)?;

                let type_id = *condition.get_by_path("/condition_type").try_i32();
                if CONDITION_SCHEME.contains_key(&type_id) {
                    let scheme = CONDITION_SCHEME[&type_id].clone();

                    Ok(scheme)
                } else {
                    Ok(ConditionConfig {
                        name: "Unknown",
                        attrs: vec![],
                    })
                }
            }
            _ => Err("Incompatible version!"),
        }
    }

    pub fn is_condition(effect: &Token, version: &str) -> Result<(), &'static str> {
        match version {
            "1.46" | "1.47" => {
                let template = ver1_46::Condition::template();
                let res = Censor::is_template(effect, &template, 2);

                if res {
                    Ok(())
                } else {
                    Err("Not a effect!")
                }
            }
            _ => Err("Incompatible version!"),
        }
    }
}