diff --git a/gamelog/src/event.rs b/gamelog/src/event.rs index 1fa3628..ffc0058 100644 --- a/gamelog/src/event.rs +++ b/gamelog/src/event.rs @@ -1,5 +1,4 @@ -use crate::{Play, Team, TerrainState}; - +use crate::{Down, Play, Team, TerrainState, error}; use serde::Deserialize; type Offence = Team; @@ -13,6 +12,56 @@ pub enum Event { Score(ScorePoints), } +impl Event { + pub fn delta(&self, following: &Self) -> Option { + // Clean this trash spaghetti code up. + + fn make_play(event: &Event) -> Option { + match event { + Event::Kickoff(_) => 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 = make_play(following)?; + + if following.down? == Down::First { + if let TerrainState::Yards(yrds) = preceeding.terrain? { + Some(yrds as i8) + } else { + None + } + } else { + let a = if let TerrainState::Yards(yrds) = preceeding.terrain? { + yrds + } else { + unreachable!() + }; + + let b = if let TerrainState::Yards(yrds) = following.terrain? { + yrds + } else { + unreachable!() + }; + + Some((a - b) as i8) + } + } +} + #[derive(Debug, Deserialize, Clone, PartialEq)] pub enum ScorePoints { Touchdown, diff --git a/gamelog/src/play.rs b/gamelog/src/play.rs index 0a4d8ec..0f1610d 100644 --- a/gamelog/src/play.rs +++ b/gamelog/src/play.rs @@ -6,13 +6,7 @@ pub trait PlayHandle { fn plays(&self) -> Vec; } -pub trait Distance { - fn distance(&self) -> u8; - - fn delta(&self, d: D); -} - -#[derive(Debug, Deserialize, Clone, Default, PartialEq)] +#[derive(Debug, Deserialize, Clone, PartialEq)] pub struct Play { pub action: Action, pub down: Option, @@ -25,13 +19,13 @@ impl PlayHandle for Play { } } -impl Distance for Play { - fn distance(&self) -> u8 { - todo!() - } - - fn delta(&self, d: D) { - todo!() +impl Default for Play { + fn default() -> Self { + Self { + action: Action::default(), + down: Some(Down::First), + terrain: Some(TerrainState::Yards(10)), + } } }