Skip to main content

gw2api/v1/
event_details.rs

1use crate::client::Client;
2use crate::error::ApiError;
3
4use std::collections::HashMap;
5
6const ENDPOINT_URL: &str = "/v1/event_details";
7
8/// Struct containing a hashmap of all available events in the game.
9#[derive(Debug, Deserialize, PartialEq)]
10pub struct Events {
11    events: HashMap<String, Event>,
12}
13
14/// Flags representing the type of the event.
15#[derive(Debug, Deserialize, PartialEq)]
16pub enum Flag {
17    #[serde(rename = "group_event")]
18    GroupEvent,
19    #[serde(rename = "map_wide")]
20    MapWide,
21    #[serde(rename = "meta_event")]
22    MetaEvent,
23    #[serde(rename = "dungeon_event")]
24    DungeonEvent,
25}
26
27/// Contains information about an event.
28#[derive(Debug, Deserialize, PartialEq)]
29pub struct Event {
30    /// Name of the event.
31    name: String,
32    /// Level of the event.
33    level: u32,
34    /// The map id of the map where the event takes place.
35    map_id: u32,
36    /// A list of additional flags.
37    flags: Vec<Flag>,
38    /// The location of the event.
39    location: Location,
40}
41
42/// Possible shapes of the event area.
43#[derive(Debug, Deserialize, PartialEq)]
44pub enum Shape {
45    #[serde(rename = "sphere")]
46    Sphere,
47    #[serde(rename = "cylinder")]
48    Cylinder,
49    #[serde(rename = "poly")]
50    Poly,
51}
52
53#[derive(Debug, Deserialize, PartialEq)]
54pub struct Location {
55    /// Shape of the event area (sphere, cylinder, poly).
56    #[serde(rename = "type")]
57    shape: Shape,
58    /// Coordinates for the center of the event.
59    center: Vec<f32>,
60    /// Height of a non-sphere.
61    #[serde(default)]
62    height: f32,
63    /// Radius of a sphere/cylinder.
64    #[serde(default)]
65    radius: f32,
66    /// Rotation of a sphere/cylinder.
67    #[serde(default)]
68    rotation: f32,
69    /// Range of the polygon.
70    #[serde(default)]
71    z_range: Vec<f32>,
72    /// Points of the polygon.
73    #[serde(default)]
74    points: Vec<(f32, f32)>,
75}
76
77impl Events {
78    /// Retrieve an event by its id.
79    pub fn get_id(client: &Client, id: String) -> Result<Events, ApiError> {
80        let url = format!("{}?event_id={}", ENDPOINT_URL, id);
81        client.request(&url)
82    }
83
84    /// Retrieve all continents that are in the game.
85    pub fn get_all_events(client: &Client) -> Result<Events, ApiError> {
86        client.request(ENDPOINT_URL)
87    }
88
89    /// Returns the hashmap containing all the events.
90    pub fn events(&self) -> &HashMap<String, Event> {
91        &self.events
92    }
93}
94
95impl Event {
96    /// Returns the name of the continent.
97    pub fn name(&self) -> &str {
98        &self.name
99    }
100
101    /// Returns the level of the continent.
102    pub fn level(&self) -> u32 {
103        self.level
104    }
105
106    /// Returns the map id of the map where the event takes place.
107    pub fn map_id(&self) -> u32 {
108        self.map_id
109    }
110
111    /// Returns the flags describing the type of event it is (can be empty).
112    pub fn flags(&self) -> &Vec<Flag> {
113        &self.flags
114    }
115
116    /// Returns location & shape of the event.
117    pub fn location(&self) -> &Location {
118        &self.location
119    }
120}
121
122impl Location {
123    /// Returns the shape of the event.
124    pub fn shape(&self) -> &Shape {
125        &self.shape
126    }
127
128    /// Returns the coordinates for the center of the event.
129    pub fn center(&self) -> &Vec<f32> {
130        &self.center
131    }
132
133    /// Returns the height of a non-sphere event zone.
134    pub fn height(&self) -> f32 {
135        self.height
136    }
137
138    /// Returns the radius of a sphere/cylinder event zone.
139    pub fn radius(&self) -> f32 {
140        self.radius
141    }
142
143    /// Returns the rotation of a sphere/cylinder event zone.
144    pub fn rotation(&self) -> f32 {
145        self.rotation
146    }
147
148    /// Returns the range of the polygon.
149    pub fn z_range(&self) -> &Vec<f32> {
150        &self.z_range
151    }
152
153    /// Returns the points of a polygon.
154    pub fn points(&self) -> &Vec<(f32, f32)> {
155        &self.points
156    }
157}
158
159#[cfg(test)]
160mod tests {
161    use crate::v1::event_details::*;
162    use crate::client::Client;
163
164    const JSON_EVENT: &str = r#"
165    {
166      "name": "Defeat the renegade charr.",
167      "level": 42,
168      "map_id": 20,
169      "flags": [],
170      "location": {
171        "type": "sphere",
172        "center": [ -9463.6, -40310.2, -785.799 ],
173        "radius": 2500,
174        "rotation": 0
175      }
176    }"#;
177
178    #[test]
179    fn create_event() {
180        match serde_json::from_str::<Event>(JSON_EVENT) {
181            Ok(_) => assert!(true),
182            Err(e) => panic!(e.to_string()),
183        }
184    }
185
186    #[test]
187    fn get_all_events() {
188        let client = Client::new();
189        assert!(Events::get_all_events(&client).unwrap().events().len() >= 1000)
190    }
191}