breed_web/
skill_detail.rs

1use serde::Serialize;
2
3use data::GameData;
4use data::skill::{self, Skill};
5
6#[derive(Serialize)]
7pub struct SkillReference
8{
9    pub name: String,
10    pub is_current: bool,
11    pub is_relevant: bool,
12    pub monsters: Vec<String>,
13}
14
15impl SkillReference
16{
17    fn fromSkill(skill: &Skill, is_current: bool, is_relevant: bool,
18                 data: &GameData) -> Self
19    {
20        Self {
21            name: skill.name.clone(),
22            is_current,
23            is_relevant,
24            monsters: data.monstersWithSkill(&skill.name)
25                .map(|m| m.name.clone()).collect(),
26        }
27    }
28}
29
30pub type SkillUpgradePath = Vec<SkillReference>;
31
32#[derive(Serialize)]
33pub struct SkillCombination
34{
35    pub target: SkillReference,
36    pub constituents: Vec<SkillUpgradePath>,
37}
38
39impl SkillCombination
40{
41    fn fromSkill(target: &Skill, current_skill: &Skill, data: &GameData) ->
42        Option<Self>
43    {
44        if target.combine_from.is_empty()
45        {
46            return None;
47        }
48
49        let constituents: Vec<SkillUpgradePath> = target.combine_from
50            .iter().map(|name| {
51                let skill = data.skill(name).unwrap();
52                data.skillUpgradePath(skill).iter().map(|s| {
53                    SkillReference::fromSkill(
54                        s,
55                        &current_skill.name == &s.name,
56                        &s.name == name,
57                        data)
58                }).collect()
59            }).collect();
60        Some(Self {
61            target: SkillReference::fromSkill(
62                target, target.name == current_skill.name, false, data),
63            constituents,
64        })
65    }
66}
67
68#[derive(Serialize)]
69pub struct SkillDetail
70{
71    pub name: String,
72    pub monsters: Vec<String>,
73    pub requirements: skill::Requirements,
74    pub upgrade_path: SkillUpgradePath,
75    pub combines_to: Vec<SkillCombination>,
76    pub combines_from: Option<SkillCombination>,
77}
78
79impl SkillDetail
80{
81    pub fn fromSkill(skill: &Skill, data: &GameData) -> Option<Self>
82    {
83        let monsters: Vec<String> = data.monstersWithSkill(&skill.name)
84            .map(|m| m.name.clone()).collect();
85        let upgrade_path_skills = data.skillUpgradePath(skill);
86        let upgrade_path: SkillUpgradePath = if upgrade_path_skills.len() > 1
87        {
88            upgrade_path_skills.iter().map(|s| SkillReference::fromSkill(
89                s, s.name == skill.name, false, data))
90                .collect()
91        }
92        else
93        {
94            Vec::new()
95        };
96        let combines_to: Vec<SkillCombination> = data.skillCombinesInto(skill)
97            .map(|target| SkillCombination::fromSkill(target, skill, data)
98                 .unwrap()).collect();
99        let combines_from: Option<SkillCombination> =
100            if skill.combine_from.is_empty()
101        {
102            None
103        }
104        else
105        {
106            Some(SkillCombination::fromSkill(skill, skill, data).unwrap())
107        };
108
109        Some(Self {
110            name: skill.name.clone(),
111            monsters,
112            requirements: skill.requirements.clone(),
113            upgrade_path,
114            combines_to,
115            combines_from,
116        })
117    }
118}