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