Skip to main content

ddapi_rs/scheme/ddnet/
map.rs

1use crate::prelude::{encode, slugify2};
2use crate::scheme::{deserialize_datetime_timestamp, serialize_datetime_timestamp, DDNET_BASE_URL};
3use chrono::NaiveDateTime;
4use serde::{Deserialize, Serialize};
5
6#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
7pub struct Map {
8    pub name: String,
9    pub website: String,
10    pub thumbnail: String,
11    pub web_preview: String,
12    pub r#type: String,
13    pub points: i64,
14    pub difficulty: i64,
15    pub mapper: String,
16    pub release: Option<f64>,
17    pub average_time: f64,
18    pub first_finish: f64,
19    pub last_finish: f64,
20    pub finishes: i64,
21    pub finishers: i64,
22    pub biggest_team: i64,
23    pub width: i64,
24    pub height: i64,
25    pub tiles: Vec<String>,
26    pub team_ranks: Vec<DTeamRank>,
27    pub ranks: Vec<DRank>,
28    pub max_finishes: Vec<MaxFinish>,
29}
30
31impl Map {
32    #[must_use]
33    pub fn url(&self) -> String {
34        format!(
35            "https://{}/maps/{}",
36            DDNET_BASE_URL,
37            encode(&slugify2(&self.name))
38        )
39    }
40
41    #[must_use]
42    pub fn url_with_name(map: &str) -> String {
43        format!("https://{}/maps/{}", DDNET_BASE_URL, encode(&slugify2(map)))
44    }
45
46    #[must_use]
47    pub fn api(map: &str) -> String {
48        format!("https://{}/maps/?json={}", DDNET_BASE_URL, encode(map))
49    }
50}
51
52#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
53pub struct DTeamRank {
54    pub rank: i64,
55    pub players: Vec<String>,
56    pub time: f64,
57    #[serde(
58        serialize_with = "serialize_datetime_timestamp",
59        deserialize_with = "deserialize_datetime_timestamp"
60    )]
61    pub timestamp: NaiveDateTime,
62    pub country: String,
63}
64
65#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
66pub struct DRank {
67    pub rank: i64,
68    pub player: String,
69    pub time: f64,
70    #[serde(
71        serialize_with = "serialize_datetime_timestamp",
72        deserialize_with = "deserialize_datetime_timestamp"
73    )]
74    pub timestamp: NaiveDateTime,
75    pub country: String,
76}
77
78#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
79pub struct MaxFinish {
80    pub rank: i64,
81    pub player: String,
82    pub num: i64,
83    pub time: f64,
84    #[serde(
85        serialize_with = "serialize_datetime_timestamp",
86        deserialize_with = "deserialize_datetime_timestamp"
87    )]
88    pub min_timestamp: NaiveDateTime,
89    #[serde(
90        serialize_with = "serialize_datetime_timestamp",
91        deserialize_with = "deserialize_datetime_timestamp"
92    )]
93    pub max_timestamp: NaiveDateTime,
94}