Fixes for FFI.
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone Build is passing

This commit is contained in:
2022-10-01 15:40:15 +02:00
parent 78fde698ca
commit 84ddf0307d
15 changed files with 457 additions and 111 deletions

View File

@@ -1,9 +1,4 @@
use std::sync::Arc;
use hashbrown::HashMap;
use crate::static_data::Statistic;
use crate::StringKey;
/// A nature is an attribute on a Pokemon that modifies the effective base stats on a Pokemon. They
/// can have an increased statistic and a decreased statistic, or be neutral.
@@ -57,90 +52,3 @@ impl Nature {
}
}
}
/// A library of all natures that can be used, stored by their names.
#[derive(Debug)]
pub struct NatureLibrary {
/// The underlying data structure.
map: HashMap<StringKey, Arc<Nature>>,
}
impl NatureLibrary {
/// Creates a new nature library with a given capacity.
pub fn new(capacity: usize) -> Self {
NatureLibrary {
map: HashMap::with_capacity(capacity),
}
}
/// Adds a new nature with name to the library.
pub fn load_nature(&mut self, name: StringKey, nature: Nature) {
self.map.insert(name, Arc::new(nature));
}
/// Gets a nature by name.
pub fn get_nature(&self, key: &StringKey) -> Option<&Arc<Nature>> {
self.map.get(key)
}
/// Finds a nature name by nature.
pub fn get_nature_name(&self, nature: &Arc<Nature>) -> StringKey {
for kv in &self.map {
// As natures can't be copied, and should always be the same reference as the value
// in the map, we just compare by reference.
if Arc::ptr_eq(kv.1, nature) {
return kv.0.clone();
}
}
panic!("No name was found for the given nature. This should never happen.");
}
}
#[cfg(test)]
pub mod tests {
use crate::static_data::natures::{Nature, NatureLibrary};
use crate::static_data::statistics::Statistic;
pub fn build() -> NatureLibrary {
let mut lib = NatureLibrary::new(2);
lib.load_nature(
"test_nature".into(),
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
);
lib
}
#[test]
fn create_nature_library_insert_and_retrieve() {
let mut lib = NatureLibrary::new(2);
lib.load_nature("foo".into(), Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9));
lib.load_nature(
"bar".into(),
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
);
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
assert_eq!(n1.increase_stat, Statistic::HP);
assert_eq!(n1.decrease_stat, Statistic::Attack);
assert_eq!(n1.increase_modifier, 1.1);
assert_eq!(n1.decrease_modifier, 0.9);
}
#[test]
fn create_nature_library_insert_and_get_name() {
let mut lib = NatureLibrary::new(2);
lib.load_nature("foo".into(), Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9));
lib.load_nature(
"bar".into(),
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
);
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
let name = lib.get_nature_name(n1);
assert_eq!(name, "foo".into());
let n2 = lib.get_nature(&"bar".into()).expect("Nature was not found");
let name2 = lib.get_nature_name(n2);
assert_eq!(name2, "bar".into());
}
}