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 cantrip_slots: u8,
51    pub level_1: u8,
52    pub level_2: u8,
53    pub level_3: u8,
54    pub level_4: u8,
55    pub level_5: u8,
56    pub level_6: u8,
57    pub level_7: u8,
58    pub level_8: u8,
59    pub level_9: u8,
60}
61
62#[derive(Debug, Default)]
63#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
64#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
65#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
66pub struct ClassProperties {
67    /// The level of the class
68    pub level: u8,
69    /// Index from https://www.dnd5eapi.co/api/subclasses/
70    pub subclass: Option<String>,
71    /// Indexes from https://www.dnd5eapi.co/api/spells/
72    pub spell_casting: Option<ClassSpellCasting>,
73    pub fighting_style: Option<String>,
74    pub hunters_prey: Option<String>,
75    pub defensive_tactics: Option<String>,
76    pub additional_fighting_style: Option<String>,
77    pub abilities_modifiers: Abilities,
78}
79
80/// The key is the index of the class from https://www.dnd5eapi.co/api/classes
81#[derive(Debug)]
82#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
83#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
84#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
85pub struct Class(String, pub ClassProperties);
86
87impl Hash for Class {
88    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
89        self.0.hash(state);
90    }
91}
92
93impl PartialEq for Class {
94    fn eq(&self, other: &Self) -> bool {
95        self.0 == other.0
96    }
97}
98
99impl Eq for Class {}
100
101impl Class {
102    pub fn index(&self) -> &str {
103        &self.0
104    }
105
106    pub fn hit_dice(&self) -> u8 {
107        match self.index() {
108            "barbarian" => 12,
109            "bard" => 8,
110            "cleric" => 8,
111            "druid" => 8,
112            "fighter" => 10,
113            "monk" => 8,
114            "paladin" => 10,
115            "ranger" => 10,
116            "rogue" => 8,
117            "sorcerer" => 6,
118            "warlock" => 8,
119            "wizard" => 6,
120            // For unknown classes we will use the minimum hit dice
121            _ => 6,
122        }
123    }
124}
125
126#[derive(Default, Debug)]
127#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
128#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
129#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
130pub struct Classes(pub HashMap<String, Class>);
131
132impl Classes {
133    pub fn new(class_index: String) -> Self {
134        let mut classes = Self::default();
135
136        let spell_casting = match class_index.as_str() {
137            "cleric" | "paladin" | "druid" => Some(ClassSpellCasting::AlreadyKnowPrepared {
138                spells_prepared_index: Vec::new(),
139                pending_preparation: true,
140            }),
141            "ranger" | "bard" | "warlock" => Some(ClassSpellCasting::KnowledgeAlreadyPrepared {
142                spells_index: Vec::new(),
143                usable_slots: UsableSlots::default(),
144            }),
145            _ => None,
146        };
147
148        let class_properties = ClassProperties {
149            spell_casting,
150            ..ClassProperties::default()
151        };
152
153        classes
154            .0
155            .insert(class_index.clone(), Class(class_index, class_properties));
156        classes
157    }
158}