dnd_character/
classes.rs

1use crate::abilities::Abilities;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4use std::hash::Hash;
5
6#[derive(Debug)]
7#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
9#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
10pub enum ClassSpellCasting {
11    // Wizard
12    // Ask the user to prepare spells at the start of the day
13    //
14    // TODO: Add slots and consume them instead of removing from prepared
15    // TODO: daily chosable spells = inteligence + level
16    KnowledgePrepared {
17        /// Indexes from https://www.dnd5eapi.co/api/spells/
18        spells_index: Vec<Vec<String>>,
19        /// Indexes from https://www.dnd5eapi.co/api/spells/
20        spells_prepared_index: Vec<Vec<String>>,
21        /// If the user has already prepared spells for the day
22        pending_preparation: bool,
23    },
24    // Cleric, Paladin, Druid
25    // Ask the user to prepare spells at the start of the day
26    //
27    // TODO: Add slots and consume them instead of removing from prepared
28    // TODO: cleric/druid daily chosable spells = WISDOM + (level/2)
29    // TODO: paladin daily chosable spells = CHARISMA + (level/2)
30    AlreadyKnowPrepared {
31        /// Indexes from https://www.dnd5eapi.co/api/spells/
32        spells_prepared_index: Vec<Vec<String>>,
33        /// If the user has already prepared spells for the day
34        pending_preparation: bool,
35    },
36    // Bard, Ranger, Warlock, (Sorcerer?)
37    // No need to ask anything, at the start of the day
38    KnowledgeAlreadyPrepared {
39        /// Indexes from https://www.dnd5eapi.co/api/spells/
40        spells_index: Vec<Vec<String>>,
41        usable_slots: UsableSlots,
42    },
43}
44
45#[derive(Debug, Default)]
46#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
47#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
48#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
49pub struct UsableSlots {
50    pub level_1: u8,
51    pub level_2: u8,
52    pub level_3: u8,
53    pub level_4: u8,
54    pub level_5: u8,
55    pub level_6: u8,
56    pub level_7: u8,
57    pub level_8: u8,
58    pub level_9: u8,
59}
60
61#[derive(Debug, Default)]
62#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
63#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
64#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
65pub struct ClassProperties {
66    /// The level of the class
67    pub level: u8,
68    /// Index from https://www.dnd5eapi.co/api/subclasses/
69    pub subclass: Option<String>,
70    /// Indexes from https://www.dnd5eapi.co/api/spells/
71    pub spell_casting: Option<ClassSpellCasting>,
72    pub fighting_style: Option<String>,
73    pub hunters_prey: Option<String>,
74    pub defensive_tactics: Option<String>,
75    pub additional_fighting_style: Option<String>,
76    pub abilities_modifiers: Abilities,
77}
78
79/// The key is the index of the class from https://www.dnd5eapi.co/api/classes
80#[derive(Debug)]
81#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
82#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
83#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
84pub struct Class(String, pub ClassProperties);
85
86impl Hash for Class {
87    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
88        self.0.hash(state);
89    }
90}
91
92impl PartialEq for Class {
93    fn eq(&self, other: &Self) -> bool {
94        self.0 == other.0
95    }
96}
97
98impl Eq for Class {}
99
100impl Class {
101    pub fn index(&self) -> &str {
102        &self.0
103    }
104
105    pub fn hit_dice(&self) -> u8 {
106        match self.index() {
107            "barbarian" => 12,
108            "bard" => 8,
109            "cleric" => 8,
110            "druid" => 8,
111            "fighter" => 10,
112            "monk" => 8,
113            "paladin" => 10,
114            "ranger" => 10,
115            "rogue" => 8,
116            "sorcerer" => 6,
117            "warlock" => 8,
118            "wizard" => 6,
119            // For unknown classes we will use the minimum hit dice
120            _ => 6,
121        }
122    }
123}
124
125#[derive(Default, Debug)]
126#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
127#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
128#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
129pub struct Classes(pub HashMap<String, Class>);
130
131impl Classes {
132    pub fn new(class_index: String) -> Self {
133        let mut classes = Self::default();
134
135        let spell_casting = match class_index.as_str() {
136            "cleric" | "paladin" | "druid" => Some(ClassSpellCasting::AlreadyKnowPrepared {
137                spells_prepared_index: Vec::new(),
138                pending_preparation: true,
139            }),
140            "ranger" | "bard" | "warlock" => Some(ClassSpellCasting::KnowledgeAlreadyPrepared {
141                spells_index: Vec::new(),
142                usable_slots: UsableSlots::default(),
143            }),
144            _ => None,
145        };
146
147        let class_properties = ClassProperties {
148            spell_casting,
149            ..ClassProperties::default()
150        };
151
152        classes
153            .0
154            .insert(class_index.clone(), Class(class_index, class_properties));
155        classes
156    }
157}