1use crate::client::Client;
2use crate::error::ApiError;
3
4use std::collections::HashMap;
5
6const ENDPOINT_URL: &str = "/v1/maps";
7
8#[derive(Debug, Deserialize, PartialEq)]
10pub struct Maps {
11 maps: HashMap<u32, Map>,
13}
14
15#[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#[derive(Debug, Deserialize, PartialEq, Hash)]
34pub struct Map {
35 #[serde(rename = "map_name")]
37 name: String,
38 min_level: i32,
40 max_level: i32,
42 default_floor: i32,
44 #[serde(default)]
46 floors: Vec<i32>,
47 #[serde(default, rename = "type")]
49 map_type: Option<MapType>,
50 region_id: Option<u32>,
52 region_name: Option<String>,
54 continent_id: Option<u32>,
56 continent_name: Option<String>,
58 map_rect: Vec<(i32, i32)>,
61 continent_rect: Vec<(i32, i32)>,
64}
65
66impl Maps {
67 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 pub fn get_all(client: &Client) -> Result<Maps, ApiError> {
75 client.request(ENDPOINT_URL)
76 }
77
78 pub fn maps(&self) -> &HashMap<u32, Map> {
80 &self.maps
81 }
82}
83
84impl Map {
85 pub fn name(&self) -> &str {
87 &self.name
88 }
89
90 pub fn min_level(&self) -> i32 {
92 self.min_level
93 }
94
95 pub fn max_level(&self) -> i32 {
97 self.max_level
98 }
99
100 pub fn default_floor(&self) -> i32 {
102 self.default_floor
103 }
104
105 pub fn floors(&self) -> &Vec<i32> {
107 &self.floors
108 }
109
110 pub fn map_type(&self) -> Option<&MapType> {
112 self.map_type.as_ref()
113 }
114
115 pub fn region_id(&self) -> Option<u32> {
117 self.region_id
118 }
119
120 pub fn region_name(&self) -> Option<&String> {
122 self.region_name.as_ref()
123 }
124
125 pub fn continent_id(&self) -> Option<u32> {
127 self.continent_id
128 }
129
130 pub fn continent_name(&self) -> Option<&String> {
132 self.continent_name.as_ref()
133 }
134
135 pub fn map_rect(&self) -> &Vec<(i32, i32)> {
138 &self.map_rect
139 }
140
141 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}