Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add date picker to stats and standings screens and other QOL updates #41

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/src/boxscore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub struct Batting {
pub runs: Option<u16>,
doubles: Option<u16>,
triples: Option<u16>,
home_runs: Option<u16>,
pub home_runs: Option<u16>,
pub strike_outs: Option<u16>,
pub base_on_balls: Option<u16>,
pub hits: Option<u16>,
Expand Down
38 changes: 36 additions & 2 deletions api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ impl MLBApi {
self.get(url).await
}

pub async fn get_standings_on_date<T: Datelike>(&self, date: T) -> StandingsResponse {
AxBolduc marked this conversation as resolved.
Show resolved Hide resolved
let url = format!(
"{}v1/standings?sportId=1&season={}&date={}-{}-{}&leagueId=103,104",
self.base_url,
date.year(),
date.year(),
date.month(),
date.day(),
);
self.get(url).await
}

pub async fn get_team_stats(&self, group: StatGroup) -> StatResponse {
let local: DateTime<Local> = Local::now();
let url = format!(
Expand All @@ -92,6 +104,17 @@ impl MLBApi {
self.get(url).await
}

pub async fn get_team_stats_on_date(&self, group: StatGroup, date: NaiveDate) -> StatResponse {
let url = format!(
"{}v1/teams/stats?sportId=1&stats=byDateRange&season={}&endDate={}&group={}",
self.base_url,
date.year(),
date.format("%Y-%m-%d"),
group
);
self.get(url).await
}

pub async fn get_player_stats(&self, group: StatGroup) -> StatResponse {
let local: DateTime<Local> = Local::now();
let url = format!(
Expand All @@ -103,15 +126,26 @@ impl MLBApi {
self.get(url).await
}

pub async fn get_player_stats_on_date(&self, group: StatGroup, date: NaiveDate) -> StatResponse {
let url = format!(
"{}v1/stats?stats=byDateRange&season={}&endDate={}&group={}",
self.base_url,
date.year(),
date.format("%Y-%m-%d"),
group
);
self.get(url).await
}

async fn get<T: Default + DeserializeOwned>(&self, url: String) -> T {
let response = self.client.get(url).send().await.expect("network error");
response
.json::<T>()
.await
.map(From::from)
.unwrap_or_else(|err| {
eprintln!("parsing error {:?}", err);
T::default()
eprintln!("parsing error {:?}", err);
T::default()
})
}
}
Expand Down
30 changes: 13 additions & 17 deletions api/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub struct DisplayName {
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Split {
season: String,
pub stat: StatSplit,
pub team: IdNameLink,
pub player: Option<Player>,
Expand Down Expand Up @@ -53,30 +52,19 @@ pub enum StatSplit {
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PitchingStat {
pub wins: u16,
pub losses: u16,
pub era: String,
pub games_played: u16,
pub games_started: u16,
pub complete_games: u16,
pub shutouts: u16,
pub saves: u16,
pub save_opportunities: u16,
ground_outs: u16,
air_outs: u16,
pub innings_pitched: String,
pub hits: u16,
pub runs: u16,
pub earned_runs: u16,
doubles: i64,
triples: i64,
pub home_runs: u16,
pub hit_batsmen: u16,
pub base_on_balls: u16,
pub strike_outs: u16,
pub base_on_balls: u16,
intentional_walks: u16,
pub hits: u16,
hit_by_pitch: u16,
pub whip: String,
pub avg: String,
at_bats: u16,
obp: String,
Expand All @@ -87,16 +75,26 @@ pub struct PitchingStat {
stolen_base_percentage: String,
ground_into_double_play: u16,
number_of_pitches: u16,
pub era: String,
pub innings_pitched: String,
pub wins: u16,
pub losses: u16,
pub saves: u16,
pub save_opportunities: u16,
holds: u16,
blown_saves: u16,
pub earned_runs: u16,
pub whip: String,
batters_faced: u16,
outs: u16,
games_pitched: u16,
pub complete_games: u16,
pub shutouts: u16,
strikes: u32,
strike_percentage: String,
pub hit_batsmen: u16,
balks: u16,
wild_pitches: u16,
pickoffs: u16,
total_bases: u16,
ground_outs_to_airouts: String,
win_percentage: String,
Expand All @@ -108,7 +106,6 @@ pub struct PitchingStat {
hits_per9_inn: String,
runs_scored_per9: String,
home_runs_per9: String,
catchers_interference: u16,
sac_bunts: u16,
sac_flies: u16,
}
Expand Down Expand Up @@ -146,6 +143,5 @@ pub struct HittingStat {
pub sac_flies: u16,
pub babip: String,
pub ground_outs_to_airouts: String,
pub catchers_interference: u16,
pub at_bats_per_home_run: String,
}
68 changes: 67 additions & 1 deletion api/tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,31 @@ mod tests {
}

#[tokio::test]
async fn test_player_stats() {
async fn test_team_stats_on_date() {
let mut server = mockito::Server::new();

let date: NaiveDate = NaiveDate::from_ymd_opt(2022, 07, 09).unwrap();
for group in vec![StatGroup::Hitting, StatGroup::Pitching] {
let url = format!(
"v1/teams/stats?sportId=1&stats=byDateRange&season=2022&endDate=2022-07-09&group={}",
group
);

let _m = server
.mock("GET", Matcher::Exact(url))
.with_status(200)
.with_header("content-type", "application/json;charset=UTF-8")
.with_body_from_file("./tests/responses/team-stats-date.json")
.create();

let resp = CLIENT.get_team_stats_on_date(group, date).await;
println!("{:?}", resp);
}
}

#[tokio::test]
async fn test_player_stats() {
let mut server = mockito::Server::new();
for group in vec![StatGroup::Hitting, StatGroup::Pitching] {
let url = format!("v1/stats?stats=season&season=2021&group={}", group);

Expand All @@ -101,4 +123,48 @@ mod tests {
println!("{:?}", resp);
}
}

#[tokio::test]
async fn test_player_stats_on_date() {
let mut server = mockito::Server::new();
let date: NaiveDate = NaiveDate::from_ymd_opt(2022, 07, 09).unwrap();
for group in vec![StatGroup::Hitting, StatGroup::Pitching] {
let url = format!(
"v1/stats?sportId=1&stats=byDateRange&season=2022&endDate=2022-07-09&group={}",
group
);

let _m = server
.mock("GET", Matcher::Exact(url))
.with_status(200)
.with_header("content-type", "application/json;charset=UTF-8")
.with_body_from_file("./tests/responses/player-stats-date.json")
.create();

let resp = CLIENT.get_player_stats_on_date(group, date).await;
println!("{:?}", resp);
}
}

#[test]
fn test_player_stats_on_date() {
let client = MLBApiBuilder::default().build().unwrap();
let date: NaiveDate = NaiveDate::from_ymd(2022, 07, 09);
for group in vec![StatGroup::Hitting, StatGroup::Pitching] {
let url = format!(
"v1/stats?sportId=1&stats=byDateRange&season=2022&endDate=2022-07-09&group={}",
group
);

let _m = mock("GET", Matcher::Exact(url))
.with_status(200)
.with_header("content-type", "application/json;charset=UTF-8")
.with_body_from_file("./tests/responses/player-stats-date.json")
.create();

let resp = client.get_player_stats_on_date(group, date);
println!("{:?}", resp);
}
}

}
Loading
Loading