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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
use serde::Deserialize;

#[derive(Deserialize)]
/// This struct represents the Minehut API server response. It
/// only contains a server struct
pub struct ServerResponse {
    /// Actual server data.
    pub server: Server
}

#[derive(Debug, Deserialize)]
/// This struct represents a Minehut server, contains most 
/// information returned by the Minehut API.
pub struct Server {
    /// Categories of the server
    pub categories: Vec<String>,
    #[serde(rename = "inheritedCategories")]
    pub inherited_categories: Vec<String>,
    /// Icons purchased by the owner.
    pub purchased_icons: Vec<String>,
    /// Backup slots for the server.
    pub backup_slots: usize,
    /// Whether ser
    /// ver is suspended.
    pub suspended: bool,
    /// What server type it is.
    pub server_version_type: String,
    /// Wether it uses a proxy.
    pub proxy: bool,
    /// All linked servers.
    #[serde(rename = "connectedServers")]
    pub connected_servers: Vec<String>,
    /// ID of the server.
    #[serde(rename = "_id")]
    pub id: String,
    /// MOTD of the server.
    pub motd: String,
    /// Wether the server is allowed to be seen on the server list.
    pub visibility: bool,
    /// The Minehut plan it is using.
    pub server_plan: String,
    /// Its storage node.
    pub storage_node: String,
    /// UUID of the owner.
    pub owner: String,
    /// Name of the server.
    pub name: String,
    /// Lower case name of the server.
    pub name_lower: String,
    /// Time it was created in.
    pub creation: usize,
    /// What platform it is on.
    pub platform: String,
    /// Credits spent per day to host it.
    pub credits_per_day: f32,
    __v: isize,
    /// Time it was last online.
    pub last_online: usize,
    /// Its icon.
    pub active_icon: Option<String>,
    /// Icon of the server.
    pub icon: Option<String>, 
    /// Wether it is online or not.
    pub online: bool,
    /// The maximum number of players it supports.
    #[serde(rename = "maxPlayers")]
    pub max_players: usize,
    /// Current number of players on it.
    #[serde(rename = "playerCount")]
    pub player_count: usize,
    /// Raw Minehut plan.
    #[serde(rename = "rawPlan")]
    pub raw_plan: String,
    /// Current server plan.
    #[serde(rename = "activeServerPlan")]
    pub active_server_plan: String
}

#[derive(Debug, Deserialize)]
/// This struct represents a server icon response from the 
/// Minehut API. Contains useful information about icons such
/// as the ID, name, price and rank.
pub struct ServerIcon {
    /// ID of the server icon.
    #[serde(rename = "_id")]
    pub id: String,
    /// Display name of it.
    pub display_name: String,
    /// Name of it.
    pub icon_name: String,
    /// Credits to buy icon.
    pub price: usize,
    /// Rarity of the icon.
    pub rank: String,
    /// Whether it can be bought.
    pub available: bool,
    /// Wether it is disabled.
    pub disabled: bool,
    /// When it was created.
    pub created: usize,
    /// When it was last updated.
    pub last_updated: usize,
    __v: isize
}

#[derive(Deserialize)]
/// This struct represents the response for a server list from the
/// Minehut API, it only contains a list of server data.
pub struct ServerListResponse {
    /// List of server data.
    pub servers: Vec<ServerData> 
}

/// This struct represents the server data obtained from server list.
/// It is different from the normal Server struct due to how Minehut
/// structured the response.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ServerData {
    /// More information about the server.
    pub static_info: StaticInfo,
    /// Name of the server.
    pub name: String,
    /// MOTD of the server.
    pub motd: String,
    /// Icon of the server.
    pub icon: Option<String>,
    /// Information about players playing on server.
    pub player_data: PlayerData,
    /// Can be connected to, might return nothing.
    pub connectable: Option<bool>,
    /// Whether server can be seen on server list.
    pub visibility: bool
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Static information for the ServerListData struct. Contains
/// useful information about the server
pub struct StaticInfo {
    /// ID of server.
    #[serde(rename = "_id")]
    pub id: String,
    /// Current server plan.
    pub server_plan: String,
    /// Plan start date.
    pub service_start_date: usize,
    /// Platform hosted on.
    pub platform: String,
    /// Maximum amount of players it can support.
    #[serde(rename = "planMaxPlayers")]
    pub max_players: usize,
    /// Raw plan.
    pub raw_plan: String,
    /// Linked servers (network).
    pub connected_servers: Vec<String>
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Player data struct to deserialize from ServerListData. Struct
/// Only stores player count.
pub struct PlayerData {
    /// Number of players on server.
    pub player_count: usize,
}

#[derive(Deserialize)]
/// This is the struct representation of the server data response from the server/id/server_data 
/// in Minehut.
pub struct OwnedServerRes {
    pub server: OwnedServer
}

#[derive(Debug, Deserialize)]
/// This represents servers owned by an authenticated user using the Client struct. Closely resembles
/// the Server struct with slight modifications allowing more information to be seen.
pub struct OwnedServer {
    // ID of the server.
    #[serde(rename = "_id")]
    pub id: String,
    /// List of plugins.
    pub active_plugins: Vec<String>,
    /// Number of back up slots.
    pub backup_slots: usize,
    /// List of categories.
    pub categories: Vec<String>,
    /// List of connected server IDs.
    #[serde(rename = "connectedServers")]
    pub connected_servers: Vec<String>,
    /// Time the server was created.
    pub creation: usize,
    /// Credits spent to host server per day.
    pub credits_per_day: f32,
    /// List of installed contents.
    pub installed_content: Vec<InstalledContent>,
    /// When it was last online.
    pub last_online: usize,
    /// MOTD of the server.
    pub motd: String,
    /// Name of server.
    pub name: String,
    /// Lower case name of server
    pub name_lower: String,
    /// Owner of server.
    pub owner: String,
    /// Platform of server.
    pub platform: String,
    /// Whether server uses a proxy.
    pub proxy: bool,
    /// List of purchased icon IDs.
    pub purchased_icons: Vec<String>,
    /// Server plan name.
    pub server_plan: String,
    /// Server properties of the server.
    pub server_properties: ServerProperties,
    /// Server version type.
    pub server_version_type: String,
    /// Wether server is suspended.
    pub suspended: bool,
    /// Wether server was visible.
    pub visibility: bool
}

#[derive(Debug, Deserialize)]
/// This is a struct that stores all server properties, used in owned server.
pub struct ServerProperties {
    /// Wether server allows fly.
    #[serde(rename = "allow-flight")]
    pub allow_flight: bool,
    /// Nether is enabled or not.
    #[serde(rename = "allow-nether")]
    pub allow_nether: bool,
    /// Announce player achievements.
    #[serde(rename = "announce-player-achievements")]
    pub announce_player_achievements: bool,
    /// Difficulty of the server.
    pub difficulty: i8,
    /// Command blocks are allowed or not.
    #[serde(rename = "enable-command-block")]
    pub allow_cmd_block: bool,
    /// Enforce white list.
    #[serde(rename = "enforce-whitelist")]
    pub enforce_whitelist: bool,
    /// Force game mode on join.
    #[serde(rename = "force-gamemode")]
    pub force_gamemode: bool,
    /// Gamemode.
    pub gamemode: usize,
    /// Hardcore mode enabled or not.
    pub hardcore: bool,
    /// Main server name.
    #[serde(rename = "level-name")]
    pub level_name: String,
    /// Max amounbt of players.
    #[serde(rename = "max-players")]
    pub max_players: usize,
    /// Tick time.
    #[serde(rename = "max-tick-time")]
    pub max_tick_time: usize,
    /// OP permissions level.
    #[serde(rename = "op-permission-level")]
    pub permission_level: u8,
    /// PVP enabled or not.
    pub pvp: bool,
    /// Spawn animals.
    #[serde(rename = "spawn-mobs")]
    pub spawn_mobs: bool,
    /// Spawn NPCs.
    #[serde(rename = "spawn-npcs")]
    pub spawn_npcs: bool,
    /// Spawn protection.
    #[serde(rename = "spawn-protection")]
    pub spawn_prot: usize,
    /// White list is on or not.
    #[serde(rename = "white-list")]
    pub whitelist: bool
}

#[derive(Debug, Deserialize)]
/// This represents installed content response from Minehut, used in owned servers to see all
/// content on the server.
pub struct InstalledContent {
    /// ID of content.
    #[serde(rename = "_id")]
    pub id: String,
    /// Content ID.
    pub content_id: String,
    /// Content version ID.
    pub content_version_id: String,
    /// When content was installed.
    pub install_date: String,
    /// When it was last updated.
    pub last_updated: String,
    /// Whether it is pinned.
    pub pinned: bool
}

use crate::{Client, Error, http};

impl OwnedServer {
    /// Start an owned server and takes it out of hibernation.
    /// 
    /// # Arguments
    /// 
    /// * `client` - Instance of calling Client struct.
    /// 
    /// # Example
    /// 
    /// ```no_run
    /// match client.my_server("server-name").await?.start_service(&client).await {
    ///     Err(_) => println!("Could not start server"),
    ///     Ok(_) => prinln!("Started server!")
    /// }
    /// ```
    /// 
    /// # Error
    /// 
    /// Returns a FailedOp error if it could not post data. This usually happens if
    /// you are not allowed to start the server.
    pub async fn start_service(&self, client: &Client) -> Result<(), Error> {
        http::post(&client, &format!("server/{}/start_service", &self.id)).await
    }

    /// Stop an owned server and hibernate it.
    /// 
    /// # Arguments
    /// 
    /// * `client` - Instance of calling Client struct.
    /// 
    /// # Example
    /// 
    /// ```no_run
    /// match client.my_server("server-name").await?.destroy_service(&client).await {
    ///     Err(_) => println!("Failed to stop server"),
    ///     Ok(_) => println!("Hibernating server")
    /// }
    /// ```
    /// 
    /// # Error
    /// 
    /// Returns a FailedOp error if it could not post data. This usually happens if
    /// you are not allowed to stop the server.
    pub async fn destroy_service(&self, client: &Client) -> Result<(), Error> {
        http::post(&client, &format!("server/{}/destroy_service", &self.id)).await
    }

    /// Start an owned server and hibernate it.
    /// 
    /// # Arguments
    /// 
    /// * `client` - Instance of calling Client struct.
    /// 
    /// # Example
    /// 
    /// ```no_run
    /// match client.my_server("server-name").await?.start(&client).await {
    ///     Err(e) => println!("Something went wrong whilst starting server: {e}"),
    ///     Ok(_) => println!("Started server, nice.")
    /// }
    /// ```
    /// 
    /// # Error
    /// 
    /// Returns a FailedOp error if it could not post data. This usually happens if 
    /// you are not allowed to start the server.
    pub async fn start(&self, client: &Client) -> Result<(), Error> {
        http::post(&client, &format!("server/{}/start", &self.id)).await
    }

    /// Stops a server without hibernating the server.
    /// 
    /// # Arguments
    /// 
    /// * `client` - Instance of calling Client struct.
    /// 
    /// # Example
    /// 
    /// ```no_run
    /// if let Ok(_) = client.my_server("server-name").await?.shut_down(&client).await {
    ///     println!("Successfully stopped server");
    /// }
    /// ```
    /// 
    /// # Error
    /// 
    /// Returns a FailedOp error if it could not post data. This usually happens if
    /// you are not allowed to shut the server down/
    pub async fn shut_down(&self, client: &Client) -> Result<(), Error> {
        http::post(&client, &format!("server/{}/shutdown", &self.id)).await
    }
}