diff options
author | Daniel Schadt <kingdread@gmx.de> | 2018-06-13 13:07:48 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2018-06-13 13:08:38 +0200 |
commit | fe16699205b6b40aed8cafbe95820835a7052908 (patch) | |
tree | 1c0a7ea744e090f6c18f4f71472bd641764cdfe0 /src/statistics/damage.rs | |
parent | cb20d6966a4c3d386925f812fe83b00f3f803db3 (diff) | |
download | evtclib-fe16699205b6b40aed8cafbe95820835a7052908.tar.gz evtclib-fe16699205b6b40aed8cafbe95820835a7052908.tar.bz2 evtclib-fe16699205b6b40aed8cafbe95820835a7052908.zip |
rework damage tracker
Diffstat (limited to 'src/statistics/damage.rs')
-rw-r--r-- | src/statistics/damage.rs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/statistics/damage.rs b/src/statistics/damage.rs new file mode 100644 index 0000000..fdf723f --- /dev/null +++ b/src/statistics/damage.rs @@ -0,0 +1,95 @@ +use super::math::{Monoid, RecordFunc, Semigroup}; +use std::fmt; + +/// Type of the damage. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum DamageType { + Physical, + Condition, +} + +/// Meta information about a damage log entry. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Meta { + pub source: u64, + pub target: u64, + pub kind: DamageType, + pub skill: u16, +} + +/// A small wrapper that wraps a damage number. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Damage(pub u64); + +impl Semigroup for Damage { + #[inline] + fn combine(&self, other: &Self) -> Self { + Damage(self.0 + other.0) + } +} + +impl Monoid for Damage { + #[inline] + fn mempty() -> Self { + Damage(0) + } +} + +/// Provides access to the damage log. +#[derive(Clone)] +pub struct DamageLog { + inner: RecordFunc<u64, Meta, Damage>, +} + +impl DamageLog { + pub fn new() -> Self { + DamageLog { + inner: RecordFunc::new(), + } + } + + pub fn log( + &mut self, + time: u64, + source: u64, + target: u64, + kind: DamageType, + skill: u16, + value: u64, + ) { + self.inner.insert( + time, + Meta { + source, + target, + kind, + skill, + }, + Damage(value), + ) + } + + pub fn damage_between<F: FnMut(&Meta) -> bool>( + &self, + start: u64, + stop: u64, + filter: F, + ) -> Damage { + self.inner.between_only(&start, &stop, filter) + } + + pub fn damage<F: FnMut(&Meta) -> bool>(&self, filter: F) -> Damage { + self.inner.tally_only(filter) + } +} + +impl fmt::Debug for DamageLog { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "DamageLog {{ {} events logged, {:?} total damage }}", + self.inner.len(), + self.inner.tally() + ) + } +} |