1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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: u32,
}
/// 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, Default)]
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: u32,
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()
)
}
}
|