dnd_character/
lib.rs

1#[cfg(feature = "api")]
2pub mod api;
3
4pub mod abilities;
5pub mod classes;
6
7use abilities::AbilityScore;
8use anyhow::{anyhow, bail};
9use api::classes::ChoosableCustomLevelFeatureOption;
10use lazy_static::lazy_static;
11#[cfg(feature = "serde")]
12use serde::{Deserialize, Serialize};
13use std::cmp::Ordering;
14use std::collections::HashMap;
15use std::fmt;
16use std::ops::Deref;
17
18use crate::abilities::Abilities;
19use crate::classes::Classes;
20
21lazy_static! {
22    pub static ref GRAPHQL_API_URL: String = std::env::var("DND_GRAPHQL_API_URL")
23        .unwrap_or_else(|_| "https://www.dnd5eapi.co/graphql/2014".to_string());
24}
25
26#[derive(Debug)]
27pub struct UnexpectedAbility;
28
29impl fmt::Display for UnexpectedAbility {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        write!(f, "The ability isn't present in the character's abilities")
32    }
33}
34
35impl std::error::Error for UnexpectedAbility {}
36
37#[derive(Debug)]
38#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
39#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
40#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
41pub struct Character {
42    /// Indexes from https://www.dnd5eapi.co/api/classes/
43    pub classes: Classes,
44    pub name: String,
45    pub age: u16,
46    /// Index from https://www.dnd5eapi.co/api/races/
47    pub race_index: String,
48    /// Index from https://www.dnd5eapi.co/api/subraces/
49    pub subrace_index: String,
50    /// Index from https://www.dnd5eapi.co/api/alignments/
51    pub alignment_index: String,
52    /// Physical description
53    pub description: String,
54    /// Index from https://www.dnd5eapi.co/api/backgrounds/
55    pub background_index: String,
56    /// Background description
57    pub background_description: String,
58
59    experience_points: u32,
60
61    pub money: u32,
62
63    pub abilities_score: Abilities,
64
65    //Health related stuff
66    pub hp: u16,
67    #[serde(default = "default_hit_dice")]
68    pub hit_dice_result: u16,
69
70    pub inventory: HashMap<String, u16>,
71
72    pub other: Vec<String>,
73}
74
75/// For parsing legacy support
76fn default_hit_dice() -> u16 {
77    12
78}
79
80#[cfg(feature = "utoipa")]
81pub mod utoipa_addon {
82    use utoipa::openapi::OpenApi;
83    use utoipa::{Modify, PartialSchema, ToSchema};
84
85    pub struct ApiDocDndCharacterAddon;
86
87    impl Modify for ApiDocDndCharacterAddon {
88        fn modify(&self, openapi: &mut OpenApi) {
89            if let Some(components) = openapi.components.as_mut() {
90                components.schemas.insert(
91                    super::classes::ClassProperties::name().to_string(),
92                    super::classes::ClassProperties::schema(),
93                );
94                components.schemas.insert(
95                    super::classes::ClassSpellCasting::name().to_string(),
96                    super::classes::ClassSpellCasting::schema(),
97                );
98                components.schemas.insert(
99                    super::classes::Class::name().to_string(),
100                    super::classes::Class::schema(),
101                );
102                components
103                    .schemas
104                    .insert(super::Classes::name().to_string(), super::Classes::schema());
105                components.schemas.insert(
106                    super::classes::UsableSlots::name().to_string(),
107                    super::classes::UsableSlots::schema(),
108                );
109                components.schemas.insert(
110                    super::Abilities::name().to_string(),
111                    super::Abilities::schema(),
112                );
113                components.schemas.insert(
114                    super::abilities::AbilityScore::name().to_string(),
115                    super::abilities::AbilityScore::schema(),
116                );
117                components.schemas.insert(
118                    super::Character::name().to_string(),
119                    super::Character::schema(),
120                );
121            }
122        }
123    }
124}
125
126const LEVELS: [u32; 19] = [
127    300, 900, 2_700, 6_500, 14_000, 23_000, 34_000, 48_000, 64_000, 85_000, 100_000, 120_000,
128    140_000, 165_000, 195_000, 225_000, 265_000, 305_000, 355_000,
129];
130
131impl Character {
132    pub fn new(
133        main_class: String,
134        name: String,
135        age: u16,
136        race_index: String,
137        subrace_index: String,
138        alignment_index: String,
139        description: String,
140        background_index: String,
141        background_description: String,
142    ) -> Self {
143        Self {
144            classes: Classes::new(main_class),
145            name,
146            age,
147            race_index,
148            subrace_index,
149            alignment_index,
150            description,
151            background_index,
152            background_description,
153            experience_points: 0,
154            money: 0,
155            inventory: HashMap::new(),
156
157            abilities_score: Abilities::default(),
158            hp: 0,
159            hit_dice_result: 0,
160            other: vec![],
161        }
162    }
163
164    pub fn class_armor(&self) -> i8 {
165        // Get the first class and its name
166        let first_class = self.classes.0.iter().next().unwrap();
167        let class_name = first_class.0.as_str();
168
169        // Calculate the base armor class based on the class type
170        let mut base = match class_name {
171            "monk" => {
172                10 + self.abilities_score.dexterity.modifier(0)
173                    + self.abilities_score.wisdom.modifier(0)
174            }
175            _ => 10 + self.abilities_score.dexterity.modifier(0),
176        };
177
178        // Check if the character has the "Fighting Style: Defense" feature
179        let has_defense_style = first_class.1 .1.fighting_style
180            == Some(
181                ChoosableCustomLevelFeatureOption::FightingStyleDefense
182                    .as_index_str()
183                    .to_string(),
184            );
185
186        // Add bonus if the character has the defense fighting style
187        if has_defense_style {
188            base += 1;
189        }
190
191        base
192    }
193
194    /// Return current level of the character
195    pub fn level(&self) -> u8 {
196        LEVELS
197            .iter()
198            .filter(|&&x| x <= self.experience_points)
199            .count() as u8
200            + 1
201    }
202
203    /// Returns the experience points of the character
204    pub fn experience_points(&self) -> u32 {
205        self.experience_points
206    }
207
208    /// Returns the number of levels the character has earned
209    /// this means that you should add the returned value to a class level (this must be done manually to permit multiclassing)
210    /// # Arguments
211    /// * `experience` - The experience points to add to the character
212    pub fn add_experience(&mut self, experience: u32) -> u8 {
213        //Save the level before adding experience
214        let previous_level = self.level();
215
216        // Limit the experience gotten to the experience needed to reach the next level
217        let experience_to_add = LEVELS
218            .get(self.level() as usize - 1)
219            .map_or(experience, |&next_level_points| {
220                (next_level_points - self.experience_points).min(experience)
221            });
222
223        //Add the experience
224        self.experience_points += experience_to_add;
225
226        //Save the level after adding experience
227        let current_level = self.level();
228
229        //Return the number of levels earned
230        current_level - previous_level
231    }
232
233    pub fn remove_item(
234        &mut self,
235        item: &str,
236        amount: Option<u16>,
237    ) -> anyhow::Result<(), anyhow::Error> {
238        if let Some(quantity) = self.inventory.get_mut(item) {
239            let quantity_to_remove = amount.unwrap_or(*quantity);
240
241            if *quantity <= quantity_to_remove {
242                self.inventory.remove(item);
243            } else {
244                *quantity -= quantity_to_remove;
245            }
246        } else {
247            bail!("Item not found")
248        }
249
250        Ok(())
251    }
252
253    pub fn add_item(&mut self, item: &str, amount: u16) {
254        if let Some(quantity) = self.inventory.get_mut(item) {
255            *quantity += amount;
256        } else {
257            self.inventory.insert(item.to_string(), amount);
258        }
259    }
260
261    pub fn alter_item_quantity(&mut self, item: &str, amount: i32) -> anyhow::Result<()> {
262        match amount.cmp(&0) {
263            Ordering::Greater => {
264                self.add_item(item, amount as u16);
265                Ok(())
266            }
267            Ordering::Less => self.remove_item(item, Some(amount.unsigned_abs() as u16)),
268            Ordering::Equal => {
269                bail!("Cannot alter quantity to 0")
270            }
271        }
272    }
273
274    /// Calculate the maximum HP of the character based on constitution modifier and hit dice result
275    pub fn max_hp(&self) -> u16 {
276        let constitution_ability: AbilityScore = self
277            .classes
278            .0
279            .values()
280            .map(|class| class.1.abilities_modifiers.constitution.clone())
281            .sum::<AbilityScore>()
282            + self.abilities_score.constitution.clone();
283
284        let constitution_modifier = constitution_ability.modifier(0);
285
286        (constitution_modifier as i32)
287            .saturating_mul(self.level().into())
288            .saturating_add(self.hit_dice_result.into())
289            .max(0) as u16
290    }
291}