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
//! Messages sent from client to server

use datatypes::*;

serde_decls! {
    /* READ BEFORE EDITING THIS FILE!
        Serialization/Deserialization is done in
        the order that the fields are declared.
        Changing the order of the fields without
        being aware of this will break things!
    */


    /// Initial packet sent to log in to
    /// the server.
    /// 
    /// This is sent to the server 
    /// when the player first joins.
    #[derive(Clone, Debug)]
    #[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
    pub struct Login {
        /// The current protocol version.
        /// Should always be 5 as of the 
        /// writing of this documentation.
        pub protocol: u8,
        /// The name that the player wishes
        /// to be called on the server. The 
        /// actual name of the player given 
        /// by the server will be returned 
        /// in the [`Login`](../server/struct.login.html)
        /// packet returned by the server.
        pub name: text,
        /// A session token for the current
        /// player. This is how a player logs
        /// into the server. If the player
        /// logging in wishes to be associated 
        /// with an account, this must be
        /// set. Otherwise, `"none"` works
        /// to avoid being given an account.
        pub session: text,
        /// Theoretically should set the size
        /// of the horizon beyond which players
        /// are not sent to the server. In practice
        /// doesn't appear to do anything.
        pub horizon_x: u16,
        /// Theoretically should set the size
        /// of the horizon beyond which players
        /// are not sent to the server. In practice
        /// doesn't appear to do anything.
        pub horizon_y: u16,
        /// The flag of the player, it should be a 
        /// 2-letter ISO country code corresponding
        /// to the country with the desired flag.
        pub flag: text
    }

    /// Opening packet for opening a second
    /// server connection for the same client.
    /// 
    /// This packet is used to allow for 
    /// multiple websocket connections to
    /// the airmash server. To open a second 
    /// connection, open a websocket connection
    /// to the server, then send this packet
    /// as the first packet instead of sending
    /// [`Login`](struct.login.html). The server
    /// will respond to client packets sent through
    /// this channel, allowing for some reduction 
    /// in packet roundtrip times.
    #[derive(Clone, Debug)]
    #[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
    pub struct Backup {
        pub token: text
    }

    /// In theory this should resize the horizon
    /// of the player. In practice the airmash
    /// server appears to ignore these packets.
    #[derive(Clone, Debug, Copy)]
    #[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
    pub struct Horizon {
        pub horizon_x: u16,
        pub horizon_y: u16
    }

    // Could include this, no point though
    //pub struct Ack { }

    /// Response packet to the server
    /// [`Ping`](../server/struct.ping.html)
    /// packet.
    #[derive(Clone, Debug, Copy)]
    #[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
    pub struct Pong {
        /// The ping number, should correspond 
        /// to the `num` field within in the 
        /// [`Ping`](../server/ping.html) packet
        /// sent by the server.
        pub num: u32
    }

    /// Send keystate of client
    #[derive(Clone, Debug, Copy)]
    #[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
    pub struct Key {
        pub seq: u32,
        /// Key that was pressed
        pub key: KeyCode,
        /// New state of the key
        pub state: KeyState
    }

    /// A free form command to be sent to the server.
    /// This is used for changing flags, respawning,
    /// spectating players, and selecting upgrades.
    /// 
    /// # Changing a flag
    /// ```
    /// # extern crate airmash_protocol;
    /// # use airmash_protocol::client::Command;
    /// # fn main() {
    /// let cmd = Command {
    ///     com: "flag".to_string(),
    ///     // Set to desired flag code,
    ///     // unknown will result in UN flag.
    ///     // Here we will set to the UN flag.
    ///     data: "XX".to_string()
    /// };
    /// 
    /// // Serialize and send to server here...
    /// # }
    /// ```
    /// 
    /// # Respawning as a plane
    /// ```
    /// # extern crate airmash_protocol;
    /// # use airmash_protocol::client::Command;
    /// # fn main() {
    /// let cmd = Command {
    ///     com: "respawn".to_string(),
    ///     // Choose the plane type here,
    ///     // each type is associated with
    ///     // an integer. Here we will pick
    ///     // predator.
    ///     data: "1".to_string()
    /// };
    /// 
    /// // Serialize and send to server here...
    /// # }
    /// ```
    /// 
    /// # Selecting Upgrades
    /// ```
    /// # extern crate airmash_protocol;
    /// # use airmash_protocol::client::Command;
    /// # fn main() {
    /// let cmd = Command {
    ///     com: "upgrade".to_string(),
    ///     // Choose upgrade type here.
    ///     // Here speed should be 1.
    ///     data: "1".to_string()
    /// };
    /// 
    /// // Serialize and send to server here...
    /// # }
    /// ```
    /// 
    /// # Going into spectate or spectating a different player
    /// ```
    /// # extern crate airmash_protocol;
    /// # use airmash_protocol::client::Command;
    /// # fn main() {
    /// let cmd = Command {
    ///     com: "spectate".to_string(),
    ///     // This can either be a player id, or
    ///     // one of -1, -2, or -3. -3 will force
    ///     // the player to go into spectate,
    ///     // -1 switches focus to the next player,
    ///     // and -2 switches focus to the previous
    ///     // player. Here we will force the player
    ///     // to go into spectate.
    ///     data: "-3".to_string()
    /// };
    /// 
    /// // Serialize and send to server here...
    /// # }
    /// 
    #[derive(Clone, Debug)]
    #[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
    pub struct Command {
        /// The command to send to the server,
        /// this can be one of `"spectate"`,
        /// `"upgrade"`, `"flag"`, or 
        /// `"respawn"`.
        pub com: text,
        /// The data associated with the command,
        /// valid values depend on the given command.
        pub data: text
    }

    //pub struct ScoreDetailed { }

    /// Say something in chat.
    #[derive(Clone, Debug)]
    #[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
    pub struct Chat {
        /// Text of the chat message.
        pub text: text
    }

    /// Send a whisper to a given player.
    #[derive(Clone, Debug)]
    #[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
    pub struct Whisper {
        /// The id of the player to send 
        /// the whisper to.
        pub id: u16,
        /// Contents of the whisper.
        pub text: text
    }

    /// Say a message in a chat bubble.
    #[derive(Clone, Debug)]
    #[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
    pub struct Say {
        /// The text within the chat bubble.
        pub text: text
    }

    /// Send a message to your team.
    #[derive(Clone, Debug)]
    #[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
    pub struct TeamChat {
        /// The message text.
        pub text: text
    }

    /// Issue a vote to mute a player.
    #[derive(Clone, Debug, Copy)]
    #[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
    pub struct VoteMute {
        /// The id of the player to mute.
        pub id: u16
    }

    #[derive(Clone, Debug, Copy)]
    #[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
    pub struct LocalPing {
        pub auth: u32
    }
}

/// All possible client packets.
/// 
/// This contains all valid packets that
/// the client can send to the server
/// (in the current version of the airmash
/// protocol). It can be serialized and 
/// deserialized to/from byte buffers
/// using [`to_bytes`](fn.to_bytes.html)
/// and [`from_bytes`](fn.from_bytes.html).
/// 
/// Some packets don't contain any data, these
/// packets do not have an associated struct
/// and as such are just empty variants within
/// this enum.
#[derive(Clone, Debug)]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
pub enum ClientPacket {
    Login(Login),
    Backup(Backup),
    Horizon(Horizon),
    Ack,
    Pong(Pong),
    Key(Key),
    Command(Command),
    ScoreDetailed,
    Chat(Chat),
    TeamChat(TeamChat),
    Whisper(Whisper),
    Say(Say),
    VoteMute(VoteMute),
    LocalPing(LocalPing),
}