From 31515fc806b8e1df7db7497fbef4b9828f3e3bb9 Mon Sep 17 00:00:00 2001 From: Cutieguwu Date: Wed, 23 Apr 2025 19:15:27 -0400 Subject: [PATCH] Implement most_frequent_action method. --- gamelog/src/action.rs | 3 ++- gamelog/src/event.rs | 3 ++- gamelog/src/file.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- gamelog/src/game.rs | 41 +++++++++++++++++++++++++---------------- miller/src/main.rs | 2 +- 5 files changed, 72 insertions(+), 20 deletions(-) diff --git a/gamelog/src/action.rs b/gamelog/src/action.rs index 40ca054..b812d75 100644 --- a/gamelog/src/action.rs +++ b/gamelog/src/action.rs @@ -1,6 +1,7 @@ use serde::Deserialize; +use strum::EnumIter; -#[derive(Debug, Deserialize, Clone, Default, PartialEq)] +#[derive(Debug, Deserialize, Clone, Default, PartialEq, EnumIter)] pub enum Action { CrackStudentBodyRightTackle, Curls, diff --git a/gamelog/src/event.rs b/gamelog/src/event.rs index f53f9be..e6f2b75 100644 --- a/gamelog/src/event.rs +++ b/gamelog/src/event.rs @@ -85,13 +85,14 @@ impl Event { } } -#[derive(Debug, Deserialize, Clone, PartialEq)] +#[derive(Debug, Deserialize, Clone, PartialEq, Default)] pub enum Team { ArizonaState, #[deprecated(since = "0.2.0", note = "Team left the project.")] BoiseState, Colorado, Iowa, + #[default] Nebraska, SouthCarolina, Syracuse, diff --git a/gamelog/src/file.rs b/gamelog/src/file.rs index b21a035..08c1951 100644 --- a/gamelog/src/file.rs +++ b/gamelog/src/file.rs @@ -1,6 +1,7 @@ -use crate::error; +use crate::{Action, Team, error}; use serde::Deserialize; use std::{fs::File, path::PathBuf}; +use strum::IntoEnumIterator; #[derive(Debug, Deserialize, Clone)] pub struct LogFile(pub Vec); @@ -22,6 +23,46 @@ impl LogFile { pub fn is_compatible(&self) -> bool { self.min_ver().cmp_precedence(&crate::MIN_VER).is_lt() } + + /// Returns the most common action for a given team. + pub fn most_frequent_action(&self, team: Team) -> Action { + let mut most_common_action = Action::Unknown; + let mut frequency = 0; + + for action in Action::iter() { + if action == Action::Unknown { + continue; + } + + let found = self + .0 + .iter() + .filter_map(|game| { + Some( + game.team_plays(team.to_owned()) + .0 + .iter() + .filter_map(|play| { + if play.action == action { + Some(()) + } else { + None + } + }) + .collect::>() + .len(), + ) + }) + .sum::(); + + if found > frequency { + frequency = found; + most_common_action = action.to_owned(); + } + } + + most_common_action + } } impl TryFrom for LogFile { diff --git a/gamelog/src/game.rs b/gamelog/src/game.rs index 3958078..acc8c31 100644 --- a/gamelog/src/game.rs +++ b/gamelog/src/game.rs @@ -1,4 +1,4 @@ -use crate::{Event, Quarter, Team, error}; +use crate::{Event, Play, Quarter, Team, error}; use serde::Deserialize; use strum::IntoEnumIterator; @@ -44,6 +44,7 @@ impl Game { pub fn deltas(&self, team: Team) -> Vec { let events: Vec = self .team_events(team) + .0 .iter() .filter_map(|event| { if let Event::Quarter(_) = event { @@ -68,18 +69,20 @@ impl Game { deltas } - pub fn team_plays(&self, team: Team) -> usize { - self.team_events(team) - .iter() - .filter_map(|event| { - if let Event::Play(_) = event { - Some(event) - } else { - None - } - }) - .collect::>() - .len() + pub fn team_plays(&self, team: Team) -> TeamPlays { + TeamPlays( + self.team_events(team) + .0 + .iter() + .filter_map(|event| { + if let Event::Play(play) = event { + Some(play.to_owned()) + } else { + None + } + }) + .collect::>(), + ) } /// The average number of plays in a quarter. @@ -144,6 +147,7 @@ impl Game { pub fn penalties(&self, team: Team) -> usize { self.team_events(team) + .0 .iter() .filter_map(|event| { if let Event::Penalty(_) = event { @@ -179,7 +183,7 @@ impl Game { } } - pub fn team_events(&self, team: Team) -> Vec { + pub fn team_events(&self, team: Team) -> TeamEvents { let mut events: Vec = vec![]; let mut first = true; let mut record: bool = true; @@ -210,10 +214,15 @@ impl Game { }); // If already handled or assumption override applicable - events + TeamEvents(events) } } +#[derive(Debug)] +pub struct TeamEvents(pub Vec); + +pub struct TeamPlays(pub Vec); + #[derive(Debug, Clone)] pub struct Period { period: Quarter, @@ -283,7 +292,7 @@ pub enum Flags { IgnoreTeam(Team), IgnoreScore, Interval(u8), - SheerDumbFuckingLuck + SheerDumbFuckingLuck, } /* diff --git a/miller/src/main.rs b/miller/src/main.rs index ee4ddbd..84eba41 100644 --- a/miller/src/main.rs +++ b/miller/src/main.rs @@ -83,7 +83,7 @@ fn main() -> io::Result<()> { stats[team_idx] .plays_per_game - .push(game.team_plays(team.to_owned())); + .push(game.team_plays(team.to_owned()).0.len()); stats[team_idx] .penalties_per_game