use crate::model::stat::Stat;
use crate::model::stattable::StatTable;
pub type StatValue = (Stat, f32);
pub type StatableIter<'a> = Box<dyn Iterator<Item=StatValue>+'a>;
pub trait Statable {
fn iter(&self) -> StatableIter;
fn get(&self, stat_type: &Stat) -> f32{
self.iter()
.filter(|x| x.0 == *stat_type)
.map(|x| x.1)
.sum()
}
fn chain(&self, other: Box<dyn Statable>) -> Box<dyn Statable>{
let mut res = StatTable::new();
res.add_table(self.iter());
res.add_table(other.iter());
Box::new(res)
}
}
pub trait ModifiableStatable: Statable {
fn add(&mut self, stat_type: &Stat, value: f32) -> f32;
fn add_table(&mut self, other: StatableIter) -> &mut Self{
other.for_each(|(k, v)| {
self.add(&k, v);
});
self
}
}
#[cfg(test)] mod tests {
use super::*;
#[test] fn test_chainging_stattables() {
let s1 = StatTable::of(&vec![(Stat::FlatATK, 100.)]);
let s2 = StatTable::of(&vec![(Stat::FlatATK, 100.)]);
let s3 = s1.chain(Box::new(s2));
assert_eq!(s3.get(&Stat::FlatATK), 200.);
}
}