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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::dice::Dice;
use serde::{Deserialize, Serialize};
use std::{cell::RefCell, collections::HashMap};

#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, Ord, PartialOrd, Serialize, Deserialize)]
#[allow(unused)]
pub enum DamageType {
    Slashing,
    Piercing,
    Bludgeoning,
    Magical,
    Acid,
    Cold,
    Divine,
    Electrical,
    Fire,
    Negative,
    Positive,
    Sonic,
    Entropy,
    Force,
    Psychic,
    Poison,
    Unknown,
}

impl DamageType {
    pub fn name(&self) -> String {
        format!("{:?}", self)
    }

    pub fn is_physical(&self) -> bool {
        match self {
            Self::Slashing | Self::Piercing | Self::Bludgeoning => true,
            _ => false,
        }
    }
}

impl From<&str> for DamageType {
    fn from(value: &str) -> Self {
        match value.to_lowercase().as_str() {
            "slashing" => Self::Slashing,
            "piercing" => Self::Piercing,
            "bludgeoning" => Self::Bludgeoning,
            "magical" => Self::Magical,
            "acid" => Self::Acid,
            "cold" => Self::Cold,
            "divine" => Self::Divine,
            "electrical" => Self::Electrical,
            "fire" => Self::Fire,
            "negative" => Self::Negative,
            "positive" => Self::Positive,
            "sonic" => Self::Sonic,
            "entropy" => Self::Entropy,
            "force" => Self::Force,
            "psychic" => Self::Psychic,
            "poison" => Self::Poison,
            _ => Self::Unknown,
        }
    }
}

impl std::fmt::Display for DamageType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name())
    }
}

#[derive(PartialEq, Serialize, Deserialize)]
pub struct Damage {
    amount: Dice,
    pub type_: DamageType,
    pub is_resistable: bool,
    pub can_crit: bool,
}

impl std::fmt::Display for Damage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.amount.to_string())
    }
}

impl Damage {
    pub fn new(type_: DamageType, amount: Dice, is_resistable: bool, can_crit: bool) -> Self {
        Damage {
            type_,
            amount,
            is_resistable,
            can_crit,
        }
    }

    #[allow(unused)]
    pub fn roll(&self) -> i32 {
        self.amount.roll()
    }

    pub fn roll_m(&self, count: i32) -> i32 {
        self.amount.roll_m(count)
    }
}

#[derive(Clone, Default, Debug, Serialize, Deserialize)]
pub struct DamageResult(RefCell<HashMap<DamageType, i32>>);

impl DamageResult {
    pub fn new() -> Self {
        Self::default()
    }

    #[allow(unused)]
    pub fn set(&self, type_: DamageType, amount: i32) {
        *self.0.borrow_mut().entry(type_).or_insert(0) += amount;
    }

    pub fn get(&self, type_: DamageType) -> i32 {
        self.0.borrow().get(&type_).unwrap_or(&0).to_owned()
    }

    pub fn get_types(&self) -> Vec<DamageType> {
        self.0.borrow().keys().cloned().collect::<Vec<DamageType>>()
    }

    pub fn get_types_sorted(&self) -> Vec<DamageType> {
        let mut types = self.get_types();
        types.sort();

        types
    }

    pub fn add(&self, type_: DamageType, amount: i32) -> i32 {
        let mut hashmap = self.0.borrow_mut();
        let current_dmg = hashmap.entry(type_).or_insert(0);
        *current_dmg += amount;

        *current_dmg
    }

    pub fn sub(&self, type_: DamageType, amount: i32) -> i32 {
        let mut hashmap = self.0.borrow_mut();

        if hashmap.contains_key(&type_) {
            let current_dmg = hashmap.get_mut(&type_);

            if let Some(current_dmg) = current_dmg {
                *current_dmg -= amount;

                if *current_dmg < 0 {
                    *current_dmg = 0;
                }

                return *current_dmg;
            }
        }

        0
    }

    pub fn total_dmg(&self) -> i32 {
        self.0.borrow().iter().map(|(_, v)| v).sum()
    }

    pub fn add_from(&mut self, other: &DamageResult) {
        for type_ in other.get_types() {
            self.add(type_, other.get(type_));
        }
    }
}

#[cfg(test)]
mod test {
    use super::DamageResult;
    use crate::item::DamageType;

    #[test]
    fn damage_result() {
        let dmg_result = DamageResult::new();
        assert_eq!(dmg_result.get(DamageType::Acid), 0);
        assert_eq!(dmg_result.get(DamageType::Bludgeoning), 0);

        dmg_result.set(DamageType::Acid, 4);
        assert_eq!(dmg_result.get(DamageType::Acid), 4);

        dmg_result.add(DamageType::Bludgeoning, 8);
        assert_eq!(dmg_result.get(DamageType::Bludgeoning), 8);

        dmg_result.add(DamageType::Cold, 3);
        assert_eq!(dmg_result.get(DamageType::Cold), 3);

        dmg_result.sub(DamageType::Cold, 1);
        assert_eq!(dmg_result.get(DamageType::Cold), 2);

        assert_eq!(dmg_result.total_dmg(), 14);

        assert_eq!(dmg_result.get_types().len(), 3);
        assert_eq!(dmg_result.get_types().contains(&DamageType::Acid), true);
        assert_eq!(
            dmg_result.get_types().contains(&DamageType::Bludgeoning),
            true
        );
        assert_eq!(dmg_result.get_types().contains(&DamageType::Cold), true);
        assert_eq!(
            dmg_result.get_types().contains(&DamageType::Slashing),
            false
        );
    }
}