mirror of
https://github.com/Death916/deathclock.git
synced 2026-04-10 03:04:40 -07:00
move sports to own file
This commit is contained in:
parent
464f109bc0
commit
261943b196
2 changed files with 105 additions and 104 deletions
120
rust/src/main.rs
120
rust/src/main.rs
|
|
@ -1,19 +1,14 @@
|
||||||
|
mod sports;
|
||||||
use iced::Center;
|
use iced::Center;
|
||||||
use iced::Element;
|
use iced::Element;
|
||||||
use iced::widget::{column, image, pane_grid, row, text};
|
use iced::widget::{column, image, pane_grid, row, text};
|
||||||
|
use sports::Game;
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::run(State::update, State::view)
|
iced::run(State::update, State::view)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
enum Sport {
|
enum PaneType {
|
||||||
NBA,
|
|
||||||
NFL,
|
|
||||||
MLB,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
enum Pane {
|
|
||||||
Main,
|
Main,
|
||||||
MlbPane,
|
MlbPane,
|
||||||
NflPane,
|
NflPane,
|
||||||
|
|
@ -35,7 +30,7 @@ struct State {
|
||||||
weather: Vec<u8>,
|
weather: Vec<u8>,
|
||||||
location: String,
|
location: String,
|
||||||
scores: Vec<Game>,
|
scores: Vec<Game>,
|
||||||
panes: pane_grid::State<Pane>,
|
panes: pane_grid::State<PaneType>,
|
||||||
}
|
}
|
||||||
impl State {
|
impl State {
|
||||||
fn update(&mut self, message: Message) {
|
fn update(&mut self, message: Message) {
|
||||||
|
|
@ -51,9 +46,9 @@ impl State {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(state: &State) -> Element<'_, Message> {
|
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 {
|
let content: Element<'_, Message> = match pane_state {
|
||||||
Pane::NbaPane => {
|
PaneType::NbaPane => {
|
||||||
let games = &state.scores;
|
let games = &state.scores;
|
||||||
column![
|
column![
|
||||||
text("NBA").size(50),
|
text("NBA").size(50),
|
||||||
|
|
@ -63,10 +58,10 @@ impl State {
|
||||||
.padding(5)
|
.padding(5)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
Pane::NflPane => text("NFL").into(),
|
PaneType::NflPane => text("NFL").into(),
|
||||||
Pane::MlbPane => text("MLB").into(),
|
PaneType::MlbPane => text("MLB").into(),
|
||||||
Pane::Main => text("Main").into(),
|
PaneType::Main => text("Main").into(),
|
||||||
Pane::Weather => {
|
PaneType::Weather => {
|
||||||
let weather_img = image::Handle::from_bytes(state.weather.clone());
|
let weather_img = image::Handle::from_bytes(state.weather.clone());
|
||||||
column![
|
column![
|
||||||
text("Weather").size(50),
|
text("Weather").size(50),
|
||||||
|
|
@ -102,103 +97,20 @@ impl Default for State {
|
||||||
weather: text,
|
weather: text,
|
||||||
location: "Sacramento".to_string(),
|
location: "Sacramento".to_string(),
|
||||||
scores: {
|
scores: {
|
||||||
sports_updates::update_nba();
|
sports::update_nba();
|
||||||
sports()
|
sports::sports()
|
||||||
},
|
},
|
||||||
panes: {
|
panes: {
|
||||||
let (mut panes, nba) = pane_grid::State::new(Pane::NbaPane);
|
let (mut panes, nba) = pane_grid::State::new(PaneType::NbaPane);
|
||||||
let (weather, _) = panes
|
let (weather, _) = panes
|
||||||
.split(pane_grid::Axis::Vertical, nba, Pane::Weather)
|
.split(pane_grid::Axis::Vertical, nba, PaneType::Weather)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let (nfl, _) = panes
|
let (nfl, _) = panes
|
||||||
.split(pane_grid::Axis::Vertical, weather, Pane::NflPane)
|
.split(pane_grid::Axis::Vertical, weather, PaneType::NflPane)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
panes.split(pane_grid::Axis::Horizontal, nfl, Pane::MlbPane);
|
panes.split(pane_grid::Axis::Horizontal, nfl, PaneType::MlbPane);
|
||||||
panes
|
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<Game> {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
89
rust/src/sports.rs
Normal file
89
rust/src/sports.rs
Normal file
|
|
@ -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<Game> {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue