use crate::model::stattable::StatTable;
use crate::model::statable::Statable;
use crate::model::operation::Operation;
pub struct Rotation {
inner: std::collections::HashMap<String, Operation>,
}
impl Rotation {
pub fn new() -> Self {
Self{inner: std::collections::HashMap::new()}
}
pub fn of(actions: Vec<(String, Operation)>) -> Self {
let mut map = std::collections::HashMap::new();
for (k, v) in actions {
map.insert(k, v);
}
Self{inner: map}
}
pub fn add(&mut self, name: String, action: Operation) -> &mut Self {
self.inner.insert(name, action);
self
}
pub fn evaluate(&self, stats: &StatTable) -> f32 {
self.inner.iter()
.map(|x| x.1(stats))
.sum()
}
pub fn copy(&self) -> Self {
Self::new()
}
}
#[cfg(test)] mod tests {
use super::*;
use crate::model::stat::Stat;
#[test] fn empty_rotation_returns_0() {
let res = Rotation::new().evaluate(&StatTable::new());
debug_assert_eq!(res, 0.0)
}
#[test] fn populated_rotation_returns_sum_of_action_results() {
let r = Rotation::of(vec![
(String::from("test"), Box::new(|stats| stats.get(&Stat::FlatATK))),
(String::from("test2"), Box::new(|stats| stats.get(&Stat::FlatATK))),
]);
let s = StatTable::of(&[
(Stat::FlatATK, 1.6),
]);
let res = r.evaluate(&s);
debug_assert_eq!(res, 1.6*2.0)
}
}