Skip to main content

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::{NaiveDate, NaiveDateTime};
4use serde::{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    #[serde(default)]
22    pub deleted_ranks: Vec<DeletedRank>,
23    pub types: Types,
24    pub activity: Vec<Activity>,
25    pub hours_played_past_365_days: i64,
26}
27
28impl Player {
29    #[must_use]
30    pub fn url(&self) -> String {
31        format!(
32            "https://{}/players/{}",
33            DDNET_BASE_URL,
34            encode(&slugify2(&self.player))
35        )
36    }
37
38    #[must_use]
39    pub fn url_with_name(player: &str) -> String {
40        format!(
41            "https://{}/players/{}",
42            DDNET_BASE_URL,
43            encode(&slugify2(player))
44        )
45    }
46
47    #[must_use]
48    pub fn api(player: &str) -> String {
49        format!(
50            "https://{}/players/?json2={}",
51            DDNET_BASE_URL,
52            encode(player)
53        )
54    }
55}
56
57#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
58pub struct Points {
59    pub total: u64,
60    pub points: Option<u64>,
61    pub rank: Option<u64>,
62}
63
64#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
65pub struct Rank {
66    pub points: Option<u64>,
67    pub rank: Option<u64>,
68}
69
70#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
71pub struct FavoriteServer {
72    pub server: String,
73}
74
75#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
76pub struct FirstFinish {
77    #[serde(
78        serialize_with = "serialize_datetime_timestamp",
79        deserialize_with = "deserialize_datetime_timestamp"
80    )]
81    pub timestamp: NaiveDateTime,
82    pub map: String,
83    pub time: f64,
84}
85
86#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
87pub struct LastFinish {
88    #[serde(
89        serialize_with = "serialize_datetime_timestamp",
90        deserialize_with = "deserialize_datetime_timestamp"
91    )]
92    pub timestamp: NaiveDateTime,
93    pub map: String,
94    pub time: f64,
95    pub country: String,
96    #[serde(rename = "type")]
97    pub type_map: Option<String>,
98}
99
100#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
101pub struct FavoritePartner {
102    pub name: String,
103    pub finishes: i64,
104}
105
106#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
107pub struct DeletedRank {
108    pub map: String,
109    pub time: f64,
110    #[serde(
111        serialize_with = "serialize_datetime_timestamp",
112        deserialize_with = "deserialize_datetime_timestamp"
113    )]
114    pub timestamp: NaiveDateTime,
115    pub country: String,
116    #[serde(
117        serialize_with = "serialize_datetime_timestamp",
118        deserialize_with = "deserialize_datetime_timestamp"
119    )]
120    pub deleted_at: NaiveDateTime,
121}
122
123#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
124#[serde(rename_all = "PascalCase")]
125pub struct Types {
126    pub novice: Type,
127    pub moderate: Type,
128    pub brutal: Type,
129    pub insane: Type,
130    pub dummy: Type,
131    #[serde(rename = "DDmaX.Easy")]
132    pub ddmax_easy: Type,
133    #[serde(rename = "DDmaX.Next")]
134    pub ddmax_next: Type,
135    #[serde(rename = "DDmaX.Pro")]
136    pub ddmax_pro: Type,
137    #[serde(rename = "DDmaX.Nut")]
138    pub ddmax_nut: Type,
139    pub oldschool: Type,
140    pub solo: Type,
141    pub race: Type,
142    pub fun: Type,
143    pub event: Type,
144}
145
146#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
147pub struct Type {
148    pub points: Points,
149    pub team_rank: Option<Rank>,
150    pub rank: Option<Rank>,
151    pub maps: HashMap<String, DDMap>,
152}
153
154#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
155pub struct DDMap {
156    pub points: i64,
157    pub total_finishes: i64,
158    pub finishes: i64,
159    pub team_rank: Option<i64>,
160    pub rank: Option<i64>,
161    pub time: Option<f64>,
162    pub first_finish: Option<f64>,
163}
164
165#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
166pub struct Activity {
167    pub date: NaiveDate,
168    pub hours_played: i64,
169}