//! This module contains specific filters that operate on log files. //! //! This is the "base unit", as each file corresponds to one log. Filters on other items (such as //! players) have to be lifted into log filters first. use super::{ super::{FightOutcome, LogResult, Weekday}, Filter, Inclusion, }; use std::collections::HashSet; use evtclib::raw::parser::PartialEvtc; use evtclib::statistics::gamedata::Boss; use chrono::{Datelike, NaiveDateTime}; use num_traits::FromPrimitive as _; /// Filter trait used for filters that operate on complete logs. pub trait LogFilter = Filter; #[derive(Debug, Clone)] pub struct BossFilter(HashSet); impl BossFilter { pub fn new(bosses: HashSet) -> Box { Box::new(BossFilter(bosses)) } } impl Filter for BossFilter { fn filter_early(&self, partial_evtc: &PartialEvtc) -> Inclusion { let boss = Boss::from_u16(partial_evtc.header.combat_id); boss.map(|b| self.0.contains(&b).into()) .unwrap_or(Inclusion::Include) } fn filter(&self, log: &LogResult) -> bool { let boss = Boss::from_u16(log.boss_id); boss.map(|b| self.0.contains(&b)).unwrap_or(false) } } #[derive(Debug, Clone)] pub struct OutcomeFilter(HashSet); impl OutcomeFilter { pub fn new(outcomes: HashSet) -> Box { Box::new(OutcomeFilter(outcomes)) } } impl Filter for OutcomeFilter { fn filter(&self, log: &LogResult) -> bool { self.0.contains(&log.outcome) } } #[derive(Debug, Clone)] pub struct WeekdayFilter(HashSet); impl WeekdayFilter { pub fn new(weekdays: HashSet) -> Box { Box::new(WeekdayFilter(weekdays)) } } impl Filter for WeekdayFilter { fn filter(&self, log: &LogResult) -> bool { self.0.contains(&log.time.weekday()) } } #[derive(Debug, Clone)] pub struct TimeFilter(Option, Option); impl TimeFilter { pub fn new(after: Option, before: Option) -> Box { Box::new(TimeFilter(after, before)) } } impl Filter for TimeFilter { fn filter(&self, log: &LogResult) -> bool { let after_ok = match self.0 { Some(time) => time <= log.time, None => true, }; let before_ok = match self.1 { Some(time) => time >= log.time, None => true, }; after_ok && before_ok } }