Skip to main content

mlbt_api/
plays.rs

1use crate::live::Person;
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6#[derive(Default, Debug, Serialize, Deserialize)]
7#[serde(rename_all = "camelCase")]
8pub struct Plays {
9    pub all_plays: Option<Vec<Play>>,
10    pub current_play: Option<Play>,
11    pub scoring_plays: Option<Vec<u8>>,
12    pub plays_by_inning: Option<Vec<PlaysByInning>>,
13}
14
15#[derive(Default, Debug, Serialize, Deserialize)]
16#[serde(rename_all = "camelCase")]
17pub struct Play {
18    pub result: Result,
19    pub about: About,
20    pub count: Count,
21    pub matchup: Matchup,
22    pub play_events: Vec<PlayEvent>,
23}
24
25#[derive(Default, Debug, Serialize, Deserialize)]
26#[serde(rename_all = "camelCase")]
27pub struct PlaysByInning {
28    pub start_index: u8,
29    pub end_index: u8,
30    pub top: Vec<u8>,
31    pub bottom: Vec<u8>,
32}
33
34#[derive(Default, Debug, Serialize, Deserialize)]
35#[serde(rename_all = "camelCase")]
36pub struct Result {
37    // #[serde(rename = "type")]
38    // pub result_type: Option<ResultType>,
39    pub event: Option<String>,
40    pub event_type: Option<String>,
41    pub description: Option<String>,
42    pub rbi: Option<u8>,
43    pub away_score: Option<u8>,
44    pub home_score: Option<u8>,
45    pub is_out: Option<bool>,
46}
47
48#[derive(Default, Debug, Serialize, Deserialize)]
49#[serde(rename_all = "camelCase")]
50pub struct About {
51    pub at_bat_index: u8,
52    pub half_inning: String,
53    pub is_top_inning: bool,
54    pub inning: u8,
55    pub is_complete: bool,
56    pub is_scoring_play: Option<bool>,
57}
58
59#[derive(Default, Debug, Serialize, Deserialize)]
60#[serde(rename_all = "camelCase")]
61pub struct Matchup {
62    pub batter: Person,
63    pub bat_side: Side,
64    pub pitcher: Person,
65    pub pitch_hand: Side,
66    pub batter_hot_cold_zones: Option<Vec<Zone>>,
67    pub post_on_first: Option<Person>,
68    pub post_on_second: Option<Person>,
69    pub post_on_third: Option<Person>,
70}
71
72#[derive(Default, Debug, Serialize, Deserialize)]
73pub struct Side {
74    pub code: String,
75}
76
77#[derive(Debug, Serialize, Deserialize)]
78pub struct Zone {
79    pub zone: String,
80    pub color: String, // this is what I want: "rgba(255, 255, 255, 0.55)" -> need to convert it to a color
81    pub value: String,
82}
83
84#[derive(Default, Debug, Serialize, Deserialize)]
85#[serde(rename_all = "camelCase")]
86pub struct PlayEvent {
87    pub details: Details,
88    pub count: Count,
89    pub pitch_data: Option<PitchData>,
90    pub hit_data: Option<HitData>,
91    pub is_pitch: bool,
92    pub is_base_running_play: Option<bool>,
93    pub pitch_number: Option<u8>,
94    pub review_details: Option<ReviewDetails>,
95}
96
97#[derive(Default, Debug, Serialize, Deserialize)]
98#[serde(rename_all = "camelCase")]
99pub struct ReviewDetails {
100    pub is_overturned: bool,
101    pub in_progress: bool,
102    pub review_type: String,
103    pub challenge_team_id: u16,
104    pub player: Option<Person>,
105}
106
107#[derive(Clone, Default, Debug, Serialize, Deserialize)]
108pub struct Count {
109    pub balls: u8,
110    pub strikes: u8,
111    pub outs: u8,
112}
113
114#[derive(Default, Debug, Serialize, Deserialize)]
115#[serde(rename_all = "camelCase")]
116pub struct Details {
117    pub description: Option<String>,
118    pub call: Option<CodeDescription>,
119    pub ball_color: Option<String>,
120    pub trail_color: Option<String>,
121    pub is_in_play: Option<bool>,
122    pub is_strike: Option<bool>,
123    pub is_ball: Option<bool>,
124    pub is_scoring_play: Option<bool>,
125    pub away_score: Option<u8>,
126    pub home_score: Option<u8>,
127    #[serde(rename = "type")]
128    pub pitch_type: Option<CodeDescription>,
129}
130
131#[derive(Clone, Default, Debug, Serialize, Deserialize)]
132pub struct CodeDescription {
133    #[serde(default = "CodeDescription::default_code")]
134    pub code: String,
135    pub description: String,
136}
137
138impl CodeDescription {
139    /// Default code to "UNKNOWN" if the code key is missing.
140    fn default_code() -> String {
141        "UNKNOWN".to_owned()
142    }
143}
144
145#[derive(Default, Debug, Serialize, Deserialize)]
146#[serde(rename_all = "camelCase")]
147pub struct PitchData {
148    pub start_speed: Option<f64>,
149    pub end_speed: Option<f64>,
150    pub strike_zone_top: Option<f64>,
151    pub strike_zone_bottom: Option<f64>,
152    pub coordinates: HashMap<String, f64>,
153    pub breaks: Option<Breaks>,
154    pub zone: Option<u8>,
155    pub plate_time: Option<f64>,
156}
157
158#[derive(Default, Debug, Serialize, Deserialize)]
159#[serde(rename_all = "camelCase")]
160pub struct HitData {
161    pub launch_speed: Option<f64>,
162    pub launch_angle: Option<f64>,
163    pub total_distance: Option<f64>,
164    pub trajectory: Option<String>,
165    pub hardness: Option<String>,
166}
167
168#[derive(Default, Debug, Serialize, Deserialize)]
169#[serde(rename_all = "camelCase")]
170pub struct Breaks {
171    pub break_angle: Option<f64>,
172    pub break_length: Option<f64>,
173    pub break_y: Option<f64>,
174    pub spin_rate: Option<u32>,
175    pub spin_direction: Option<u32>,
176}