aboutsummaryrefslogtreecommitdiff
path: root/src/statistics/damage.rs
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2018-06-13 13:07:48 +0200
committerDaniel Schadt <kingdread@gmx.de>2018-06-13 13:08:38 +0200
commitfe16699205b6b40aed8cafbe95820835a7052908 (patch)
tree1c0a7ea744e090f6c18f4f71472bd641764cdfe0 /src/statistics/damage.rs
parentcb20d6966a4c3d386925f812fe83b00f3f803db3 (diff)
downloadevtclib-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.rs95
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()
+ )
+ }
+}