aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2020-04-29 14:55:50 +0200
committerDaniel Schadt <kingdread@gmx.de>2020-04-29 14:55:50 +0200
commit03dfb82d19cc017e10017b880bca7ea8300af108 (patch)
tree664eee3bdcdcab22fbfaca7a8c19a9d1230f211e /src
parent3e8b1c186bb4e748920cce002bd23bb60b52cb04 (diff)
downloadevtclib-03dfb82d19cc017e10017b880bca7ea8300af108.tar.gz
evtclib-03dfb82d19cc017e10017b880bca7ea8300af108.tar.bz2
evtclib-03dfb82d19cc017e10017b880bca7ea8300af108.zip
add some convenience methods to Log
Those are methods that are probably useful to some applications, and it feels like some of that data should even be in the file header. Due to the evtc limitations though, we need to loop through the events to access it, which means that every application would have to implement this. Those functions should be kept in a separate impl though, as they are more costly to call than the other accessors. Maybe they should even be moved to an "extension trait", though it's not clear whether putting this behind a trait would be idiomatic Rust. The advantage would be that users would have to specifically import the trait, thereby making sure they're aware of the performance implications.
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index b463ac3..b77f3bc 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -689,6 +689,70 @@ impl Log {
}
}
+/// Convenience data accessing funtions for [`Log`][Log]s.
+///
+/// The information that is gathered by those functions is "expensive" to compute, as we have to
+/// loop through every event. They are not saved in the header, and instead are implemented using
+/// special [`EventKind`][EventKind]s. This is not a limitation of `evtclib`, but rather a result
+/// of how arcdps stores the data.
+///
+/// This also means that those functions are fallible because we cannot guarantee that the special
+/// events that we're looking for is actually present in every log file.
+///
+/// Use those functions only if necessary, and prefer to cache the result if it will be reused!
+impl Log {
+ /// Get the timestamp of when the log was started.
+ ///
+ /// The returned value is a unix timestamp in the local time zone.
+ ///
+ /// If the [`LogStart`][EventKind::LogStart] event cannot be found, this function returns
+ /// `None`.
+ pub fn local_start_timestamp(&self) -> Option<u32> {
+ self.events().iter().find_map(|e| {
+ if let EventKind::LogStart {
+ local_timestamp, ..
+ } = e.kind
+ {
+ Some(local_timestamp)
+ } else {
+ None
+ }
+ })
+ }
+
+ /// Get the timestamp of when the log was ended.
+ ///
+ /// The returned value is a unix timestamp in the local time zone.
+ ///
+ /// If the [`LogEnd`][EventKind::LogEnd] event cannot be found, this function returns `None`.
+ pub fn local_end_timestamp(&self) -> Option<u32> {
+ self.events().iter().find_map(|e| {
+ if let EventKind::LogEnd {
+ local_timestamp, ..
+ } = e.kind
+ {
+ Some(local_timestamp)
+ } else {
+ None
+ }
+ })
+ }
+
+ /// Check if rewards for this fight have been given out.
+ ///
+ /// This can be used as an indication whether the fight was successful (`true`) or not
+ /// (`false`).
+ pub fn was_rewarded(&self) -> bool {
+ self.events().iter().any(|e| {
+ if let EventKind::Reward { .. } = e.kind {
+ true
+ } else {
+ false
+ }
+ })
+ }
+}
+
pub fn process(data: &raw::Evtc) -> Result<Log, EvtcError> {
// Prepare "augmented" agents
let mut agents = setup_agents(data)?;