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 additional_fighting_style: Option<String>,
74    pub abilities_modifiers: Abilities,
75}
76
77/// The key is the index of the class from https://www.dnd5eapi.co/api/classes
78#[derive(Debug)]
79#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
80#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
81#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
82pub struct Class(String, pub ClassProperties);
83
84impl Hash for Class {
85    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
86        self.0.hash(state);
87    }
88}
89
90impl PartialEq for Class {
91    fn eq(&self, other: &Self) -> bool {
92        self.0 == other.0
93    }
94}
95
96impl Eq for Class {}
97
98impl Class {
99    pub fn index(&self) -> &str {
100        &self.0
101    }
102
103    pub fn hit_dice(&self) -> u8 {
104        match self.index() {
105            "barbarian" => 12,
106            "bard" => 8,
107            "cleric" => 8,
108            "druid" => 8,
109            "fighter" => 10,
110            "monk" => 8,
111            "paladin" => 10,
112            "ranger" => 10,
113            "rogue" => 8,
114            "sorcerer" => 6,
115            "warlock" => 8,
116            "wizard" => 6,
117            // For unknown classes we will use the minimum hit dice
118            _ => 6,
119        }
120    }
121}
122
123#[derive(Default, Debug)]
124#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
125#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
126#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
127pub struct Classes(pub HashMap<String, Class>);
128
129impl Classes {
130    pub fn new(class_index: String) -> Self {
131        let mut classes = Self::default();
132
133        let spell_casting = match class_index.as_str() {
134            "cleric" | "paladin" | "druid" => Some(ClassSpellCasting::AlreadyKnowPrepared {
135                spells_prepared_index: Vec::new(),
136                pending_preparation: true,
137            }),
138            _ => None,
139        };
140
141        let class_properties = ClassProperties {
142            spell_casting,
143            ..ClassProperties::default()
144        };
145
146        classes
147            .0
148            .insert(class_index.clone(), Class(class_index, class_properties));
149        classes
150    }
151}