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 all 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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ crossterm = "0.26.1"
crossbeam-channel = "0.5.8"
directories = "5.0.1"
indexmap = "2.0.0"
mlb-api = { path = "api", version = "0.0.9" }
mlb-api = { path = "api", version = "0.0.10" }
once_cell = "1.18.0"
serde = { version = "1.0.164", features = ["derive"] }
serde_json = "1.0.99"
Expand Down
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ With the date picker active, input a date in the form of `YYYY-MM-DD`, or use
the `left`/`right` arrow keys, and press `Enter`. This will display the schedule
for that day. To view games for the current day, input `today`.

To switch the team displayed in the box score:

- `h`: home team
- `a`: away team

### Gameday

Press `2` to activate this tab.
Expand All @@ -136,11 +131,6 @@ toggled on and off using:
- `p`: pitches pane
- `b`: box score pane

To switch the team displayed in the box score:

- `h`: home team
- `a`: away team

### Stats

Press `3` to activate this tab.
Expand All @@ -152,6 +142,11 @@ or `player` using:
- `h`: hitting
- `t`: team
- `l`: player
- `:`: activate date picker

With the date picker active, input a date in the form of `YYYY-MM-DD`, or use
the `left`/`right` arrow keys, and press `Enter`. This will display the selected stats
for the season up to that day. To view stats for the up to the current day, input `today`.

Within each stat group (pitching or hitting) you can toggle the display of
individual stat columns by selecting the stat with `Enter`. This selection pane
Expand All @@ -171,8 +166,14 @@ Press `4` to activate this tab.

- `j`: move down
- `k`: move up
- `:`: activate date picker
- `Enter`: display a teams roster TODO

With the date picker active, input a date in the form of `YYYY-MM-DD`, or use
the `left`/`right` arrow keys, and press `Enter`. This will display the standings
up to that day. To view the standingsup to the current day, input `today`.


### Help

- `?`: display help box
Expand Down
2 changes: 1 addition & 1 deletion api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mlb-api"
version = "0.0.9"
version = "0.0.10"
authors = ["Andrew Schneider <[email protected]>"]
edition = "2021"

Expand Down
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
43 changes: 41 additions & 2 deletions api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ pub struct MLBApi {
/// The available stat groups. These are taken from the "meta" endpoint:
/// https://statsapi.mlb.com/api/v1/statGroups
/// I only need to use Hitting and Pitching for now.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub enum StatGroup {
Hitting,
#[default]
Pitching,
Hitting,
// Fielding,
// Catching,
// Running,
Expand Down Expand Up @@ -81,6 +82,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 +105,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,6 +127,21 @@ 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
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,
}
46 changes: 45 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, 7, 9).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,26 @@ 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, 7, 9).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);
}
}
}
Loading
Loading