Clean up delta calculation.

This commit is contained in:
Cutieguwu
2025-04-30 21:14:57 -04:00
parent 07e9d0b5b6
commit 4d8f1d2d46
2 changed files with 24 additions and 26 deletions

View File

@@ -18,34 +18,13 @@ impl Event {
/// Returns `None` if no delta can be calculated between /// Returns `None` if no delta can be calculated between
/// `self` and following events. /// `self` and following events.
pub fn delta(&self, following: &Self) -> Option<i8> { pub fn delta(&self, following: &Self) -> Option<i8> {
// Clean this trash spaghetti code up. let preceeding = self.to_play()?;
fn make_play(event: &Event) -> Option<Play> {
match event {
Event::Kickoff(_) | Event::Turnover(_) => Some(Play::default()),
Event::Play(play) => {
let p = play.to_owned();
if p.down.is_none()
|| p.terrain.is_none()
|| p.terrain.as_ref()? == &TerrainState::Unknown
{
None
} else {
Some(p)
}
}
_ => None,
}
}
let preceeding = make_play(self)?;
let following = if let Event::Turnover(_) = following { let following = if let Event::Turnover(_) = following {
// I should really just early return // I should really just early return
// but this is too funny to look at. // but this is too funny to look at.
None? None?
} else { } else {
make_play(following)? following.to_play()?
}; };
if following.down? == Down::First { if following.down? == Down::First {
@@ -90,6 +69,21 @@ impl Event {
None None
} }
} }
/// Converts an event into it's associated Play object, if there is one.
fn to_play(self: &Event) -> Option<Play> {
if let Event::Play(play) = self {
if play.down.is_none() || play.terrain.is_none() {
None
} else {
Some(play.to_owned())
}
} else if let Event::Kickoff(_) | Event::Turnover(_) = self {
Some(Play::default())
} else {
None
}
}
} }
#[derive(Debug, Deserialize, Clone, PartialEq, Default)] #[derive(Debug, Deserialize, Clone, PartialEq, Default)]

View File

@@ -1,10 +1,14 @@
use serde::Deserialize; use serde::Deserialize;
#[derive(Debug, Deserialize, Clone, Default, PartialEq)] #[derive(Debug, Deserialize, Clone, PartialEq)]
pub enum TerrainState { pub enum TerrainState {
Yards(u8), Yards(u8),
GoalLine, GoalLine,
Inches, Inches,
#[default] }
Unknown,
impl Default for TerrainState {
fn default() -> Self {
TerrainState::Yards(10)
}
} }