Support for new error handling.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
use crate::defines::LevelInt;
|
||||
use anyhow::Result;
|
||||
use anyhow_ext::ensure;
|
||||
|
||||
/// A growth rate defines how much experience is required per level.
|
||||
pub trait GrowthRate {
|
||||
/// Calculate the level something with this growth rate would have at a certain experience.
|
||||
fn calculate_level(&self, experience: u32) -> LevelInt;
|
||||
/// Calculate the experience something with this growth rate would have at a certain level.
|
||||
fn calculate_experience(&self, level: LevelInt) -> u32;
|
||||
fn calculate_experience(&self, level: LevelInt) -> Result<u32>;
|
||||
}
|
||||
|
||||
/// An implementation of the growth rate that uses a lookup table for experience.
|
||||
@@ -32,11 +34,12 @@ impl GrowthRate for LookupGrowthRate {
|
||||
self.experience.len() as LevelInt
|
||||
}
|
||||
|
||||
fn calculate_experience(&self, level: LevelInt) -> u32 {
|
||||
fn calculate_experience(&self, level: LevelInt) -> Result<u32> {
|
||||
ensure!(level > 0, "Level must be greater than 0, but was {}", level);
|
||||
if level >= self.experience.len() as LevelInt {
|
||||
*self.experience.last().unwrap()
|
||||
Ok(*self.experience.last().unwrap())
|
||||
} else {
|
||||
self.experience[(level - 1) as usize]
|
||||
Ok(self.experience[(level - 1) as usize])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,7 +53,7 @@ pub(crate) mod tests {
|
||||
pub GrowthRate {}
|
||||
impl GrowthRate for GrowthRate {
|
||||
fn calculate_level(&self, experience: u32) -> LevelInt;
|
||||
fn calculate_experience(&self, level: LevelInt) -> u32;
|
||||
fn calculate_experience(&self, level: LevelInt) -> Result<u32>;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use anyhow::Result;
|
||||
use std::fmt;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
|
||||
@@ -12,7 +13,7 @@ pub trait GrowthRateLibrary: Debug + ValueIdentifiable {
|
||||
/// Calculates the level for a given growth key name and a certain experience.
|
||||
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> LevelInt;
|
||||
/// Calculates the experience for a given growth key name and a certain level.
|
||||
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> u32;
|
||||
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32>;
|
||||
/// Adds a new growth rate with a name and value.
|
||||
fn add_growth_rate(&mut self, key: &StringKey, value: Box<dyn GrowthRate>);
|
||||
}
|
||||
@@ -41,7 +42,7 @@ impl GrowthRateLibrary for GrowthRateLibraryImpl {
|
||||
self.growth_rates[growth_rate].calculate_level(experience)
|
||||
}
|
||||
/// Calculates the experience for a given growth key name and a certain level.
|
||||
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> u32 {
|
||||
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32> {
|
||||
self.growth_rates[growth_rate].calculate_experience(level)
|
||||
}
|
||||
|
||||
@@ -89,7 +90,7 @@ pub mod tests {
|
||||
pub GrowthRateLibrary{}
|
||||
impl GrowthRateLibrary for GrowthRateLibrary {
|
||||
fn calculate_level(&self, growth_rate: &StringKey, experience: u32) -> LevelInt;
|
||||
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> u32;
|
||||
fn calculate_experience(&self, growth_rate: &StringKey, level: LevelInt) -> Result<u32>;
|
||||
fn add_growth_rate(&mut self, key: &StringKey, value: Box<dyn GrowthRate>);
|
||||
}
|
||||
impl ValueIdentifiable for GrowthRateLibrary {
|
||||
@@ -109,7 +110,7 @@ pub mod tests {
|
||||
#[test]
|
||||
fn add_growth_rate_to_library_and_calculate_experience() {
|
||||
let lib = build();
|
||||
assert_eq!(lib.calculate_experience(&"test_growthrate".into(), 1), 0);
|
||||
assert_eq!(lib.calculate_experience(&"test_growthrate".into(), 3), 10);
|
||||
assert_eq!(lib.calculate_experience(&"test_growthrate".into(), 1).unwrap(), 0);
|
||||
assert_eq!(lib.calculate_experience(&"test_growthrate".into(), 3).unwrap(), 10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use crate::defines::LevelInt;
|
||||
use crate::{ValueIdentifiable, ValueIdentifier};
|
||||
use anyhow::Result;
|
||||
use anyhow_ext::ensure;
|
||||
use std::fmt::Debug;
|
||||
|
||||
/// This library holds several misc settings for the library.
|
||||
@@ -30,14 +32,14 @@ impl LibrarySettingsImpl {
|
||||
/// - `shiny_rate` is the chance of a Pokemon being shiny, as the denominator of a fraction, where
|
||||
/// the nominator is 1. For example, if this is 1000, then the chance of a Pokemon being shiny is
|
||||
/// 1/1000.
|
||||
pub fn new(maximum_level: LevelInt, shiny_rate: u32) -> Self {
|
||||
assert!(shiny_rate >= 1);
|
||||
assert!(maximum_level >= 1);
|
||||
Self {
|
||||
pub fn new(maximum_level: LevelInt, shiny_rate: u32) -> Result<Self> {
|
||||
ensure!(shiny_rate >= 1);
|
||||
ensure!(maximum_level >= 1);
|
||||
Ok(Self {
|
||||
identifier: Default::default(),
|
||||
maximum_level,
|
||||
shiny_rate,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -140,4 +140,18 @@ pub mod tests {
|
||||
let name2 = lib.get_nature_name(&n2);
|
||||
assert_eq!(name2, "bar".into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_nature_library_insert_single_and_retrieve_random() {
|
||||
let mut lib = NatureLibraryImpl::new(2);
|
||||
lib.load_nature(
|
||||
"foo".into(),
|
||||
NatureImpl::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||
);
|
||||
let n1 = lib.get_random_nature(&mut Random::new(0));
|
||||
assert_eq!(n1.increased_stat(), Statistic::HP);
|
||||
assert_eq!(n1.decreased_stat(), Statistic::Attack);
|
||||
assert_eq!(n1.get_stat_modifier(n1.increased_stat()), 1.1);
|
||||
assert_eq!(n1.get_stat_modifier(n1.decreased_stat()), 0.9);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,7 +199,7 @@ pub mod test {
|
||||
pub fn build() -> StaticDataImpl {
|
||||
StaticDataImpl {
|
||||
identifier: Default::default(),
|
||||
settings: Box::new(LibrarySettingsImpl::new(100, 100)),
|
||||
settings: Box::new(LibrarySettingsImpl::new(100, 100).unwrap()),
|
||||
species: crate::static_data::libraries::species_library::tests::build(),
|
||||
moves: crate::static_data::libraries::move_library::tests::build(),
|
||||
items: crate::static_data::libraries::item_library::tests::build(),
|
||||
|
||||
Reference in New Issue
Block a user