2023-04-15 12:34:42 +00:00
|
|
|
use anyhow::Result;
|
2022-06-19 19:34:08 +00:00
|
|
|
use std::fmt;
|
|
|
|
use std::fmt::{Debug, Formatter};
|
|
|
|
|
|
|
|
use hashbrown::HashMap;
|
|
|
|
|
2021-01-30 21:29:59 +00:00
|
|
|
use crate::defines::LevelInt;
|
2022-06-11 18:51:37 +00:00
|
|
|
use crate::static_data::GrowthRate;
|
2022-10-08 11:15:04 +00:00
|
|
|
use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
|
2021-01-30 21:29:59 +00:00
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// A library to store all growth rates.
|
2022-12-24 11:00:50 +00:00
|
|
|
pub trait GrowthRateLibrary: Debug + ValueIdentifiable {
|
|
|
|
/// Calculates the level for a given growth key name and a certain experience.
|
2023-04-19 16:44:11 +00:00
|
|
|
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> Result<LevelInt>;
|
2022-12-24 11:00:50 +00:00
|
|
|
/// Calculates the experience for a given growth key name and a certain level.
|
2023-04-15 12:34:42 +00:00
|
|
|
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32>;
|
2022-12-24 11:00:50 +00:00
|
|
|
/// Adds a new growth rate with a name and value.
|
|
|
|
fn add_growth_rate(&mut self, key: &StringKey, value: Box<dyn GrowthRate>);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A library to store all growth rates.
|
|
|
|
pub struct GrowthRateLibraryImpl {
|
2022-10-08 11:15:04 +00:00
|
|
|
/// A unique identifier so we know what value this is.
|
|
|
|
identifier: ValueIdentifier,
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The underlying data structure.
|
2022-06-11 15:22:46 +00:00
|
|
|
growth_rates: HashMap<StringKey, Box<dyn GrowthRate>>,
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-24 11:00:50 +00:00
|
|
|
impl GrowthRateLibraryImpl {
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Instantiates a new growth rate library with a capacity.
|
2022-12-24 11:00:50 +00:00
|
|
|
pub fn new(capacity: usize) -> Self {
|
|
|
|
Self {
|
2022-10-08 11:15:04 +00:00
|
|
|
identifier: Default::default(),
|
2021-01-30 21:29:59 +00:00
|
|
|
growth_rates: HashMap::with_capacity(capacity),
|
|
|
|
}
|
|
|
|
}
|
2022-12-24 11:00:50 +00:00
|
|
|
}
|
2021-01-30 21:29:59 +00:00
|
|
|
|
2022-12-24 11:00:50 +00:00
|
|
|
impl GrowthRateLibrary for GrowthRateLibraryImpl {
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Calculates the level for a given growth key name and a certain experience.
|
2023-04-19 16:44:11 +00:00
|
|
|
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> Result<LevelInt> {
|
|
|
|
Ok(self
|
|
|
|
.growth_rates
|
|
|
|
.get(growth_rate)
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("No growth rate found with key {}", growth_rate.to_string()))?
|
|
|
|
.calculate_level(experience))
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Calculates the experience for a given growth key name and a certain level.
|
2023-04-15 12:34:42 +00:00
|
|
|
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32> {
|
2023-04-19 16:44:11 +00:00
|
|
|
self.growth_rates
|
|
|
|
.get(growth_rate)
|
|
|
|
.ok_or_else(|| anyhow::anyhow!("No growth rate found with key {}", growth_rate.to_string()))?
|
|
|
|
.calculate_experience(level)
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
2022-07-01 15:07:22 +00:00
|
|
|
|
|
|
|
/// Adds a new growth rate with a name and value.
|
2022-12-24 11:00:50 +00:00
|
|
|
fn add_growth_rate(&mut self, key: &StringKey, value: Box<dyn GrowthRate>) {
|
2022-06-11 15:22:46 +00:00
|
|
|
self.growth_rates.insert(key.clone(), value);
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-24 11:00:50 +00:00
|
|
|
impl ValueIdentifiable for GrowthRateLibraryImpl {
|
2022-10-08 11:15:04 +00:00
|
|
|
fn value_identifier(&self) -> ValueIdentifier {
|
|
|
|
self.identifier
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-24 11:00:50 +00:00
|
|
|
impl Debug for GrowthRateLibraryImpl {
|
2021-01-30 21:29:59 +00:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_struct("GrowthRateLibrary").finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2023-04-19 16:44:11 +00:00
|
|
|
#[allow(clippy::unwrap_used)]
|
|
|
|
#[allow(clippy::indexing_slicing)]
|
2021-01-31 16:31:22 +00:00
|
|
|
pub mod tests {
|
2022-12-24 11:00:50 +00:00
|
|
|
use super::*;
|
2022-06-19 19:34:08 +00:00
|
|
|
use crate::static_data::growth_rates::LookupGrowthRate;
|
2021-01-30 21:29:59 +00:00
|
|
|
use crate::static_data::libraries::growth_rate_library::GrowthRateLibrary;
|
2022-12-24 11:00:50 +00:00
|
|
|
use crate::static_data::GrowthRateLibraryImpl;
|
2021-01-30 21:29:59 +00:00
|
|
|
|
2022-12-24 11:00:50 +00:00
|
|
|
pub fn build() -> GrowthRateLibraryImpl {
|
|
|
|
let mut lib = GrowthRateLibraryImpl::new(1);
|
2021-01-30 21:29:59 +00:00
|
|
|
|
|
|
|
// Borrow as mut so we can insert
|
|
|
|
let w = &mut lib;
|
2022-06-06 11:54:59 +00:00
|
|
|
w.add_growth_rate(
|
2022-06-11 15:22:46 +00:00
|
|
|
&"test_growthrate".into(),
|
2022-06-06 11:54:59 +00:00
|
|
|
Box::new(LookupGrowthRate::new(vec![0, 5, 10, 100])),
|
|
|
|
);
|
2021-01-30 21:29:59 +00:00
|
|
|
// Drops borrow as mut
|
|
|
|
|
2021-01-31 16:31:22 +00:00
|
|
|
lib
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-24 11:00:50 +00:00
|
|
|
mockall::mock! {
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub GrowthRateLibrary{}
|
|
|
|
impl GrowthRateLibrary for GrowthRateLibrary {
|
2023-04-19 16:44:11 +00:00
|
|
|
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> Result<LevelInt>;
|
2023-04-15 12:34:42 +00:00
|
|
|
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32>;
|
2022-12-24 11:00:50 +00:00
|
|
|
fn add_growth_rate(&mut self, key: &StringKey, value: Box<dyn GrowthRate>);
|
|
|
|
}
|
|
|
|
impl ValueIdentifiable for GrowthRateLibrary {
|
|
|
|
fn value_identifier(&self) -> ValueIdentifier{
|
|
|
|
ValueIdentifier::new(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 21:29:59 +00:00
|
|
|
#[test]
|
2021-01-31 16:31:22 +00:00
|
|
|
fn add_growth_rate_to_library_and_calculate_level() {
|
|
|
|
let lib = build();
|
2023-04-19 16:44:11 +00:00
|
|
|
assert_eq!(lib.calculate_level(&"test_growthrate".into(), 3).unwrap(), 1);
|
|
|
|
assert_eq!(lib.calculate_level(&"test_growthrate".into(), 50).unwrap(), 3);
|
2021-01-31 16:31:22 +00:00
|
|
|
}
|
2021-01-30 21:29:59 +00:00
|
|
|
|
2021-01-31 16:31:22 +00:00
|
|
|
#[test]
|
|
|
|
fn add_growth_rate_to_library_and_calculate_experience() {
|
|
|
|
let lib = build();
|
2023-04-15 12:34:42 +00:00
|
|
|
assert_eq!(lib.calculate_experience(&"test_growthrate".into(), 1).unwrap(), 0);
|
|
|
|
assert_eq!(lib.calculate_experience(&"test_growthrate".into(), 3).unwrap(), 10);
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
|
|
|
}
|