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