airmash_protocol/packets/
client.rs

1//! Packets that the client sends to the server.
2
3use bstr::BString;
4
5use crate::enums::KeyCode;
6
7/// Opening packet for opening a second server connection for the same client.
8///
9/// This packet is used to allow for multiple websocket connections to the
10/// airmash server. To open a second connection, open a websocket connection to
11/// the server, then send this packet as the first packet instead of sending
12/// [`Login`](struct.login.html). The server will respond to client packets sent
13/// through this channel, allowing for some reduction in head of line blocking.
14#[derive(Clone, Debug)]
15#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
16pub struct Backup {
17  pub token: BString,
18}
19
20/// Say something in public chat.
21#[derive(Clone, Debug)]
22#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
23pub struct Chat {
24  pub text: BString,
25}
26
27/// A free form command to be sent to the server. This is used for changing
28/// flags, respawning, spectating players, and selecting upgrades.
29///
30/// # Changing a flag
31/// ```
32/// # extern crate airmash_protocol;
33/// # use airmash_protocol::client::Command;
34/// # fn main() {
35/// let cmd = Command {
36///     com: "flag".into(),
37///     // Set to desired flag code,
38///     // unknown will result in UN flag.
39///     // Here we will set to the UN flag.
40///     data: "XX".into()
41/// };
42///
43/// // Serialize and send to server here...
44/// # }
45/// ```
46///
47/// # Respawning as a plane
48/// ```
49/// # extern crate airmash_protocol;
50/// # use airmash_protocol::client::Command;
51/// # fn main() {
52/// let cmd = Command {
53///     com: "respawn".into(),
54///     // Choose the plane type here,
55///     // each type is associated with
56///     // an integer. Here we will pick
57///     // predator.
58///     data: "1".into()
59/// };
60///
61/// // Serialize and send to server here...
62/// # }
63/// ```
64///
65/// # Selecting Upgrades
66/// ```
67/// # extern crate airmash_protocol;
68/// # use airmash_protocol::client::Command;
69/// # fn main() {
70/// let cmd = Command {
71///     com: "upgrade".into(),
72///     // Choose upgrade type here.
73///     // Here speed should be 1.
74///     data: "1".into()
75/// };
76///
77/// // Serialize and send to server here...
78/// # }
79/// ```
80///
81/// # Going into spectate or spectating a different player
82/// ```
83/// # extern crate airmash_protocol;
84/// # use airmash_protocol::client::Command;
85/// # use bstr::B;
86/// # fn main() {
87/// let cmd = Command {
88///     com: "spectate".into(),
89///     // This can either be a player id, or
90///     // one of -1, -2, or -3. -3 will force
91///     // the player to go into spectate,
92///     // -1 switches focus to the next player,
93///     // and -2 switches focus to the previous
94///     // player. Here we will force the player
95///     // to go into spectate.
96///     data: "-3".into()
97/// };
98///
99/// // Serialize and send to server here...
100/// # }
101#[derive(Clone, Debug)]
102#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
103pub struct Command {
104  /// The command to send to the server. The official server recognizes the
105  /// commands `"spectate"`, `"upgrade"`, `"flag"`, and `"respawn"`.
106  pub com: BString,
107  /// The data associated with the command,
108  /// value values epend on the given command.
109  pub data: BString,
110}
111
112/// Sent periodically by the client to indicate that it is still alive to the
113/// server.
114#[derive(Copy, Clone, Debug, Default)]
115#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
116pub struct Ack;
117
118/// Request a detailed score packet from the server.
119#[derive(Copy, Clone, Debug, Default)]
120#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
121pub struct ScoreDetailed;
122
123/// Packet to tell the server to resize the horizon for the client.
124///
125/// In theory this should expand the visible range for the client, in practice
126/// the official server appears to ignore these packets.
127#[derive(Copy, Clone, Debug)]
128#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
129pub struct Horizon {
130  pub horizon_x: u16,
131  pub horizon_y: u16,
132}
133
134/// Purpose unknown, doesn't appear to be used in the official client.
135#[derive(Copy, Clone, Debug)]
136#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
137pub struct LocalPing {
138  pub auth: u32,
139}
140
141/// Initial packet sent to log in to the server.
142///
143/// This sent to the server when the player first joins.
144#[derive(Clone, Debug)]
145#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
146pub struct Login {
147  /// The current protocol version. Should always be 5 as of the writing of this
148  /// documentation.
149  pub protocol: u8,
150  /// The name that the player wishes to be called on the server. The actual
151  /// name of the player given by the server will be in the
152  /// [`Login`](../server/struct.login.html) packet returned by the server.
153  pub name: BString,
154  /// A session token for the current player. This session token is the way that
155  /// a player would log in to the server. If the player does not wish to be
156  /// logged on to the server then a session token of `"none"` will suffice.
157  pub session: BString,
158  /// Should set the size of the horizon beyond which game updates (missile
159  /// updates and player updates) are not sent to the client. In practice, this
160  /// doesn't appear to be used by the official server.
161  pub horizon_x: u16,
162  /// Same as `horizon_x` but in the y direction.
163  pub horizon_y: u16,
164  /// The desired flag of the player. This should be the ISO-2 country code
165  /// corresponding to the flag that the player wishes to take. It may also be
166  /// one of the special flag codes for non-country flags.
167  ///
168  /// If the flag code passed in is not one of the ones for which there is a
169  /// known (to the server) flag, then the player will be assigned to UN flag
170  /// (in the official server).
171  pub flag: BString,
172}
173
174/// Send a key update for the client.
175///
176/// Notes:
177/// - `seq` should be monotonically increasing with every key press.
178/// - `state`: `true` -> pressed, `false` -> released.
179#[derive(Copy, Clone, Debug)]
180#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
181pub struct Key {
182  pub seq: u32,
183  pub key: KeyCode,
184  pub state: bool,
185}
186
187/// Response packet to server [`Ping`](../server/struct.ping.html)s.
188#[derive(Copy, Clone, Debug)]
189#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
190pub struct Pong {
191  /// The ping number, should correspond to the `num` field within in the
192  /// [`Ping`](../server/ping.html) packet sent by the server.
193  pub num: u32,
194}
195
196/// Say a message in a chat bubble
197#[derive(Clone, Debug)]
198#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
199pub struct Say {
200  pub text: BString,
201}
202
203/// Send a message to your team.
204#[derive(Clone, Debug)]
205#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
206pub struct TeamChat {
207  pub text: BString,
208}
209
210/// Vote to mute a player
211#[derive(Copy, Clone, Debug)]
212#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
213pub struct VoteMute {
214  pub id: u16,
215}
216
217/// Send a whisper to another player.
218#[derive(Clone, Debug)]
219#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
220pub struct Whisper {
221  pub id: u16,
222  pub text: BString,
223}