author | unC0Rr |
Thu, 19 Dec 2024 12:40:48 +0100 | |
branch | transitional_engine |
changeset 16079 | 624b74443b53 |
parent 15852 | ea459da15b30 |
permissions | -rw-r--r-- |
15826 | 1 |
use crate::types::{GameCfg, ServerVar, TeamInfo, VoteType}; |
2 |
use std::iter::once; |
|
3 |
||
4 |
#[derive(PartialEq, Eq, Clone, Debug)] |
|
5 |
pub enum HwProtocolMessage { |
|
6 |
// common messages |
|
7 |
Ping, |
|
8 |
Pong, |
|
9 |
Quit(Option<String>), |
|
10 |
Global(String), |
|
11 |
Watch(u32), |
|
12 |
ToggleServerRegisteredOnly, |
|
13 |
SuperPower, |
|
14 |
Info(String), |
|
15 |
// anteroom messages |
|
16 |
Nick(String), |
|
17 |
Proto(u16), |
|
18 |
Password(String, String), |
|
19 |
Checker(u16, String, String), |
|
20 |
// lobby messages |
|
21 |
List, |
|
22 |
Chat(String), |
|
23 |
CreateRoom(String, Option<String>), |
|
24 |
JoinRoom(String, Option<String>), |
|
25 |
Follow(String), |
|
26 |
Rnd(Vec<String>), |
|
27 |
Kick(String), |
|
28 |
Ban(String, String, u32), |
|
29 |
BanIp(String, String, u32), |
|
30 |
BanNick(String, String, u32), |
|
31 |
BanList, |
|
32 |
Unban(String), |
|
33 |
SetServerVar(ServerVar), |
|
34 |
GetServerVar, |
|
35 |
RestartServer, |
|
36 |
Stats, |
|
37 |
// room messages |
|
38 |
Part(Option<String>), |
|
39 |
Cfg(GameCfg), |
|
40 |
AddTeam(Box<TeamInfo>), |
|
41 |
RemoveTeam(String), |
|
42 |
SetHedgehogsNumber(String, u8), |
|
43 |
SetTeamColor(String, u8), |
|
44 |
ToggleReady, |
|
45 |
StartGame, |
|
46 |
EngineMessage(String), |
|
47 |
RoundFinished, |
|
48 |
ToggleRestrictJoin, |
|
49 |
ToggleRestrictTeams, |
|
50 |
ToggleRegisteredOnly, |
|
51 |
RoomName(String), |
|
52 |
Delegate(String), |
|
53 |
TeamChat(String), |
|
54 |
MaxTeams(u8), |
|
55 |
Fix, |
|
56 |
Unfix, |
|
57 |
Greeting(Option<String>), |
|
58 |
CallVote(Option<VoteType>), |
|
59 |
Vote(bool), |
|
60 |
ForceVote(bool), |
|
61 |
Save(String, String), |
|
62 |
Delete(String), |
|
63 |
SaveRoom(String), |
|
64 |
LoadRoom(String), |
|
15833
a855f32ab3ca
- Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents:
15832
diff
changeset
|
65 |
CheckerReady, |
a855f32ab3ca
- Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents:
15832
diff
changeset
|
66 |
CheckedOk(Vec<String>), |
a855f32ab3ca
- Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents:
15832
diff
changeset
|
67 |
CheckedFail(String), |
15826 | 68 |
} |
69 |
||
70 |
#[derive(Debug, Clone, Copy)] |
|
71 |
pub enum ProtocolFlags { |
|
72 |
InRoom, |
|
73 |
RoomMaster, |
|
74 |
Ready, |
|
75 |
InGame, |
|
76 |
Registered, |
|
77 |
Admin, |
|
78 |
Contributor, |
|
79 |
} |
|
80 |
||
81 |
impl ProtocolFlags { |
|
82 |
#[inline] |
|
83 |
fn flag_char(&self) -> char { |
|
84 |
match self { |
|
85 |
ProtocolFlags::InRoom => 'i', |
|
86 |
ProtocolFlags::RoomMaster => 'h', |
|
87 |
ProtocolFlags::Ready => 'r', |
|
88 |
ProtocolFlags::InGame => 'g', |
|
89 |
ProtocolFlags::Registered => 'u', |
|
90 |
ProtocolFlags::Admin => 'a', |
|
91 |
ProtocolFlags::Contributor => 'c', |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
#[inline] |
|
96 |
fn format(prefix: char, flags: &[ProtocolFlags]) -> String { |
|
97 |
once(prefix) |
|
98 |
.chain(flags.iter().map(|f| f.flag_char())) |
|
99 |
.collect() |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
#[inline] |
|
104 |
pub fn add_flags(flags: &[ProtocolFlags]) -> String { |
|
105 |
ProtocolFlags::format('+', flags) |
|
106 |
} |
|
107 |
||
108 |
#[inline] |
|
109 |
pub fn remove_flags(flags: &[ProtocolFlags]) -> String { |
|
110 |
ProtocolFlags::format('-', flags) |
|
111 |
} |
|
112 |
||
15832
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
113 |
#[derive(PartialEq, Eq, Clone, Debug)] |
15826 | 114 |
pub enum HwServerMessage { |
15832
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
115 |
Connected(String, u32), |
15826 | 116 |
Redirect(u16), |
117 |
||
118 |
Ping, |
|
119 |
Pong, |
|
120 |
Bye(String), |
|
121 |
||
122 |
Nick(String), |
|
123 |
Proto(u16), |
|
124 |
AskPassword(String), |
|
125 |
ServerAuth(String), |
|
126 |
LogonPassed, |
|
127 |
||
128 |
LobbyLeft(String, String), |
|
129 |
LobbyJoined(Vec<String>), |
|
130 |
ChatMsg { nick: String, msg: String }, |
|
131 |
ClientFlags(String, Vec<String>), |
|
132 |
Rooms(Vec<String>), |
|
133 |
RoomAdd(Vec<String>), |
|
134 |
RoomJoined(Vec<String>), |
|
135 |
RoomLeft(String, String), |
|
136 |
RoomRemove(String), |
|
137 |
RoomUpdated(String, Vec<String>), |
|
138 |
Joining(String), |
|
139 |
TeamAdd(Vec<String>), |
|
140 |
TeamRemove(String), |
|
141 |
TeamAccepted(String), |
|
142 |
TeamColor(String, u8), |
|
143 |
HedgehogsNumber(String, u8), |
|
144 |
ConfigEntry(String, Vec<String>), |
|
145 |
Kicked, |
|
146 |
RunGame, |
|
147 |
ForwardEngineMessage(Vec<String>), |
|
148 |
RoundFinished, |
|
149 |
ReplayStart, |
|
150 |
||
151 |
Info(Vec<String>), |
|
152 |
ServerMessage(String), |
|
153 |
ServerVars(Vec<String>), |
|
154 |
Notice(String), |
|
155 |
Warning(String), |
|
156 |
Error(String), |
|
157 |
||
15833
a855f32ab3ca
- Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents:
15832
diff
changeset
|
158 |
Replay(Vec<String>), |
a855f32ab3ca
- Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents:
15832
diff
changeset
|
159 |
|
15826 | 160 |
//Deprecated messages |
161 |
LegacyReady(bool, Vec<String>), |
|
162 |
} |
|
163 |
||
164 |
fn special_chat(nick: &str, msg: String) -> HwServerMessage { |
|
165 |
HwServerMessage::ChatMsg { |
|
166 |
nick: nick.to_string(), |
|
167 |
msg, |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
pub fn server_chat(msg: String) -> HwServerMessage { |
|
172 |
special_chat("[server]", msg) |
|
173 |
} |
|
174 |
||
175 |
pub fn global_chat(msg: String) -> HwServerMessage { |
|
176 |
special_chat("(global notice)", msg) |
|
177 |
} |
|
178 |
||
179 |
impl ServerVar { |
|
180 |
pub fn to_protocol(&self) -> Vec<String> { |
|
181 |
use ServerVar::*; |
|
182 |
match self { |
|
183 |
MOTDNew(s) => vec!["MOTD_NEW".to_string(), s.clone()], |
|
184 |
MOTDOld(s) => vec!["MOTD_OLD".to_string(), s.clone()], |
|
185 |
LatestProto(n) => vec!["LATEST_PROTO".to_string(), n.to_string()], |
|
186 |
} |
|
187 |
} |
|
188 |
} |
|
189 |
||
190 |
impl VoteType { |
|
191 |
pub fn to_protocol(&self) -> Vec<String> { |
|
192 |
use VoteType::*; |
|
193 |
match self { |
|
194 |
Kick(nick) => vec!["KICK".to_string(), nick.clone()], |
|
195 |
Map(None) => vec!["MAP".to_string()], |
|
196 |
Map(Some(name)) => vec!["MAP".to_string(), name.clone()], |
|
197 |
Pause => vec!["PAUSE".to_string()], |
|
198 |
NewSeed => vec!["NEWSEED".to_string()], |
|
199 |
HedgehogsPerTeam(count) => vec!["HEDGEHOGS".to_string(), count.to_string()], |
|
200 |
} |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
impl GameCfg { |
|
205 |
pub fn to_protocol(&self) -> (String, Vec<String>) { |
|
206 |
use GameCfg::*; |
|
207 |
match self { |
|
208 |
FeatureSize(s) => ("FEATURE_SIZE".to_string(), vec![s.to_string()]), |
|
209 |
MapType(t) => ("MAP".to_string(), vec![t.to_string()]), |
|
210 |
MapGenerator(g) => ("MAPGEN".to_string(), vec![g.to_string()]), |
|
211 |
MazeSize(s) => ("MAZE_SIZE".to_string(), vec![s.to_string()]), |
|
212 |
Seed(s) => ("SEED".to_string(), vec![s.to_string()]), |
|
213 |
Template(t) => ("TEMPLATE".to_string(), vec![t.to_string()]), |
|
214 |
||
215 |
Ammo(n, None) => ("AMMO".to_string(), vec![n.to_string()]), |
|
216 |
Ammo(n, Some(s)) => ("AMMO".to_string(), vec![n.to_string(), s.to_string()]), |
|
217 |
Scheme(n, s) if s.is_empty() => ("SCHEME".to_string(), vec![n.to_string()]), |
|
218 |
Scheme(n, s) => ("SCHEME".to_string(), { |
|
219 |
let mut v = vec![n.to_string()]; |
|
220 |
v.extend(s.clone()); |
|
221 |
v |
|
222 |
}), |
|
223 |
Script(s) => ("SCRIPT".to_string(), vec![s.to_string()]), |
|
224 |
Theme(t) => ("THEME".to_string(), vec![t.to_string()]), |
|
225 |
DrawnMap(m) => ("DRAWNMAP".to_string(), vec![m.to_string()]), |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
pub fn to_server_msg(&self) -> HwServerMessage { |
|
230 |
let (name, args) = self.to_protocol(); |
|
231 |
HwServerMessage::ConfigEntry(name, args) |
|
232 |
} |
|
233 |
} |
|
234 |
||
235 |
impl TeamInfo { |
|
236 |
pub fn to_protocol(&self) -> Vec<String> { |
|
237 |
let mut info = vec![ |
|
238 |
self.name.clone(), |
|
239 |
self.grave.clone(), |
|
240 |
self.fort.clone(), |
|
241 |
self.voice_pack.clone(), |
|
242 |
self.flag.clone(), |
|
243 |
self.owner.clone(), |
|
244 |
self.difficulty.to_string(), |
|
245 |
]; |
|
246 |
let hogs = self |
|
247 |
.hedgehogs |
|
248 |
.iter() |
|
249 |
.flat_map(|h| once(h.name.clone()).chain(once(h.hat.clone()))); |
|
250 |
info.extend(hogs); |
|
251 |
info |
|
252 |
} |
|
253 |
} |
|
254 |
||
255 |
macro_rules! const_braces { |
|
256 |
($e: expr) => { |
|
257 |
"{}\n" |
|
258 |
}; |
|
259 |
} |
|
260 |
||
261 |
macro_rules! msg { |
|
262 |
[$($part: expr),*] => { |
|
15852 | 263 |
format!(concat!($(const_braces!($part)),*, "\n"), $($part),*) |
15826 | 264 |
}; |
265 |
} |
|
266 |
||
267 |
impl HwProtocolMessage { |
|
268 |
/** Converts the message to a raw `String`, which can be sent over the network. |
|
269 |
* |
|
270 |
* This is the inverse of the `message` parser. |
|
271 |
*/ |
|
272 |
pub fn to_raw_protocol(&self) -> String { |
|
273 |
use self::HwProtocolMessage::*; |
|
274 |
match self { |
|
275 |
Ping => msg!["PING"], |
|
276 |
Pong => msg!["PONG"], |
|
277 |
Quit(None) => msg!["QUIT"], |
|
278 |
Quit(Some(msg)) => msg!["QUIT", msg], |
|
279 |
Global(msg) => msg!["CMD", format!("GLOBAL {}", msg)], |
|
280 |
Watch(name) => msg!["CMD", format!("WATCH {}", name)], |
|
281 |
ToggleServerRegisteredOnly => msg!["CMD", "REGISTERED_ONLY"], |
|
282 |
SuperPower => msg!["CMD", "SUPER_POWER"], |
|
283 |
Info(info) => msg!["CMD", format!("INFO {}", info)], |
|
284 |
Nick(nick) => msg!("NICK", nick), |
|
285 |
Proto(version) => msg!["PROTO", version], |
|
286 |
Password(p, s) => msg!["PASSWORD", p, s], |
|
287 |
Checker(i, n, p) => msg!["CHECKER", i, n, p], |
|
288 |
List => msg!["LIST"], |
|
289 |
Chat(msg) => msg!["CHAT", msg], |
|
290 |
CreateRoom(name, None) => msg!["CREATE_ROOM", name], |
|
291 |
CreateRoom(name, Some(password)) => msg!["CREATE_ROOM", name, password], |
|
292 |
JoinRoom(name, None) => msg!["JOIN_ROOM", name], |
|
293 |
JoinRoom(name, Some(password)) => msg!["JOIN_ROOM", name, password], |
|
294 |
Follow(name) => msg!["FOLLOW", name], |
|
295 |
Rnd(args) => { |
|
296 |
if args.is_empty() { |
|
297 |
msg!["CMD", "RND"] |
|
298 |
} else { |
|
299 |
msg!["CMD", format!("RND {}", args.join(" "))] |
|
300 |
} |
|
301 |
} |
|
302 |
Kick(name) => msg!["KICK", name], |
|
303 |
Ban(name, reason, time) => msg!["BAN", name, reason, time], |
|
304 |
BanIp(ip, reason, time) => msg!["BAN_IP", ip, reason, time], |
|
305 |
BanNick(nick, reason, time) => msg!("BAN_NICK", nick, reason, time), |
|
306 |
BanList => msg!["BANLIST"], |
|
307 |
Unban(name) => msg!["UNBAN", name], |
|
308 |
SetServerVar(var) => construct_message(&["SET_SERVER_VAR"], &var.to_protocol()), |
|
309 |
GetServerVar => msg!["GET_SERVER_VAR"], |
|
310 |
RestartServer => msg!["CMD", "RESTART_SERVER YES"], |
|
311 |
Stats => msg!["CMD", "STATS"], |
|
312 |
Part(None) => msg!["PART"], |
|
313 |
Part(Some(msg)) => msg!["PART", msg], |
|
314 |
Cfg(config) => { |
|
315 |
let (name, args) = config.to_protocol(); |
|
316 |
msg!["CFG", name, args.join("\n")] |
|
317 |
} |
|
318 |
AddTeam(info) => msg![ |
|
319 |
"ADD_TEAM", |
|
320 |
info.name, |
|
321 |
info.color, |
|
322 |
info.grave, |
|
323 |
info.fort, |
|
324 |
info.voice_pack, |
|
325 |
info.flag, |
|
326 |
info.difficulty, |
|
327 |
&(info.hedgehogs.iter()) |
|
328 |
.flat_map(|h| [&h.name[..], &h.hat[..]]) |
|
329 |
.collect::<Vec<_>>() |
|
330 |
.join("\n") |
|
331 |
], |
|
332 |
RemoveTeam(name) => msg!["REMOVE_TEAM", name], |
|
333 |
SetHedgehogsNumber(team, number) => msg!["HH_NUM", team, number], |
|
334 |
SetTeamColor(team, color) => msg!["TEAM_COLOR", team, color], |
|
335 |
ToggleReady => msg!["TOGGLE_READY"], |
|
336 |
StartGame => msg!["START_GAME"], |
|
337 |
EngineMessage(msg) => msg!["EM", msg], |
|
338 |
RoundFinished => msg!["ROUNDFINISHED"], |
|
339 |
ToggleRestrictJoin => msg!["TOGGLE_RESTRICT_JOINS"], |
|
340 |
ToggleRestrictTeams => msg!["TOGGLE_RESTRICT_TEAMS"], |
|
341 |
ToggleRegisteredOnly => msg!["TOGGLE_REGISTERED_ONLY"], |
|
342 |
RoomName(name) => msg!["ROOM_NAME", name], |
|
343 |
Delegate(name) => msg!["CMD", format!("DELEGATE {}", name)], |
|
344 |
TeamChat(msg) => msg!["TEAMCHAT", msg], |
|
345 |
MaxTeams(count) => msg!["CMD", format!("MAXTEAMS {}", count)], |
|
346 |
Fix => msg!["CMD", "FIX"], |
|
347 |
Unfix => msg!["CMD", "UNFIX"], |
|
348 |
Greeting(None) => msg!["CMD", "GREETING"], |
|
349 |
Greeting(Some(msg)) => msg!["CMD", format!("GREETING {}", msg)], |
|
350 |
CallVote(None) => msg!["CMD", "CALLVOTE"], |
|
351 |
CallVote(Some(vote)) => { |
|
352 |
msg!["CMD", format!("CALLVOTE {}", &vote.to_protocol().join(" "))] |
|
353 |
} |
|
354 |
Vote(msg) => msg!["CMD", format!("VOTE {}", if *msg { "YES" } else { "NO" })], |
|
355 |
ForceVote(msg) => msg!["CMD", format!("FORCE {}", if *msg { "YES" } else { "NO" })], |
|
356 |
Save(name, location) => msg!["CMD", format!("SAVE {} {}", name, location)], |
|
357 |
Delete(name) => msg!["CMD", format!("DELETE {}", name)], |
|
358 |
SaveRoom(name) => msg!["CMD", format!("SAVEROOM {}", name)], |
|
359 |
LoadRoom(name) => msg!["CMD", format!("LOADROOM {}", name)], |
|
15833
a855f32ab3ca
- Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents:
15832
diff
changeset
|
360 |
CheckerReady => msg!["READY"], |
a855f32ab3ca
- Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents:
15832
diff
changeset
|
361 |
CheckedOk(args) => msg!["CHECKED", "OK", args.join("\n")], |
a855f32ab3ca
- Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents:
15832
diff
changeset
|
362 |
CheckedFail(message) => msg!["CHECKED", "FAIL", message], |
15826 | 363 |
} |
364 |
} |
|
365 |
} |
|
366 |
||
367 |
fn construct_message(header: &[&str], msg: &[String]) -> String { |
|
368 |
let mut v: Vec<_> = header.iter().cloned().collect(); |
|
369 |
v.extend(msg.iter().map(|s| &s[..])); |
|
370 |
v.push("\n"); |
|
371 |
v.join("\n") |
|
372 |
} |
|
373 |
||
374 |
impl HwServerMessage { |
|
375 |
pub fn to_raw_protocol(&self) -> String { |
|
376 |
use self::HwServerMessage::*; |
|
377 |
match self { |
|
378 |
Ping => msg!["PING"], |
|
379 |
Pong => msg!["PONG"], |
|
15832
ee84e417d8d0
Add parser and idempotention tests for server messages
unc0rr
parents:
15826
diff
changeset
|
380 |
Connected(message, protocol_version) => msg!["CONNECTED", message, protocol_version], |
15826 | 381 |
Redirect(port) => msg!["REDIRECT", port], |
382 |
Bye(msg) => msg!["BYE", msg], |
|
383 |
Nick(nick) => msg!["NICK", nick], |
|
384 |
Proto(proto) => msg!["PROTO", proto], |
|
385 |
AskPassword(salt) => msg!["ASKPASSWORD", salt], |
|
386 |
ServerAuth(hash) => msg!["SERVER_AUTH", hash], |
|
387 |
LogonPassed => msg!["LOGONPASSED"], |
|
388 |
LobbyLeft(nick, msg) => msg!["LOBBY:LEFT", nick, msg], |
|
389 |
LobbyJoined(nicks) => construct_message(&["LOBBY:JOINED"], &nicks), |
|
390 |
ClientFlags(flags, nicks) => construct_message(&["CLIENT_FLAGS", flags], &nicks), |
|
391 |
Rooms(info) => construct_message(&["ROOMS"], &info), |
|
392 |
RoomAdd(info) => construct_message(&["ROOM", "ADD"], &info), |
|
393 |
RoomJoined(nicks) => construct_message(&["JOINED"], &nicks), |
|
394 |
RoomLeft(nick, msg) => msg!["LEFT", nick, msg], |
|
395 |
RoomRemove(name) => msg!["ROOM", "DEL", name], |
|
396 |
RoomUpdated(name, info) => construct_message(&["ROOM", "UPD", name], &info), |
|
397 |
Joining(name) => msg!["JOINING", name], |
|
398 |
TeamAdd(info) => construct_message(&["ADD_TEAM"], &info), |
|
399 |
TeamRemove(name) => msg!["REMOVE_TEAM", name], |
|
400 |
TeamAccepted(name) => msg!["TEAM_ACCEPTED", name], |
|
401 |
TeamColor(name, color) => msg!["TEAM_COLOR", name, color], |
|
402 |
HedgehogsNumber(name, number) => msg!["HH_NUM", name, number], |
|
403 |
ConfigEntry(name, values) => construct_message(&["CFG", name], &values), |
|
404 |
Kicked => msg!["KICKED"], |
|
405 |
RunGame => msg!["RUN_GAME"], |
|
406 |
ForwardEngineMessage(em) => construct_message(&["EM"], &em), |
|
407 |
RoundFinished => msg!["ROUND_FINISHED"], |
|
408 |
ChatMsg { nick, msg } => msg!["CHAT", nick, msg], |
|
409 |
Info(info) => construct_message(&["INFO"], &info), |
|
410 |
ServerMessage(msg) => msg!["SERVER_MESSAGE", msg], |
|
411 |
ServerVars(vars) => construct_message(&["SERVER_VARS"], &vars), |
|
412 |
Notice(msg) => msg!["NOTICE", msg], |
|
413 |
Warning(msg) => msg!["WARNING", msg], |
|
414 |
Error(msg) => msg!["ERROR", msg], |
|
415 |
ReplayStart => msg!["REPLAY_START"], |
|
15833
a855f32ab3ca
- Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents:
15832
diff
changeset
|
416 |
Replay(em) => construct_message(&["REPLAY"], &em), |
15826 | 417 |
|
418 |
LegacyReady(is_ready, nicks) => { |
|
419 |
construct_message(&[if *is_ready { "READY" } else { "NOT_READY" }], &nicks) |
|
420 |
} |
|
421 |
} |
|
422 |
} |
|
423 |
} |