Implement most_frequent_action method.
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<super::Game>);
|
||||
@@ -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::<Vec<()>>()
|
||||
.len(),
|
||||
)
|
||||
})
|
||||
.sum::<usize>();
|
||||
|
||||
if found > frequency {
|
||||
frequency = found;
|
||||
most_common_action = action.to_owned();
|
||||
}
|
||||
}
|
||||
|
||||
most_common_action
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<File> for LogFile {
|
||||
|
||||
@@ -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<i8> {
|
||||
let events: Vec<Event> = 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::<Vec<&Event>>()
|
||||
.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::<Vec<Play>>(),
|
||||
)
|
||||
}
|
||||
|
||||
/// 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<Event> {
|
||||
pub fn team_events(&self, team: Team) -> TeamEvents {
|
||||
let mut events: Vec<Event> = 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<Event>);
|
||||
|
||||
pub struct TeamPlays(pub Vec<Play>);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Period {
|
||||
period: Quarter,
|
||||
@@ -283,7 +292,7 @@ pub enum Flags {
|
||||
IgnoreTeam(Team),
|
||||
IgnoreScore,
|
||||
Interval(u8),
|
||||
SheerDumbFuckingLuck
|
||||
SheerDumbFuckingLuck,
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user