From 03dfb82d19cc017e10017b880bca7ea8300af108 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Wed, 29 Apr 2020 14:55:50 +0200 Subject: 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. --- src/lib.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'src/lib.rs') 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 { + 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 { + 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 { // Prepare "augmented" agents let mut agents = setup_agents(data)?; -- cgit v1.2.3