diff --git a/src/config.rs b/src/config.rs index c95a9d1..00de01a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,5 @@ -use crate::{ApiResponse, SteamIdResponse}; +use crate::responses::api_response::ApiResponse; +use crate::responses::steam_id_response::SteamIdResponse; use serde::Deserialize; use serde::Serialize; use std::fs::File; diff --git a/src/game.rs b/src/game.rs new file mode 100644 index 0000000..4149804 --- /dev/null +++ b/src/game.rs @@ -0,0 +1,12 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct Game { + pub appid: u32, + pub name: Option, + pub playtime_2weeks: Option, + pub playtime_forever: Option, + pub img_icon_url: Option, + pub img_logo_url: Option, + pub has_community_visible_stats: Option, +} diff --git a/src/gamelist.rs b/src/gamelist.rs new file mode 100644 index 0000000..57a6ed1 --- /dev/null +++ b/src/gamelist.rs @@ -0,0 +1,51 @@ +use crate::config::Config; +use crate::game::Game; +use crate::responses::api_response::ApiResponse; +use rand::seq::IteratorRandom; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct GameList { + pub game_count: i32, + pub games: Vec, +} + +impl GameList { + pub fn new() -> GameList { + GameList { + game_count: 0, + games: Vec::new(), + } + } + + pub fn update(&mut self) { + let config = Config::new(); + match reqwest::blocking::get(format!("https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key={}&steamid={}&format=json&include_appinfo=true", + config.steam_api_key, + config.get_steam_id(Some(&config.user.username)) + )) { + Ok(res) => { + match res.text() { + Ok(text) => { + let api_response: ApiResponse= serde_json::from_str(&text).unwrap(); + self.games = api_response.response.games; + }, + Err(_) => println!("Error getting list") , + } + } + Err(_) => println!("Error getting list") , + } + } + + pub fn pick(&self) -> Option { + self.games.iter().choose(&mut rand::rng()).cloned() + } + + pub fn pick_unplayed(&self) -> Option { + self.games + .iter() + .filter(|&game| game.playtime_forever == Some(0)) + .choose(&mut rand::rng()) + .cloned() + } +} diff --git a/src/main.rs b/src/main.rs index ab74668..c7cac0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,77 +1,10 @@ mod config; +mod game; +mod gamelist; +mod responses; -use crate::config::Config; +use crate::gamelist::GameList; use clap::Parser; -use rand::seq::IteratorRandom; -use serde::{Deserialize, Serialize}; - -#[derive(Serialize, Deserialize)] -struct ApiResponse { - response: T, -} - -#[derive(Serialize, Deserialize)] -struct SteamIdResponse { - steamid: String, - success: u8, -} - -#[derive(Serialize, Deserialize, Clone, Debug)] -struct Game { - appid: u32, - name: Option, - playtime_2weeks: Option, - playtime_forever: Option, - img_icon_url: Option, - img_logo_url: Option, - has_community_visible_stats: Option, -} - -#[derive(Serialize, Deserialize)] -struct GameList { - game_count: i32, - games: Vec, -} - -impl GameList { - fn new() -> GameList { - GameList { - game_count: 0, - games: Vec::new(), - } - } - - fn update(&mut self) { - let config = Config::new(); - match reqwest::blocking::get(format!("https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key={}&steamid={}&format=json&include_appinfo=true", - config.steam_api_key, - config.get_steam_id(Some(&config.user.username)) - )) { - Ok(res) => { - match res.text() { - Ok(text) => { - let api_response: ApiResponse= serde_json::from_str(&text).unwrap(); - self.games = api_response.response.games; - }, - Err(_) => println!("Error getting list") , - } - } - Err(_) => println!("Error getting list") , - } - } - - fn pick(&self) -> Option { - self.games.iter().choose(&mut rand::rng()).cloned() - } - - fn pick_unplayed(&self) -> Option { - self.games - .iter() - .filter(|&game| game.playtime_forever == Some(0)) - .choose(&mut rand::rng()) - .cloned() - } -} #[derive(Parser, Debug)] struct Args { diff --git a/src/responses/api_response.rs b/src/responses/api_response.rs new file mode 100644 index 0000000..f5aad5b --- /dev/null +++ b/src/responses/api_response.rs @@ -0,0 +1,6 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct ApiResponse { + pub response: T, +} diff --git a/src/responses/mod.rs b/src/responses/mod.rs new file mode 100644 index 0000000..aef7ee3 --- /dev/null +++ b/src/responses/mod.rs @@ -0,0 +1,2 @@ +pub mod api_response; +pub mod steam_id_response; diff --git a/src/responses/steam_id_response.rs b/src/responses/steam_id_response.rs new file mode 100644 index 0000000..7966272 --- /dev/null +++ b/src/responses/steam_id_response.rs @@ -0,0 +1,7 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct SteamIdResponse { + pub steamid: String, + success: u8, +}