Initial commit, implements most of the static_data side.
This commit is contained in:
69
src/static_data/libraries/growth_rate_library.rs
Normal file
69
src/static_data/libraries/growth_rate_library.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use crate::defines::LevelInt;
|
||||
use crate::static_data::growth_rates::growth_rate::GrowthRate;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
|
||||
pub struct GrowthRateLibrary {
|
||||
growth_rates: HashMap<String, Box<dyn GrowthRate>>,
|
||||
}
|
||||
|
||||
impl GrowthRateLibrary {
|
||||
pub fn new(capacity: usize) -> GrowthRateLibrary {
|
||||
GrowthRateLibrary {
|
||||
growth_rates: HashMap::with_capacity(capacity),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn calculate_level(&self, growth_rate: &str, experience: u32) -> LevelInt {
|
||||
self.growth_rates[growth_rate].calculate_level(experience)
|
||||
}
|
||||
pub fn calculate_experience(&self, growth_rate: &str, level: LevelInt) -> u32 {
|
||||
self.growth_rates[growth_rate].calculate_experience(level)
|
||||
}
|
||||
pub fn add_growth_rate(&mut self, key: &str, value: Box<dyn GrowthRate>) {
|
||||
self.growth_rates.insert(key.to_string(), value);
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for GrowthRateLibrary {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("GrowthRateLibrary").finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::static_data::growth_rates::lookup_growth_rate::LookupGrowthRate;
|
||||
use crate::static_data::libraries::growth_rate_library::GrowthRateLibrary;
|
||||
|
||||
#[test]
|
||||
fn add_growth_rate_to_library_and_calculate_level() {
|
||||
let mut lib = GrowthRateLibrary::new(1);
|
||||
|
||||
// Borrow as mut so we can insert
|
||||
let w = &mut lib;
|
||||
w.add_growth_rate("foo", Box::new(LookupGrowthRate::new(vec![0, 5, 10, 100])));
|
||||
// Drops borrow as mut
|
||||
|
||||
// Borrow as read so we can read
|
||||
let r = &lib;
|
||||
assert_eq!(r.calculate_level("foo", 3), 1);
|
||||
assert_eq!(r.calculate_level("foo", 50), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_growth_rate_to_library_and_calculate_experience() {
|
||||
let mut lib = GrowthRateLibrary::new(1);
|
||||
|
||||
// Borrow as mut so we can insert
|
||||
let w = &mut lib;
|
||||
w.add_growth_rate("foo", Box::new(LookupGrowthRate::new(vec![0, 5, 10, 100])));
|
||||
// Drops borrow as mut
|
||||
|
||||
// Borrow as read so we can read
|
||||
let r = &lib;
|
||||
assert_eq!(r.calculate_experience("foo", 1), 0);
|
||||
assert_eq!(r.calculate_experience("foo", 3), 10);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user