nadeo_api_rs/
core.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::{
    collections::{HashMap, HashSet, VecDeque},
    error::Error,
    sync::OnceLock,
};

use chrono::{DateTime, NaiveDateTime, Utc};
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};

use crate::{auth::NadeoClient, client::NadeoApiClient};

lazy_static! {
    static ref ZONES_INFO: OnceLock<ZoneTree> = OnceLock::new();
}

/// API calls for the Core API
pub trait CoreApiClient: NadeoApiClient {
    /// Get a list of zones
    ///
    /// <https://webservices.openplanet.dev/core/meta/zones>
    async fn get_zones(&self) -> Result<Vec<Zone>, Box<dyn Error>> {
        let j = self.run_core_get("zones").await?;
        Ok(serde_json::from_value(j)?)
    }

    /// Get the zone tree -- cached!
    async fn get_zone_tree(&self) -> Result<&ZoneTree, Box<dyn Error>> {
        if ZONES_INFO.get().is_none() {
            let zones = self.get_zones().await?;
            let zt = ZoneTree::new(zones);
            ZONES_INFO.set(zt).unwrap();
        }
        Ok(ZONES_INFO.get().unwrap())
    }

    /// Get players' zone details
    ///
    /// <https://webservices.openplanet.dev/core/accounts/zones>
    ///
    /// https://prod.trackmania.core.nadeo.online/accounts/zones/?accountIdList={accountIdList}
    async fn get_user_zones<T: Into<String> + Clone>(
        &self,
        player_ids: &[T],
    ) -> Result<HashMap<String, PlayerZone>, Box<dyn Error + Send + Sync>> {
        let mut zones = HashMap::new();
        let player_ids = player_ids
            .iter()
            .cloned()
            .map(Into::into)
            .collect::<Vec<String>>()
            .join(",");
        let j = self
            .run_core_get(&format!("accounts/zones/?accountIdList={}", player_ids))
            .await?;
        let j = j.as_array().ok_or("Response was not an array")?;
        for data in j {
            let pz = serde_json::from_value::<PlayerZone>(data.clone())?;
            zones.insert(pz.account_id.clone(), pz);
        }
        Ok(zones)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlayerZone {
    #[serde(rename = "accountId")]
    pub account_id: String,
    #[serde(rename = "zoneId")]
    pub zone_id: String,
    pub timestamp: String,
}

impl CoreApiClient for NadeoClient {}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(non_camel_case_types, non_snake_case)]
pub struct Zone {
    pub icon: String,
    pub name: String,
    pub parentId: Option<String>,
    pub timestamp: String,
    pub zoneId: String,
    pub depth: Option<u32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZoneTree {
    pub world_id: String,
    pub zones: HashMap<String, Zone>,
    pub children: HashMap<String, Vec<String>>,
}

impl ZoneTree {
    pub fn new(zones: Vec<Zone>) -> Self {
        let mut z = HashMap::new();
        let mut c = HashMap::new();
        let mut world_id = String::new();
        for mut zone in zones.into_iter() {
            if zone.name == "World" {
                world_id = zone.zoneId.clone();
                zone.depth = Some(0);
            }
            if let Some(parent) = zone.parentId.clone() {
                c.entry(parent)
                    .or_insert_with(Vec::new)
                    .push(zone.zoneId.clone());
            }
            z.insert(zone.zoneId.clone(), zone);
        }
        if world_id.is_empty() {
            panic!("No World zone found");
        }
        let mut edge = vec![world_id.clone()];
        let mut visited = HashSet::new();
        while !edge.is_empty() {
            let mut new_edge = Vec::new();
            for zone_id in edge {
                if let Some(children) = c.get(&zone_id) {
                    for child in children {
                        if visited.contains(child) {
                            continue;
                        }
                        visited.insert(child);
                        new_edge.push(child.clone());
                        let child_depth = z
                            .get(&zone_id)
                            .expect(&format!("has key: {} (child: {})", zone_id, child))
                            .depth
                            .expect(&format!("has key depth: {} (child: {})", zone_id, child))
                            + 1;
                        if let Some(node) = z.get_mut(child) {
                            node.depth = Some(child_depth);
                        } else {
                            panic!("Child zone not found: {}", child);
                        }
                    }
                }
            }
            edge = new_edge;
        }

        Self {
            world_id,
            zones: z,
            children: c,
        }
    }

    pub fn get_zone(&self, zone_id: &str) -> Option<&Zone> {
        self.zones.get(zone_id)
    }

    pub fn get_children(&self, zone_id: &str) -> Option<&Vec<String>> {
        self.children.get(zone_id)
    }

    pub fn get_country_or_higher(&self, zone_id: &str) -> Option<&Zone> {
        let mut id = zone_id;
        while let Some(zone) = self.get_zone(id) {
            if zone.depth.unwrap() <= 2 {
                return Some(zone);
            }
            if let Some(parent) = &zone.parentId {
                id = parent;
            } else {
                break;
            }
        }
        None
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        auth::{NadeoClient, UserAgentDetails},
        core::CoreApiClient,
        test_helpers::get_test_creds,
        user_agent_auto,
    };

    #[ignore]
    #[tokio::test]
    async fn test_get_zones() {
        let creds = get_test_creds();
        let email = std::env::var("NADEO_TEST_UA_EMAIL").unwrap();
        let client = NadeoClient::create(creds, user_agent_auto!(&email), 10)
            .await
            .unwrap();
        // let zones = client.get_zones().await.unwrap();

        let _t1 = std::time::Instant::now();
        let _zt = client.get_zone_tree().await.unwrap();
        let _t2 = std::time::Instant::now();
        let _zt2 = client.get_zone_tree().await.unwrap();
        let _t3 = std::time::Instant::now();
        assert!(_zt2.zones.len() > 1000);
        println!("World: {:?}", _zt2.get_zone(&_zt2.world_id).unwrap());
        println!("Children: {:?}", _zt2.get_children(&_zt2.world_id).unwrap());

        let user_zones = client
            .get_user_zones(&[
                "5b4d42f4-c2de-407d-b367-cbff3fe817bc",
                "0a2d1bc0-4aaa-4374-b2db-3d561bdab1c9",
            ])
            .await
            .unwrap();
        println!("{:?}", user_zones);
        assert_eq!(user_zones.len(), 2);
    }
}