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
use crate::client::Client;
use crate::error::ApiError;
use std::collections::HashMap;
const ENDPOINT_URL: &str = "/v1/event_details";
/// Struct containing a hashmap of all available events in the game.
#[derive(Debug, Deserialize, PartialEq)]
pub struct Events {
events: HashMap<String, Event>,
}
/// Flags representing the type of the event.
#[derive(Debug, Deserialize, PartialEq)]
pub enum Flag {
#[serde(rename = "group_event")]
GroupEvent,
#[serde(rename = "map_wide")]
MapWide,
#[serde(rename = "meta_event")]
MetaEvent,
#[serde(rename = "dungeon_event")]
DungeonEvent,
}
/// Contains information about an event.
#[derive(Debug, Deserialize, PartialEq)]
pub struct Event {
/// Name of the event.
name: String,
/// Level of the event.
level: u32,
/// The map id of the map where the event takes place.
map_id: u32,
/// A list of additional flags.
flags: Vec<Flag>,
/// The location of the event.
location: Location,
}
/// Possible shapes of the event area.
#[derive(Debug, Deserialize, PartialEq)]
pub enum Shape {
#[serde(rename = "sphere")]
Sphere,
#[serde(rename = "cylinder")]
Cylinder,
#[serde(rename = "poly")]
Poly,
}
#[derive(Debug, Deserialize, PartialEq)]
pub struct Location {
/// Shape of the event area (sphere, cylinder, poly).
#[serde(rename = "type")]
shape: Shape,
/// Coordinates for the center of the event.
center: Vec<f32>,
/// Height of a non-sphere.
#[serde(default)]
height: f32,
/// Radius of a sphere/cylinder.
#[serde(default)]
radius: f32,
/// Rotation of a sphere/cylinder.
#[serde(default)]
rotation: f32,
/// Range of the polygon.
#[serde(default)]
z_range: Vec<f32>,
/// Points of the polygon.
#[serde(default)]
points: Vec<(f32, f32)>,
}
impl Events {
/// Retrieve an event by its id.
pub fn get_id(client: &Client, id: String) -> Result<Events, ApiError> {
let url = format!("{}?event_id={}", ENDPOINT_URL, id);
client.request(&url)
}
/// Retrieve all continents that are in the game.
pub fn get_all_events(client: &Client) -> Result<Events, ApiError> {
client.request(ENDPOINT_URL)
}
/// Returns the hashmap containing all the events.
pub fn events(&self) -> &HashMap<String, Event> {
&self.events
}
}
impl Event {
/// Returns the name of the continent.
pub fn name(&self) -> &str {
&self.name
}
/// Returns the level of the continent.
pub fn level(&self) -> u32 {
self.level
}
/// Returns the map id of the map where the event takes place.
pub fn map_id(&self) -> u32 {
self.map_id
}
/// Returns the flags describing the type of event it is (can be empty).
pub fn flags(&self) -> &Vec<Flag> {
&self.flags
}
/// Returns location & shape of the event.
pub fn location(&self) -> &Location {
&self.location
}
}
impl Location {
/// Returns the shape of the event.
pub fn shape(&self) -> &Shape {
&self.shape
}
/// Returns the coordinates for the center of the event.
pub fn center(&self) -> &Vec<f32> {
&self.center
}
/// Returns the height of a non-sphere event zone.
pub fn height(&self) -> f32 {
self.height
}
/// Returns the radius of a sphere/cylinder event zone.
pub fn radius(&self) -> f32 {
self.radius
}
/// Returns the rotation of a sphere/cylinder event zone.
pub fn rotation(&self) -> f32 {
self.rotation
}
/// Returns the range of the polygon.
pub fn z_range(&self) -> &Vec<f32> {
&self.z_range
}
/// Returns the points of a polygon.
pub fn points(&self) -> &Vec<(f32, f32)> {
&self.points
}
}
#[cfg(test)]
mod tests {
use crate::v1::event_details::*;
use crate::client::Client;
const JSON_EVENT: &str = r#"
{
"name": "Defeat the renegade charr.",
"level": 42,
"map_id": 20,
"flags": [],
"location": {
"type": "sphere",
"center": [ -9463.6, -40310.2, -785.799 ],
"radius": 2500,
"rotation": 0
}
}"#;
#[test]
fn create_event() {
match serde_json::from_str::<Event>(JSON_EVENT) {
Ok(_) => assert!(true),
Err(e) => panic!(e.to_string()),
}
}
#[test]
fn get_all_events() {
let client = Client::new();
assert!(Events::get_all_events(&client).unwrap().events().len() >= 1000)
}
}