Skip to main content

esi_openapi/groups/
fleets.rs

1#![allow(unused)]
2
3use crate::prelude::*;
4
5/// Endpoints for Fleets
6pub struct FleetsGroup<'a> {
7    pub(crate) esi: &'a Esi,
8}
9
10#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
11/// The role of a character in a fleet.
12pub enum FleetRole {
13    /// Fleet commander.
14    #[serde(rename = "fleet_commander")]
15    FleetCommander,
16    /// Wing commander.
17    #[serde(rename = "wing_commander")]
18    WingCommander,
19    /// Squad commander.
20    #[serde(rename = "squad_commander")]
21    SquadCommander,
22    /// Squad member.
23    #[serde(rename = "squad_member")]
24    SquadMember,
25    /// A value not known to this version of the crate.
26    #[serde(other)]
27    Unrecognized,
28}
29
30#[derive(Debug, Deserialize, Serialize, Clone)]
31/// The fleet a character is in.
32pub struct CharacterFleet {
33    /// Character ID of the fleet boss.
34    pub fleet_boss_id: i64,
35    /// ID of the fleet.
36    pub fleet_id: i64,
37    /// Role of the character in the fleet.
38    pub role: FleetRole,
39    /// ID of the squad the character is in (`-1` if none).
40    pub squad_id: i64,
41    /// ID of the wing the character is in (`-1` if none).
42    pub wing_id: i64,
43}
44
45#[derive(Debug, Deserialize, Serialize, Clone)]
46/// Fleet settings.
47pub struct FleetInfo {
48    /// Whether free-move is enabled.
49    pub is_free_move: bool,
50    /// Whether the fleet is registered in the fleet finder.
51    pub is_registered: bool,
52    /// Whether voice is enabled.
53    pub is_voice_enabled: bool,
54    /// Message of the day.
55    pub motd: String,
56}
57
58#[derive(Debug, Serialize, Clone, Default)]
59/// The body of a request to update fleet settings.
60pub struct FleetUpdate {
61    /// Whether free-move is enabled.
62    #[serde(skip_serializing_if = "Option::is_none")]
63    pub is_free_move: Option<bool>,
64    /// Message of the day.
65    #[serde(skip_serializing_if = "Option::is_none")]
66    pub motd: Option<String>,
67}
68
69#[derive(Debug, Deserialize, Serialize, Clone)]
70/// A member of a fleet.
71pub struct FleetMember {
72    /// Character ID of the member.
73    pub character_id: i64,
74    /// Date and time the member joined.
75    pub join_time: String,
76    /// Role of the member.
77    pub role: FleetRole,
78    /// Localized role name.
79    pub role_name: String,
80    /// Type ID of the ship the member flies.
81    pub ship_type_id: i64,
82    /// Solar system the member is in.
83    pub solar_system_id: i64,
84    /// Squad of the member (`-1` if none).
85    pub squad_id: i64,
86    /// Station the member is docked at, if any.
87    #[serde(default)]
88    pub station_id: Option<i64>,
89    /// Whether the member takes fleet warps.
90    pub takes_fleet_warp: bool,
91    /// Wing of the member (`-1` if none).
92    pub wing_id: i64,
93}
94
95#[derive(Debug, Serialize, Clone)]
96/// The body of a request to invite a character to a fleet.
97pub struct FleetInvitation {
98    /// Character to invite.
99    pub character_id: i64,
100    /// Role to give the character.
101    pub role: FleetRole,
102    /// Squad to place the character in, when the role requires it.
103    #[serde(skip_serializing_if = "Option::is_none")]
104    pub squad_id: Option<i64>,
105    /// Wing to place the character in, when the role requires it.
106    #[serde(skip_serializing_if = "Option::is_none")]
107    pub wing_id: Option<i64>,
108}
109
110#[derive(Debug, Serialize, Clone)]
111/// The body of a request to move a fleet member.
112pub struct FleetMovement {
113    /// New role of the member.
114    pub role: FleetRole,
115    /// New squad of the member, when the role requires it.
116    #[serde(skip_serializing_if = "Option::is_none")]
117    pub squad_id: Option<i64>,
118    /// New wing of the member, when the role requires it.
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub wing_id: Option<i64>,
121}
122
123#[derive(Debug, Serialize, Clone)]
124/// The body of a request to rename a wing or squad.
125pub struct FleetNaming {
126    /// New name.
127    pub name: String,
128}
129
130#[derive(Debug, Deserialize, Serialize, Clone)]
131/// A squad of a fleet wing.
132pub struct FleetSquad {
133    /// ID of the squad.
134    pub id: i64,
135    /// Name of the squad.
136    pub name: String,
137}
138
139#[derive(Debug, Deserialize, Serialize, Clone)]
140/// A wing of a fleet.
141pub struct FleetWing {
142    /// ID of the wing.
143    pub id: i64,
144    /// Name of the wing.
145    pub name: String,
146    /// Squads of the wing.
147    pub squads: Vec<FleetSquad>,
148}
149
150#[derive(Debug, Deserialize, Serialize, Clone, Copy)]
151/// The result of creating a wing.
152pub struct CreatedWing {
153    /// ID of the new wing.
154    pub wing_id: i64,
155}
156
157#[derive(Debug, Deserialize, Serialize, Clone, Copy)]
158/// The result of creating a squad.
159pub struct CreatedSquad {
160    /// ID of the new squad.
161    pub squad_id: i64,
162}
163
164impl FleetsGroup<'_> {
165    api_get!(
166        /// Return the fleet a character is in.
167        get_character_fleet,
168        "GetCharactersCharacterIdFleet",
169        RequestType::Authenticated,
170        CharacterFleet,
171        (character_id: i64) => "{character_id}"
172    );
173
174    api_get!(
175        /// Return the settings of a fleet.
176        get_fleet,
177        "GetFleetsFleetId",
178        RequestType::Authenticated,
179        FleetInfo,
180        (fleet_id: i64) => "{fleet_id}"
181    );
182
183    api_put!(
184        /// Update the settings of a fleet.
185        update_fleet,
186        "PutFleetsFleetId",
187        RequestType::Authenticated,
188        (),
189        (fleet_id: i64) => "{fleet_id}",
190        settings: &FleetUpdate,
191    );
192
193    api_get!(
194        /// Return the members of a fleet.
195        get_members,
196        "GetFleetsFleetIdMembers",
197        RequestType::Authenticated,
198        Vec<FleetMember>,
199        (fleet_id: i64) => "{fleet_id}"
200    );
201
202    api_post!(
203        /// Invite a character to a fleet.
204        invite_member,
205        "PostFleetsFleetIdMembers",
206        RequestType::Authenticated,
207        (),
208        (fleet_id: i64) => "{fleet_id}",
209        invitation: &FleetInvitation,
210    );
211
212    api_delete!(
213        /// Kick a member from a fleet.
214        kick_member,
215        "DeleteFleetsFleetIdMembersMemberId",
216        RequestType::Authenticated,
217        (),
218        (fleet_id: i64) => "{fleet_id}",
219        (member_id: i64) => "{member_id}"
220    );
221
222    api_put!(
223        /// Move a fleet member to another role, squad or wing.
224        move_member,
225        "PutFleetsFleetIdMembersMemberId",
226        RequestType::Authenticated,
227        (),
228        (fleet_id: i64) => "{fleet_id}",
229        (member_id: i64) => "{member_id}",
230        movement: &FleetMovement,
231    );
232
233    api_delete!(
234        /// Delete a fleet squad (only empty squads can be deleted).
235        delete_squad,
236        "DeleteFleetsFleetIdSquadsSquadId",
237        RequestType::Authenticated,
238        (),
239        (fleet_id: i64) => "{fleet_id}",
240        (squad_id: i64) => "{squad_id}"
241    );
242
243    api_put!(
244        /// Rename a fleet squad.
245        rename_squad,
246        "PutFleetsFleetIdSquadsSquadId",
247        RequestType::Authenticated,
248        (),
249        (fleet_id: i64) => "{fleet_id}",
250        (squad_id: i64) => "{squad_id}",
251        naming: &FleetNaming,
252    );
253
254    api_get!(
255        /// Return the wings and squads of a fleet.
256        get_wings,
257        "GetFleetsFleetIdWings",
258        RequestType::Authenticated,
259        Vec<FleetWing>,
260        (fleet_id: i64) => "{fleet_id}"
261    );
262
263    api_post!(
264        /// Create a new wing in a fleet.
265        create_wing,
266        "PostFleetsFleetIdWings",
267        RequestType::Authenticated,
268        CreatedWing,
269        (fleet_id: i64) => "{fleet_id}";
270        NoBody
271    );
272
273    api_delete!(
274        /// Delete a fleet wing (only empty wings can be deleted).
275        delete_wing,
276        "DeleteFleetsFleetIdWingsWingId",
277        RequestType::Authenticated,
278        (),
279        (fleet_id: i64) => "{fleet_id}",
280        (wing_id: i64) => "{wing_id}"
281    );
282
283    api_put!(
284        /// Rename a fleet wing.
285        rename_wing,
286        "PutFleetsFleetIdWingsWingId",
287        RequestType::Authenticated,
288        (),
289        (fleet_id: i64) => "{fleet_id}",
290        (wing_id: i64) => "{wing_id}",
291        naming: &FleetNaming,
292    );
293
294    api_post!(
295        /// Create a new squad in a fleet wing.
296        create_squad,
297        "PostFleetsFleetIdWingsWingIdSquads",
298        RequestType::Authenticated,
299        CreatedSquad,
300        (fleet_id: i64) => "{fleet_id}",
301        (wing_id: i64) => "{wing_id}";
302        NoBody
303    );
304}
305
306#[cfg(test)]
307mod fleets_tests {
308    use super::*;
309
310    #[test]
311    fn test_parse_fleet_and_members() {
312        let f: CharacterFleet = serde_json::from_str(
313            r#"{"fleet_boss_id": 1, "fleet_id": 1234567890123, "role": "squad_member",
314                "squad_id": -1, "wing_id": 2000000000000}"#,
315        )
316        .unwrap();
317        assert_eq!(f.fleet_id, 1_234_567_890_123);
318        assert_eq!(f.role, FleetRole::SquadMember);
319        let m: Vec<FleetMember> = serde_json::from_str(
320            r#"[{"character_id": 1, "join_time": "t", "role": "new_role", "role_name": "r",
321                 "ship_type_id": 587, "solar_system_id": 30000142, "squad_id": 1, "wing_id": 2,
322                 "takes_fleet_warp": true}]"#,
323        )
324        .unwrap();
325        assert_eq!(m[0].role, FleetRole::Unrecognized);
326        assert_eq!(m[0].station_id, None);
327    }
328
329    #[test]
330    fn test_serialize_bodies() {
331        let inv = FleetInvitation {
332            character_id: 1,
333            role: FleetRole::SquadMember,
334            squad_id: Some(5),
335            wing_id: None,
336        };
337        assert_eq!(
338            serde_json::to_string(&inv).unwrap(),
339            r#"{"character_id":1,"role":"squad_member","squad_id":5}"#
340        );
341        let u = FleetUpdate {
342            is_free_move: Some(true),
343            motd: None,
344        };
345        assert_eq!(
346            serde_json::to_string(&u).unwrap(),
347            r#"{"is_free_move":true}"#
348        );
349    }
350
351    #[test]
352    fn test_parse_wings_and_created() {
353        let w: Vec<FleetWing> =
354            serde_json::from_str(r#"[{"id": 1, "name": "w", "squads": [{"id": 2, "name": "s"}]}]"#)
355                .unwrap();
356        assert_eq!(w[0].squads[0].id, 2);
357        let c: CreatedWing = serde_json::from_str(r#"{"wing_id": 2000000000000}"#).unwrap();
358        assert_eq!(c.wing_id, 2_000_000_000_000);
359    }
360}