Implement most_frequent_action method.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use strum::EnumIter;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Clone, Default, PartialEq)]
|
#[derive(Debug, Deserialize, Clone, Default, PartialEq, EnumIter)]
|
||||||
pub enum Action {
|
pub enum Action {
|
||||||
CrackStudentBodyRightTackle,
|
CrackStudentBodyRightTackle,
|
||||||
Curls,
|
Curls,
|
||||||
|
|||||||
@@ -85,13 +85,14 @@ impl Event {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Clone, PartialEq)]
|
#[derive(Debug, Deserialize, Clone, PartialEq, Default)]
|
||||||
pub enum Team {
|
pub enum Team {
|
||||||
ArizonaState,
|
ArizonaState,
|
||||||
#[deprecated(since = "0.2.0", note = "Team left the project.")]
|
#[deprecated(since = "0.2.0", note = "Team left the project.")]
|
||||||
BoiseState,
|
BoiseState,
|
||||||
Colorado,
|
Colorado,
|
||||||
Iowa,
|
Iowa,
|
||||||
|
#[default]
|
||||||
Nebraska,
|
Nebraska,
|
||||||
SouthCarolina,
|
SouthCarolina,
|
||||||
Syracuse,
|
Syracuse,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use crate::error;
|
use crate::{Action, Team, error};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::{fs::File, path::PathBuf};
|
use std::{fs::File, path::PathBuf};
|
||||||
|
use strum::IntoEnumIterator;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Clone)]
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
pub struct LogFile(pub Vec<super::Game>);
|
pub struct LogFile(pub Vec<super::Game>);
|
||||||
@@ -22,6 +23,46 @@ impl LogFile {
|
|||||||
pub fn is_compatible(&self) -> bool {
|
pub fn is_compatible(&self) -> bool {
|
||||||
self.min_ver().cmp_precedence(&crate::MIN_VER).is_lt()
|
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 {
|
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 serde::Deserialize;
|
||||||
use strum::IntoEnumIterator;
|
use strum::IntoEnumIterator;
|
||||||
|
|
||||||
@@ -44,6 +44,7 @@ impl Game {
|
|||||||
pub fn deltas(&self, team: Team) -> Vec<i8> {
|
pub fn deltas(&self, team: Team) -> Vec<i8> {
|
||||||
let events: Vec<Event> = self
|
let events: Vec<Event> = self
|
||||||
.team_events(team)
|
.team_events(team)
|
||||||
|
.0
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|event| {
|
.filter_map(|event| {
|
||||||
if let Event::Quarter(_) = event {
|
if let Event::Quarter(_) = event {
|
||||||
@@ -68,18 +69,20 @@ impl Game {
|
|||||||
deltas
|
deltas
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn team_plays(&self, team: Team) -> usize {
|
pub fn team_plays(&self, team: Team) -> TeamPlays {
|
||||||
|
TeamPlays(
|
||||||
self.team_events(team)
|
self.team_events(team)
|
||||||
|
.0
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|event| {
|
.filter_map(|event| {
|
||||||
if let Event::Play(_) = event {
|
if let Event::Play(play) = event {
|
||||||
Some(event)
|
Some(play.to_owned())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect::<Vec<&Event>>()
|
.collect::<Vec<Play>>(),
|
||||||
.len()
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The average number of plays in a quarter.
|
/// The average number of plays in a quarter.
|
||||||
@@ -144,6 +147,7 @@ impl Game {
|
|||||||
|
|
||||||
pub fn penalties(&self, team: Team) -> usize {
|
pub fn penalties(&self, team: Team) -> usize {
|
||||||
self.team_events(team)
|
self.team_events(team)
|
||||||
|
.0
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|event| {
|
.filter_map(|event| {
|
||||||
if let Event::Penalty(_) = 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 events: Vec<Event> = vec![];
|
||||||
let mut first = true;
|
let mut first = true;
|
||||||
let mut record: bool = true;
|
let mut record: bool = true;
|
||||||
@@ -210,10 +214,15 @@ impl Game {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// If already handled or assumption override applicable
|
// 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)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Period {
|
pub struct Period {
|
||||||
period: Quarter,
|
period: Quarter,
|
||||||
@@ -283,7 +292,7 @@ pub enum Flags {
|
|||||||
IgnoreTeam(Team),
|
IgnoreTeam(Team),
|
||||||
IgnoreScore,
|
IgnoreScore,
|
||||||
Interval(u8),
|
Interval(u8),
|
||||||
SheerDumbFuckingLuck
|
SheerDumbFuckingLuck,
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ fn main() -> io::Result<()> {
|
|||||||
|
|
||||||
stats[team_idx]
|
stats[team_idx]
|
||||||
.plays_per_game
|
.plays_per_game
|
||||||
.push(game.team_plays(team.to_owned()));
|
.push(game.team_plays(team.to_owned()).0.len());
|
||||||
|
|
||||||
stats[team_idx]
|
stats[team_idx]
|
||||||
.penalties_per_game
|
.penalties_per_game
|
||||||
|
|||||||
Reference in New Issue
Block a user