rust_openttd_admin/packet/admin/server_packets.rs
1//! This module contains the definitions for the admin packets that can be
2//! sent by the server. All different types of packets are contained in the
3//! enum [`Packet`](crate::packet::admin::server_packets::Packet). Packets that contain extra information also
4//! have their own struct.
5
6use crate::types;
7use serde_derive::{Deserialize, Serialize};
8
9/// An error was caused by this admin connection (connection gets closed).
10#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone, Copy)]
11pub struct Error {
12 /// The error caused.
13 pub error_code: u8,
14}
15
16/// Describes an update packet the admin client can register for.
17#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone, Copy)]
18pub struct UpdatePacketDescription {
19 /// Update packet type.
20 pub packet_type: types::AdminUpdateType,
21 /// Frequencies allowed for this update packet (bitwise).
22 pub frequencies_allowed: types::UpdateFrequencies,
23}
24
25/// Inform a just joined admin about the protocol specifics.
26#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
27pub struct Protocol {
28 /// Protocol version.
29 pub version: u8,
30 /// Different update packet descriptions.
31 pub update_packets: Vec<UpdatePacketDescription>,
32}
33
34/// Welcome a connected admin to the game.
35#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
36pub struct Welcome {
37 /// Name of the Server (e.g. as advertised to master server).
38 pub server_name: String,
39 /// OpenTTD version string.
40 pub openttd_version: String,
41 /// Server is dedicated.
42 pub is_dedicated: bool,
43 /// Name of the Map.
44 pub map_name: String,
45 /// Random seed of the Map.
46 pub map_seed: u32,
47 /// Landscape of the Map.
48 pub map_landscape: u8,
49 /// Start date of the Map.
50 pub map_start_date: types::Date,
51 /// Map width.
52 pub map_width: u16,
53 /// Map height.
54 pub map_height: u16,
55}
56
57/// Send the current date of the game.
58#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
59pub struct Date {
60 /// Current game date.
61 pub date: types::Date,
62}
63
64/// Notification of a new client.
65#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
66pub struct ClientJoin {
67 /// ID of the new client.
68 pub id: u32,
69}
70
71/// Client information of a specific client.
72#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
73pub struct ClientInfo {
74 /// ID of the client.
75 pub id: u32,
76 /// Network address of the client.
77 pub address: String,
78 /// Name of the client.
79 pub name: String,
80 /// Language of the client.
81 pub language: u8,
82 /// Date the client joined the game.
83 pub date_joined: types::Date,
84 /// ID of the company the client is playing as (255 for spectators).
85 pub company_id: u8,
86}
87
88/// Client update details on a specific client (e.g. after rename or move).
89#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
90pub struct ClientUpdate {
91 /// ID of the client.
92 pub id: u32,
93 /// Name of the client.
94 pub name: String,
95 /// ID of the company the client is playing as (255 for spectators).
96 pub company_id: u8,
97}
98
99/// Notification about a client leaving the game.
100#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Copy, Clone)]
101pub struct ClientQuit {
102 /// ID of the client that just left.
103 pub id: u32,
104}
105
106/// Notification about a client error (and thus the clients disconnection).
107#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Copy, Clone)]
108pub struct ClientError {
109 /// ID of the client that made the error.
110 pub id: u32,
111 /// Error the client made (see NetworkErrorCode).
112 pub error: u8,
113}
114
115/// Notification of a new company.
116#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Copy, Clone)]
117pub struct CompanyNew {
118 /// ID of the new company.
119 pub id: u32,
120}
121
122#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
123pub struct CompanyInfo {
124 /// ID of the company.
125 pub id: u8,
126 /// Name of the company.
127 pub name: String,
128 /// Name of the companies manager.
129 pub manager: String,
130 /// Main company colour.
131 pub color: u8,
132 /// Company is password protected.
133 pub password_protected: bool,
134 /// Year the company was inaugurated.
135 pub inaugurated_year: u32,
136 /// Company is an AI.
137 pub ai: bool,
138}
139
140#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
141pub struct CompanyUpdate {
142 /// ID of the company.
143 pub id: u8,
144 /// Name of the company.
145 pub name: String,
146 // Name of the companies manager.
147 pub manager: String,
148 /// Main company colour.
149 pub color: u8,
150 /// Company is password protected.
151 pub password_protected: bool,
152 /// Quarters of bankruptcy.
153 pub quarters_bankrupt: u8,
154 /// Owner of share 1.
155 pub owner_share_1: u8,
156 /// Owner of share 2.
157 pub owner_share_2: u8,
158 /// Owner of share 3.
159 pub owner_share_3: u8,
160 /// Owner of share 4.
161 pub owner_share_4: u8,
162}
163
164/// Notification about a removed company (e.g. due to bankruptcy).
165#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Copy, Clone)]
166pub struct CompanyRemove {
167 /// ID of the company.
168 pub id: u8,
169 /// Reason for being removed (see #AdminCompanyRemoveReason).
170 pub reason: u8,
171}
172
173/// Economy update of a specific company.
174#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Copy, Clone)]
175pub struct CompanyEconomy {
176 /// ID of the company.
177 pub id: u8,
178 /// Money.
179 pub money: u64,
180 /// Loan.
181 pub loan: u64,
182 /// Income.
183 pub income: i64,
184 /// Delivered cargo (this quarter).
185 pub delivered_cargo: u16,
186 /// Company value (last quarter).
187 pub company_value_last: u64,
188 /// Performance (last quarter).
189 pub performance_last: u16,
190 /// Delivered cargo (last quarter).
191 pub delivered_cargo_last: u16,
192 /// Company value (previous quarter).
193 pub company_value_previous: u64,
194 /// Performance (previous quarter).
195 pub performance_previous: u16,
196 /// Delivered cargo (previous quarter).
197 pub delivered_previous: u16,
198}
199
200/// Company statistics on stations and vehicles.
201#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Copy, Clone)]
202pub struct CompanyStats {
203 /// ID of the company.
204 pub id: u8,
205 /// Number of trains.
206 pub trains: u16,
207 /// Number of lorries.
208 pub lorries: u16,
209 /// Number of busses.
210 pub busses: u16,
211 /// Number of planes.
212 pub planes: u16,
213 /// Number of ships.
214 pub ships: u16,
215 /// Number of train stations.
216 pub train_stations: u16,
217 /// Number of lorry stations.
218 pub lorry_stations: u16,
219 /// Number of bus stops.
220 pub bus_stops: u16,
221 /// Number of airports and heliports.
222 pub airports_and_heliports: u16,
223 /// Number of harbours.
224 pub harbours: u16,
225}
226
227/// Send chat from the game into the admin network.
228#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
229pub struct Chat {
230 /// Action such as NETWORK_ACTION_CHAT_CLIENT (see #NetworkAction).
231 pub action: u8,
232 /// Destination type such as DESTTYPE_BROADCAST (see #DestType).
233 pub destination: u8,
234 /// ID of the client who sent this message.
235 pub client: u32,
236 /// Message.
237 pub message: String,
238 /// Money (only when it is a 'give money' action).
239 pub money: Option<u64>,
240}
241
242/// Result of an rcon command.
243#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
244pub struct Rcon {
245 /// Colour as it would be used on the server or a client.
246 pub color: u16,
247 /// Output of the executed command.
248 pub output: String,
249}
250
251/// Send what would be printed on the server's console also into the admin network.
252#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
253pub struct Console {
254 /// The origin of the text, e.g. "console" for console, or "net" for network related (debug) messages.
255 pub origin: String,
256 /// Text as found on the console of the server.
257 pub text: String,
258}
259
260#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
261pub struct CmdName {
262 /// ID of the DoCommand.
263 pub id: u16,
264 /// Name of DoCommand.
265 pub name: String,
266}
267
268/// Send DoCommand names to the bot upon request only. Multiple of these
269/// packets can follow each other in order to provide all known DoCommand names.
270///
271/// NOTICE: Data provided with this packet is not stable and will not be
272/// treated as such. Do not rely on IDs or names to be constant across
273/// different versions / revisions of OpenTTD. Data provided in this packet is
274/// for logging purposes only.
275#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
276pub struct CmdNames {
277 pub names: Vec<CmdName>,
278}
279
280/// Send incoming command packets to the admin network. This is for logging
281/// purposes only.
282///
283/// NOTICE: Data provided with this packet is not stable and will not be
284/// across different versions / revisions of OpenTTD.
285/// Data provided in this packet is for logging purposes only.
286#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
287pub struct CmdLogging {
288 /// ID of the client sending the command.
289 pub client_id: u32,
290 /// ID of the company (0..MAX_COMPANIES-1).
291 pub company_id: u8,
292 /// ID of the command.
293 pub command_id: u16,
294 /// P1 (variable data passed to the command).
295 pub p1: u32,
296 /// P2 (variable data passed to the command).
297 pub p2: u32,
298 /// Tile where this is taking place.
299 pub tile: u32,
300 /// Text passed to the command.
301 pub text: String,
302 /// Frame of execution.
303 pub execution_frame: u32,
304}
305
306/// Send a JSON string to the current active GameScript.
307#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
308pub struct Gamescript {
309 /// JSON string for the GameScript.
310 pub json: String,
311}
312
313/// Notify the admin connection that the rcon command has finished.
314#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
315pub struct RconEnd {
316 /// The command as requested by the admin connection.
317 pub command: String,
318}
319
320/// Send a ping-reply (pong) to the admin that sent us the ping packet.
321#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
322pub struct Pong {
323 /// Should be the same as read from the admins ping packet.
324 pub id: u32,
325}
326
327#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug)]
328pub enum Packet {
329 /// The server is full (connection gets closed).
330 Full,
331 /// The source IP address is banned (connection gets closed).
332 Banned,
333 /// An error was caused by this admin connection (connection gets closed).
334 Error(Error),
335 /// Inform a just joined admin about the protocol specifics.
336 Protocol(Protocol),
337 /// Welcome a connected admin to the game.
338 Welcome(Welcome),
339 /// Notification about a newgame.
340 Newgame,
341 /// Notification about the server shutting down.
342 Shutdown,
343 /// Send the current date of the game.
344 Date(Date),
345 /// Notification of a new client.
346 ClientJoin(ClientJoin),
347 /// Client information of a specific client.
348 ClientInfo(ClientInfo),
349 /// Client update details on a specific client (e.g. after rename or move).
350 ClientUpdate(ClientUpdate),
351 /// Notification about a client leaving the game.
352 ClientQuit(ClientQuit),
353 /// Notification about a client error (and thus the clients disconnection).
354 ClientError(ClientError),
355 /// Notification of a new company.
356 CompanyNew(CompanyNew),
357 /// Company information on a specific company.
358 CompanyInfo(CompanyInfo),
359 /// Company information of a specific company.
360 CompanyUpdate(CompanyUpdate),
361 /// Notification about a removed company (e.g. due to bankruptcy).
362 CompanyRemove(CompanyRemove),
363 /// Economy update of a specific company.
364 CompanyEconomy(CompanyEconomy),
365 /// Company statistics on stations and vehicles.
366 CompanyStats(CompanyStats),
367 /// Send chat from the game into the admin network.
368 Chat(Chat),
369 /// Result of an rcon command.
370 Rcon(Rcon),
371 /// Send what would be printed on the server's console also into the admin network.
372 Console(Console),
373 CmdNames(CmdNames),
374 CmdLogging(CmdLogging),
375 Gamescript(Gamescript),
376 /// Notify the admin connection that the rcon command has finished.
377 RconEnd(RconEnd),
378 /// Send a ping-reply (pong) to the admin that sent us the ping packet.
379 Pong(Pong),
380 UnknownPacket {
381 packet_type: u8,
382 buffer: Vec<u8>,
383 },
384}