use serde::Deserialize;
use std::fmt::Formatter;
use crate::{client::SteamClient, errors::SteamError};
const ENDPOINT_OWNED_GAMES: &str =
"https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001";
#[derive(Debug, Deserialize)]
struct OwnedGamesResponse {
response: Option<Library>,
}
#[derive(Debug, Default, Deserialize)]
pub struct Library {
pub game_count: u32,
pub games: Vec<Game>,
}
impl From<OwnedGamesResponse> for Library {
fn from(value: OwnedGamesResponse) -> Self {
let v = value.response.unwrap_or_default();
Self {
game_count: v.game_count,
games: v.games,
}
}
}
#[derive(Debug, Deserialize)]
pub struct Game {
#[serde(rename(deserialize = "appid"))]
pub app_id: u64,
pub name: String,
pub playtime_forever: u64,
pub playtime_windows_forever: u64,
pub playtime_mac_forever: u64,
pub playtime_linux_forever: u64,
}
impl PartialEq for Game {
fn eq(&self, other: &Self) -> bool {
self.app_id == other.app_id
}
}
impl std::fmt::Display for Game {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Game: {}, total time played: {}",
self.name, self.playtime_forever
)
}
}
impl SteamClient {
pub fn get_library(&self, steam_id: &str) -> Result<Library, SteamError> {
let response = self.get_request(
ENDPOINT_OWNED_GAMES,
vec![
("steamid", steam_id),
("include_appInfo", "true"),
("include_played_free_games", "true"),
],
)?;
let games = self
.parse_response::<OwnedGamesResponse, Library>(response)
.unwrap();
Ok(games)
}
}