From 261943b196dd84ebd3fa2da38df2dc10fdbcfdc9 Mon Sep 17 00:00:00 2001 From: death916 Date: Thu, 12 Mar 2026 01:43:32 -0700 Subject: [PATCH] move sports to own file --- rust/src/main.rs | 120 ++++++--------------------------------------- rust/src/sports.rs | 89 +++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 104 deletions(-) create mode 100644 rust/src/sports.rs diff --git a/rust/src/main.rs b/rust/src/main.rs index 9dd2cb5..4e35992 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,19 +1,14 @@ +mod sports; use iced::Center; use iced::Element; use iced::widget::{column, image, pane_grid, row, text}; +use sports::Game; pub fn main() -> iced::Result { iced::run(State::update, State::view) } #[derive(Debug, Clone)] -enum Sport { - NBA, - NFL, - MLB, -} - -#[derive(Debug, Clone)] -enum Pane { +enum PaneType { Main, MlbPane, NflPane, @@ -35,7 +30,7 @@ struct State { weather: Vec, location: String, scores: Vec, - panes: pane_grid::State, + panes: pane_grid::State, } impl State { fn update(&mut self, message: Message) { @@ -51,9 +46,9 @@ impl State { } fn view(state: &State) -> Element<'_, Message> { - pane_grid(&state.panes, |_pane, pane_state, _is_maximized| { + pane_grid(&state.panes, |_panes, pane_state, _is_maximized| { let content: Element<'_, Message> = match pane_state { - Pane::NbaPane => { + PaneType::NbaPane => { let games = &state.scores; column![ text("NBA").size(50), @@ -63,10 +58,10 @@ impl State { .padding(5) .into() } - Pane::NflPane => text("NFL").into(), - Pane::MlbPane => text("MLB").into(), - Pane::Main => text("Main").into(), - Pane::Weather => { + PaneType::NflPane => text("NFL").into(), + PaneType::MlbPane => text("MLB").into(), + PaneType::Main => text("Main").into(), + PaneType::Weather => { let weather_img = image::Handle::from_bytes(state.weather.clone()); column![ text("Weather").size(50), @@ -102,103 +97,20 @@ impl Default for State { weather: text, location: "Sacramento".to_string(), scores: { - sports_updates::update_nba(); - sports() + sports::update_nba(); + sports::sports() }, panes: { - let (mut panes, nba) = pane_grid::State::new(Pane::NbaPane); + let (mut panes, nba) = pane_grid::State::new(PaneType::NbaPane); let (weather, _) = panes - .split(pane_grid::Axis::Vertical, nba, Pane::Weather) + .split(pane_grid::Axis::Vertical, nba, PaneType::Weather) .unwrap(); let (nfl, _) = panes - .split(pane_grid::Axis::Vertical, weather, Pane::NflPane) + .split(pane_grid::Axis::Vertical, weather, PaneType::NflPane) .unwrap(); - panes.split(pane_grid::Axis::Horizontal, nfl, Pane::MlbPane); + panes.split(pane_grid::Axis::Horizontal, nfl, PaneType::MlbPane); panes }, } } } - -#[derive(Debug)] -struct Game { - sport: Sport, - team1: String, - team2: String, - score1: String, - score2: String, - period: u8, -} - -impl Game { - fn new(sport: Sport, team1: &str, team2: &str, score1: &str, score2: &str, period: u8) -> Self { - Game { - sport, - team1: team1.to_string(), - team2: team2.to_string(), - score1: score1.to_string(), - score2: score2.to_string(), - period, - } - } - - fn update(&mut self, score1: &str, score2: &str) { - self.score1 = score1.to_string(); - self.score2 = score2.to_string(); - } -} - -fn sports() -> Vec { - println!("Sports!"); - - vec![ - Game::new(Sport::NBA, "Lakers", "Warriors", "100", "95", 3), - Game::new(Sport::NBA, "Celtics", "Nets", "110", "105", 2), - Game::new(Sport::MLB, "Red Sox", "Yankees", "100", "95", 1), - ] -} - -mod sports_updates { - use super::Game; - use super::Sport; - - pub fn update_nba() { - let nba_games = ureq::get( - "https://cdn.nba.com/static/json/liveData/scoreboard/todaysScoreboard_00.json", - ) - .header("User-Agent", "deathclock-app/1.0") - .call() - .unwrap() - .into_body() - .read_to_vec() - .unwrap(); - - let json: serde_json::Value = serde_json::from_slice(&nba_games).unwrap(); - let games = json["scoreboard"]["games"].as_array().unwrap(); - - for game in games { - let game_id = game["gameId"].as_str().unwrap(); - let home_team = game["homeTeam"]["teamName"].as_str().unwrap(); - let away_team = game["awayTeam"]["teamName"].as_str().unwrap(); - let home_score = game["homeTeam"]["score"].as_u64().unwrap().to_string(); - let away_score = game["awayTeam"]["score"].as_u64().unwrap().to_string(); - let period = game["period"].as_u64().unwrap() as u8; - - let mut game = Game::new( - Sport::NBA, - home_team, - away_team, - &home_score, - &away_score, - period, - ); - game.update(&home_score, &away_score); - println!("Game ID: {}", game_id); - println!("Home Team: {}", home_team); - println!("Away Team: {}", away_team); - println!("Home Score: {}", home_score); - println!("Away Score: {}", away_score); - println!("Period: {}", period); - } - } -} diff --git a/rust/src/sports.rs b/rust/src/sports.rs new file mode 100644 index 0000000..294f1c6 --- /dev/null +++ b/rust/src/sports.rs @@ -0,0 +1,89 @@ +#[derive(Debug, Clone)] +pub enum Sport { + NBA, + NFL, + MLB, +} + +#[derive(Debug)] +pub struct Game { + pub sport: Sport, + pub team1: String, + pub team2: String, + pub score1: String, + pub score2: String, + pub period: u8, +} + +impl Game { + pub fn new( + sport: Sport, + team1: &str, + team2: &str, + score1: &str, + score2: &str, + period: u8, + ) -> Self { + Game { + sport, + team1: team1.to_string(), + team2: team2.to_string(), + score1: score1.to_string(), + score2: score2.to_string(), + period, + } + } + + pub fn update(&mut self, score1: &str, score2: &str, period: u8) { + self.score1 = score1.to_string(); + self.score2 = score2.to_string(); + self.period = period; + } +} + +pub fn sports() -> Vec { + vec![ + Game::new(Sport::NBA, "Lakers", "Warriors", "100", "95", 3), + Game::new(Sport::NBA, "Celtics", "Nets", "110", "105", 2), + Game::new(Sport::MLB, "Red Sox", "Yankees", "100", "95", 1), + ] +} + +pub fn update_nba() { + let nba_games = + ureq::get("https://cdn.nba.com/static/json/liveData/scoreboard/todaysScoreboard_00.json") + .header("User-Agent", "deathclock-app/1.0") + .call() + .unwrap() + .into_body() + .read_to_vec() + .unwrap(); + + let json: serde_json::Value = serde_json::from_slice(&nba_games).unwrap(); + let games = json["scoreboard"]["games"].as_array().unwrap(); + + for game in games { + let game_id = game["gameId"].as_str().unwrap(); + let home_team = game["homeTeam"]["teamName"].as_str().unwrap(); + let away_team = game["awayTeam"]["teamName"].as_str().unwrap(); + let home_score = game["homeTeam"]["score"].as_u64().unwrap().to_string(); + let away_score = game["awayTeam"]["score"].as_u64().unwrap().to_string(); + let period = game["period"].as_u64().unwrap() as u8; + + let mut game = Game::new( + Sport::NBA, + home_team, + away_team, + &home_score, + &away_score, + period, + ); + game.update(&home_score, &away_score, period); + println!("Game ID: {}", game_id); + println!("Home Team: {}", home_team); + println!("Away Team: {}", away_team); + println!("Home Score: {}", home_score); + println!("Away Score: {}", away_score); + println!("Period: {}", period); + } +}