gw2api/v1/
maps.rs

1use crate::client::Client;
2use crate::error::ApiError;
3
4use std::collections::HashMap;
5
6const ENDPOINT_URL: &str = "/v1/maps";
7
8/// Struct contains a map of map objects.
9#[derive(Debug, Deserialize, PartialEq)]
10pub struct Maps {
11    /// HashMap of map objects.
12    maps: HashMap<u32, Map>,
13}
14
15/// Different types of maps.
16#[derive(Debug, Deserialize, PartialEq, Hash)]
17pub enum MapType {
18    Center,
19    Instance,
20    Public,
21    GreenHome,
22    BlueHome,
23    RedHome,
24    Tutorial,
25    Pvp,
26    JumpPuzzle,
27    EdgeOfTheMists,
28    Unknown,
29}
30
31/// Struct containing information about a maps in the game, including information about floor and
32/// translation data on how to translate between world coordinates and map coordinates.
33#[derive(Debug, Deserialize, PartialEq, Hash)]
34pub struct Map {
35    /// Name of the map.
36    #[serde(rename = "map_name")]
37    name: String,
38    /// Minimum level (height) of the map.
39    min_level: i32,
40    /// Maximum level of the map.
41    max_level: i32,
42    /// Default floor for the map.
43    default_floor: i32,
44    /// List of available floors.
45    #[serde(default)]
46    floors: Vec<i32>,
47    /// The type of map.
48    #[serde(default, rename = "type")]
49    map_type: Option<MapType>,
50    /// id of the region this map belongs to.
51    region_id: Option<u32>,
52    /// Name of the region this map belongs to.
53    region_name: Option<String>,
54    /// id of the continent this map belongs to.
55    continent_id: Option<u32>,
56    /// Name of the continent this map belongs to.
57    continent_name: Option<String>,
58    /// Dimensions of the map, given as the coordinates of the lower-left (SW) and upper-right (NE)
59    /// corners.
60    map_rect: Vec<(i32, i32)>,
61    /// Dimensions of the map within the continent coordinate system,
62    /// given as the coordinates of the lower-left (SW) and upper-right (NE) corners.
63    continent_rect: Vec<(i32, i32)>,
64}
65
66impl Maps {
67    /// Retrieve a map by its id.
68    pub fn get_id(client: &Client, id: String) -> Result<Maps, ApiError> {
69        let url = format!("{}?map_id={}", ENDPOINT_URL, id);
70        client.request(&url)
71    }
72
73    /// Retrieve a map by its id.
74    pub fn get_all(client: &Client) -> Result<Maps, ApiError> {
75        client.request(ENDPOINT_URL)
76    }
77
78    /// Returns the map of matched map objects.
79    pub fn maps(&self) -> &HashMap<u32, Map> {
80        &self.maps
81    }
82}
83
84impl Map {
85    /// Returns the name of the map.
86    pub fn name(&self) -> &str {
87        &self.name
88    }
89
90    /// Returns the minimum level of the map.
91    pub fn min_level(&self) -> i32 {
92        self.min_level
93    }
94
95    /// Returns the maximum level of the map.
96    pub fn max_level(&self) -> i32 {
97        self.max_level
98    }
99
100    /// Returns the default level of the map.
101    pub fn default_floor(&self) -> i32 {
102        self.default_floor
103    }
104
105    /// Returns the list of available floors.
106    pub fn floors(&self) -> &Vec<i32> {
107        &self.floors
108    }
109
110    /// Returns the type of the map.
111    pub fn map_type(&self) -> Option<&MapType> {
112        self.map_type.as_ref()
113    }
114
115    /// Returns the id of the region this map belongs to.
116    pub fn region_id(&self) -> Option<u32> {
117        self.region_id
118    }
119
120    /// Returns the name of the region this map belongs to.
121    pub fn region_name(&self) -> Option<&String> {
122        self.region_name.as_ref()
123    }
124
125    /// Returns the id of the continent this map belongs to.
126    pub fn continent_id(&self) -> Option<u32> {
127        self.continent_id
128    }
129
130    /// Returns the name of the continent this map belongs to.
131    pub fn continent_name(&self) -> Option<&String> {
132        self.continent_name.as_ref()
133    }
134
135    /// Returns the dimensions of the map, given as the coordinates of the lower-left (SW)
136    /// and upper-right (NE) corners.
137    pub fn map_rect(&self) -> &Vec<(i32, i32)> {
138        &self.map_rect
139    }
140
141    /// Returns the dimensions of the map within the continent coordinate system,
142    /// given as the coordinates of the lower-left (SW) and upper-right (NE) corners.
143    pub fn continent_rect(&self) -> &Vec<(i32, i32)> {
144        &self.continent_rect
145    }
146}
147
148#[cfg(test)]
149mod tests {
150    use crate::v1::maps::*;
151    use crate::client::Client;
152
153    const JSON_MAP: &str = r#"
154    {
155      "map_name": "Queensdale",
156      "min_level": 1,
157      "max_level": 15,
158      "default_floor": 1,
159      "floors": [ 1, 3, 2, 0 ],
160      "type": "Public",
161      "region_id": 4,
162      "region_name": "Kryta",
163      "continent_id": 1,
164      "continent_name": "Tyria",
165      "map_rect": [
166        [ -43008, -27648 ],
167        [ 43008, 30720 ]
168      ],
169      "continent_rect": [
170        [ 9856, 11648 ],
171        [ 13440, 14080 ]
172      ]
173    }"#;
174
175    #[test]
176    fn create_map() {
177        match serde_json::from_str::<Map>(JSON_MAP) {
178            Ok(_) => assert!(true),
179            Err(e) => panic!(e.to_string()),
180        }
181    }
182
183    #[test]
184    fn get_map() {
185        let client = Client::new();
186        let id = 15;
187        let map = serde_json::from_str::<Map>(JSON_MAP).unwrap();
188        assert_eq!(Maps::get_id(&client, id.to_string()).unwrap().maps()[&id], map)
189    }
190
191    #[test]
192    fn get_all_maps() {
193        let client = Client::new();
194        assert!(Maps::get_all(&client).unwrap().maps().len() >= 50)
195    }
196}