Add delta fetching method to events.
This commit is contained in:
@@ -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<i8> {
|
||||
// Clean this trash spaghetti code up.
|
||||
|
||||
fn make_play(event: &Event) -> Option<Play> {
|
||||
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,
|
||||
|
||||
@@ -6,13 +6,7 @@ pub trait PlayHandle {
|
||||
fn plays(&self) -> Vec<Play>;
|
||||
}
|
||||
|
||||
pub trait Distance {
|
||||
fn distance(&self) -> u8;
|
||||
|
||||
fn delta<D: Distance>(&self, d: D);
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone, Default, PartialEq)]
|
||||
#[derive(Debug, Deserialize, Clone, PartialEq)]
|
||||
pub struct Play {
|
||||
pub action: Action,
|
||||
pub down: Option<Down>,
|
||||
@@ -25,13 +19,13 @@ impl PlayHandle for Play {
|
||||
}
|
||||
}
|
||||
|
||||
impl Distance for Play {
|
||||
fn distance(&self) -> u8 {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn delta<D: Distance>(&self, d: D) {
|
||||
todo!()
|
||||
impl Default for Play {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
action: Action::default(),
|
||||
down: Some(Down::First),
|
||||
terrain: Some(TerrainState::Yards(10)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user