use crate::model::prelude::*;
use std::collections::HashMap;
#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
#[non_exhaustive]
pub enum LeagueDataWrap {
Some(LeagueData),
Empty {},
}
impl LeagueDataWrap {
pub fn is_some(&self) -> bool {
matches!(self, LeagueDataWrap::Some(_))
}
pub fn is_empty(&self) -> bool {
matches!(self, LeagueDataWrap::Empty {})
}
pub fn expect(self, msg: &str) -> LeagueData {
match self {
LeagueDataWrap::Some(val) => val,
LeagueDataWrap::Empty {} => panic!("{}", msg),
}
}
pub fn unwrap(self) -> LeagueData {
match self {
LeagueDataWrap::Some(val) => val,
LeagueDataWrap::Empty {} => {
panic!("called `LeagueDataWrap::unwrap()` on an `LeagueDataWrap::Empty` value")
}
}
}
}
impl AsRef<LeagueDataWrap> for LeagueDataWrap {
fn as_ref(&self) -> &Self {
self
}
}
impl Default for LeagueDataWrap {
fn default() -> LeagueDataWrap {
LeagueDataWrap::Empty {}
}
}
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct LeagueData {
#[serde(rename = "gamesplayed")]
pub games_played: u32,
#[serde(rename = "gameswon")]
pub games_won: u32,
pub glicko: f64,
pub rd: Option<f64>,
#[serde(rename = "decaying")]
pub is_decaying: bool,
pub tr: f64,
pub gxe: f64,
pub rank: Rank,
#[serde(rename = "bestrank")]
pub best_rank: Option<Rank>,
pub apm: Option<f64>,
pub pps: Option<f64>,
pub vs: Option<f64>,
pub standing: Option<i32>,
pub standing_local: Option<i32>,
pub percentile: Option<f64>,
pub percentile_rank: Option<Rank>,
pub next_rank: Option<Rank>,
pub prev_rank: Option<Rank>,
pub next_at: Option<i32>,
pub prev_at: Option<i32>,
pub past: HashMap<String, PastUser>,
}
impl LeagueData {
pub fn rank_progress(&self) -> Option<f64> {
if let (Some(standing), Some(prev_at), Some(next_at)) =
(self.standing, self.prev_at, self.next_at)
{
if prev_at < 0 || next_at < 0 {
return None;
}
let current_standing = standing as f64;
let prev_at = prev_at as f64;
let next_at = next_at as f64;
return Some((current_standing - prev_at) / (next_at - prev_at) * 100.);
}
None
}
}
impl AsRef<LeagueData> for LeagueData {
fn as_ref(&self) -> &Self {
self
}
}
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct PastUser {
pub season: String,
pub username: String,
pub country: Option<String>,
pub placement: Option<i32>,
#[serde(rename = "ranked")]
pub is_ranked: bool,
#[serde(rename = "gamesplayed")]
pub games_played: u32,
#[serde(rename = "gameswon")]
pub games_won: u32,
pub glicko: f64,
pub rd: f64,
pub tr: f64,
pub gxe: f64,
pub rank: Rank,
#[serde(rename = "bestrank")]
pub best_rank: Option<Rank>,
pub apm: f64,
pub pps: f64,
pub vs: f64,
}
impl PastUser {
impl_for_country!();
}
impl AsRef<PastUser> for PastUser {
fn as_ref(&self) -> &Self {
self
}
}