summaryrefslogtreecommitdiff
path: root/src/character/mod.rs
blob: fc67e426958b1d731f43c9e8104fe56f8b21db12 (plain) (blame)
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
pub mod stats;
use std::io;

use io::BufRead;
pub use stats::*;

use crate::inventory::Inventory;

pub const MAX_LEVEL: u8 = 10;

fn swap_stats_according_to_string(stats: &mut Stats, string: String)
{
    let string = string.trim();
    let parts: Vec<&str> = string.split_whitespace().collect();

    if parts.len() == 2 && stats.swap(parts[0], parts[1]) {
        println!("{} and {} values have been switched.", parts[0], parts[1]);
    }
    else {
        println!("Not switching any stats.");
    }
}

pub fn cli_create_character() -> Character
{
    println!("Starting character creation.");
    println!("Rolling initial stats...");

    let mut stats = Stats::roll_initial();
    println!("Initial stats: {}", stats);

    println!(
        "Would you like to swap two stats' values? If so, write the names of them seperated by a \
         whitespace and press enter, if not just press enter."
    );

    let mut buf = String::new();

    {
        let stdin = io::stdin();
        let mut lock = stdin.lock();
        lock.read_line(&mut buf)
            .expect("Unable to read from console");
    }

    swap_stats_according_to_string(&mut stats, buf);

    unimplemented!()
}

#[derive(Clone, Debug)]
pub struct Character
{
    health:         u8,
    max_health:     u8,
    stats:          Stats,
    /// Free text describing the special trait of the character
    special_traits: Vec<String>,
    /// Total value of experience. The level can be calculated through it
    experience:     u32,
    wealth:         u32,
    inventory:      Inventory,
    goals:          Vec<String>,
}

impl Character
{
    pub fn new(
        health: u8,
        max_health: u8,
        stats: Stats,
        special_traits: Vec<String>,
        experience: u32,
        wealth: u32,
        inventory: Inventory,
        goals: Vec<String>,
    ) -> Self
    {
        Self {
            health,
            max_health,
            stats,
            special_traits,
            experience,
            wealth,
            inventory,
            goals,
        }
    }

    pub fn stats(&self) -> &Stats { &self.stats }
    pub fn stats_mut(&mut self) -> &mut Stats { &mut self.stats }

    pub fn special_traits(&self) -> &Vec<String> { &self.special_traits }

    pub fn experience(&self) -> u32 { self.experience }

    pub fn wealth(&self) -> u32 { self.wealth }

    pub fn level(&self) -> u8
    {
        /* Start iteration at first level */
        let mut level_counter = 1;
        let mut xp_remaining = self.experience;
        let mut xp_required_for_levelup = 200;

        while level_counter < MAX_LEVEL && xp_remaining >= xp_required_for_levelup {
            level_counter += 1;
            xp_remaining -= xp_required_for_levelup;
            xp_required_for_levelup += 100;
        }

        level_counter
    }
}

#[cfg(test)]
mod tests
{
    use super::*;

    const DEFAULT_STATS: Stats = Stats::from_values([10; NUM_STATS]);

    #[test]
    fn create_character()
    {
        let character = Character::new(
            10,
            10,
            DEFAULT_STATS.clone(),
            Vec::new(),
            1,
            2,
            Inventory::new(),
            Vec::new(),
        );

        assert_eq!(character.stats(), &DEFAULT_STATS);
        assert_eq!(character.special_traits(), &Vec::<String>::new());
        assert_eq!(character.experience(), 1);
        assert_eq!(character.wealth, 2);
    }

    #[test]
    fn min_level_calculation()
    {
        let character = Character::new(
            10,
            10,
            DEFAULT_STATS.clone(),
            Vec::new(),
            199,
            0,
            Inventory::new(),
            Vec::new(),
        );

        assert_eq!(character.level(), 1);
    }

    #[test]
    fn mid_level_calculation()
    {
        let character = Character::new(
            10,
            10,
            DEFAULT_STATS.clone(),
            Vec::new(),
            900,
            0,
            Inventory::new(),
            Vec::new(),
        );

        assert_eq!(character.level(), 4);
    }

    #[test]
    fn max_level_calculation()
    {
        let character = Character::new(
            10,
            10,
            DEFAULT_STATS.clone(),
            Vec::new(),
            5400,
            0,
            Inventory::new(),
            Vec::new(),
        );

        assert_eq!(character.level(), 10);

        let character = Character::new(
            10,
            10,
            DEFAULT_STATS.clone(),
            Vec::new(),
            6500,
            0,
            Inventory::new(),
            Vec::new(),
        );

        assert_eq!(character.level(), 10);
    }
}