ddapi_rs/scheme/ddnet/
player.rs

1use crate::prelude::{encode, slugify2};
2use crate::scheme::{deserialize_datetime_timestamp, serialize_datetime_timestamp, DDNET_BASE_URL};
3use chrono::NaiveDateTime;
4use serde_derive::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
8pub struct Player {
9    pub player: String,
10    pub points: Points,
11    pub team_rank: Option<Rank>,
12    pub rank: Option<Rank>,
13    pub points_last_year: Option<Rank>,
14    pub points_last_month: Option<Rank>,
15    pub points_last_week: Option<Rank>,
16    pub favorite_server: FavoriteServer,
17    pub first_finish: FirstFinish,
18    pub last_finishes: Vec<LastFinish>,
19    #[serde(default)]
20    pub favorite_partners: Vec<FavoritePartner>,
21    pub types: Types,
22    pub activity: Vec<Activity>,
23    pub hours_played_past_365_days: i64,
24}
25
26impl Player {
27    pub fn url(&self) -> String {
28        format!(
29            "https://{}/players/{}",
30            DDNET_BASE_URL,
31            encode(&slugify2(&self.player))
32        )
33    }
34
35    pub fn url_with_name(player: &str) -> String {
36        format!(
37            "https://{}/players/{}",
38            DDNET_BASE_URL,
39            encode(&slugify2(player))
40        )
41    }
42
43    pub fn api(player: &str) -> String {
44        format!(
45            "https://{}/players/?json2={}",
46            DDNET_BASE_URL,
47            encode(player)
48        )
49    }
50}
51
52#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
53pub struct Points {
54    pub total: u64,
55    pub points: Option<u64>,
56    pub rank: Option<u64>,
57}
58
59#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
60pub struct Rank {
61    pub points: Option<u64>,
62    pub rank: Option<u64>,
63}
64
65#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
66pub struct FavoriteServer {
67    pub server: String,
68}
69
70#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
71pub struct FirstFinish {
72    #[serde(
73        serialize_with = "serialize_datetime_timestamp",
74        deserialize_with = "deserialize_datetime_timestamp"
75    )]
76    pub timestamp: NaiveDateTime,
77    pub map: String,
78    pub time: f64,
79}
80
81#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
82pub struct LastFinish {
83    #[serde(
84        serialize_with = "serialize_datetime_timestamp",
85        deserialize_with = "deserialize_datetime_timestamp"
86    )]
87    pub timestamp: NaiveDateTime,
88    pub map: String,
89    pub time: f64,
90    pub country: String,
91    #[serde(rename = "type")]
92    pub type_map: Option<String>,
93}
94
95#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
96pub struct FavoritePartner {
97    pub name: String,
98    pub finishes: i64,
99}
100
101#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
102#[serde(rename_all = "PascalCase")]
103pub struct Types {
104    pub novice: Type,
105    pub moderate: Type,
106    pub brutal: Type,
107    pub insane: Type,
108    pub dummy: Type,
109    #[serde(rename = "DDmaX.Easy")]
110    pub ddmax_easy: Type,
111    #[serde(rename = "DDmaX.Next")]
112    pub ddmax_next: Type,
113    #[serde(rename = "DDmaX.Pro")]
114    pub ddmax_pro: Type,
115    #[serde(rename = "DDmaX.Nut")]
116    pub ddmax_nut: Type,
117    pub oldschool: Type,
118    pub solo: Type,
119    pub race: Type,
120    pub fun: Type,
121}
122
123#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
124pub struct Type {
125    pub points: Points,
126    pub team_rank: Option<Rank>,
127    pub rank: Option<Rank>,
128    pub maps: HashMap<String, DDMap>,
129}
130
131#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
132pub struct DDMap {
133    pub points: i64,
134    pub total_finishes: i64,
135    pub finishes: i64,
136    pub team_rank: Option<i64>,
137    pub rank: Option<i64>,
138    pub time: Option<f64>,
139    pub first_finish: Option<f64>,
140}
141
142#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
143pub struct Activity {
144    pub date: String,
145    pub hours_played: i64,
146}