code cleanup

This commit is contained in:
Edward Tirado Jr 2025-03-16 23:42:12 -05:00
parent 5565fa39c1
commit b4f75b149a
7 changed files with 84 additions and 72 deletions

View file

@ -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;

12
src/game.rs Normal file
View file

@ -0,0 +1,12 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Game {
pub appid: u32,
pub name: Option<String>,
pub playtime_2weeks: Option<u32>,
pub playtime_forever: Option<u32>,
pub img_icon_url: Option<String>,
pub img_logo_url: Option<String>,
pub has_community_visible_stats: Option<bool>,
}

51
src/gamelist.rs Normal file
View file

@ -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<Game>,
}
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<GameList>= 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<Game> {
self.games.iter().choose(&mut rand::rng()).cloned()
}
pub fn pick_unplayed(&self) -> Option<Game> {
self.games
.iter()
.filter(|&game| game.playtime_forever == Some(0))
.choose(&mut rand::rng())
.cloned()
}
}

View file

@ -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<T> {
response: T,
}
#[derive(Serialize, Deserialize)]
struct SteamIdResponse {
steamid: String,
success: u8,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
struct Game {
appid: u32,
name: Option<String>,
playtime_2weeks: Option<u32>,
playtime_forever: Option<u32>,
img_icon_url: Option<String>,
img_logo_url: Option<String>,
has_community_visible_stats: Option<bool>,
}
#[derive(Serialize, Deserialize)]
struct GameList {
game_count: i32,
games: Vec<Game>,
}
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<GameList>= 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<Game> {
self.games.iter().choose(&mut rand::rng()).cloned()
}
fn pick_unplayed(&self) -> Option<Game> {
self.games
.iter()
.filter(|&game| game.playtime_forever == Some(0))
.choose(&mut rand::rng())
.cloned()
}
}
#[derive(Parser, Debug)]
struct Args {

View file

@ -0,0 +1,6 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct ApiResponse<T> {
pub response: T,
}

2
src/responses/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod api_response;
pub mod steam_id_response;

View file

@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct SteamIdResponse {
pub steamid: String,
success: u8,
}