author | alfadur |
Tue, 28 May 2019 19:04:18 +0300 | |
changeset 15095 | c5a6e8566425 |
parent 14928 | c156273b57de |
child 15096 | e935b1ad23f3 |
permissions | -rw-r--r-- |
15095 | 1 |
use crate::core::types::{GameCfg, HedgehogInfo, ServerVar, TeamInfo, VoteType}; |
14478 | 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)] |
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
5 |
pub enum HWProtocolMessage { |
14718 | 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), |
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
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), |
14718 | 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), |
13486 | 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), |
13771 | 19 |
Checker(u16, String, String), |
14718 | 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), |
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
29 |
BanIP(String, String, u32), |
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, |
|
14718 | 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), |
13500 | 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, |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
57 |
Greeting(String), |
13450 | 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 |
|
14812 | 67 |
#[derive(Debug, Clone, Copy)] |
14803 | 68 |
pub enum ProtocolFlags { |
69 |
InRoom, |
|
70 |
RoomMaster, |
|
71 |
Ready, |
|
72 |
InGame, |
|
14812 | 73 |
Registered, |
14803 | 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', |
|
14812 | 86 |
ProtocolFlags::Registered => 'u', |
14803 | 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)] |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
111 |
pub enum HWServerMessage { |
14856 | 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), |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14718
diff
changeset
|
118 |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
119 |
Nick(String), |
13486 | 120 |
Proto(u16), |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14718
diff
changeset
|
121 |
AskPassword(String), |
14804 | 122 |
ServerAuth(String), |
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14718
diff
changeset
|
123 |
|
13416 | 124 |
LobbyLeft(String, String), |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
125 |
LobbyJoined(Vec<String>), |
14478 | 126 |
ChatMsg { nick: String, msg: String }, |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
127 |
ClientFlags(String, Vec<String>), |
13416 | 128 |
Rooms(Vec<String>), |
129 |
RoomAdd(Vec<String>), |
|
130 |
RoomJoined(Vec<String>), |
|
131 |
RoomLeft(String, String), |
|
132 |
RoomRemove(String), |
|
133 |
RoomUpdated(String, Vec<String>), |
|
14808 | 134 |
Joining(String), |
13419 | 135 |
TeamAdd(Vec<String>), |
136 |
TeamRemove(String), |
|
137 |
TeamAccepted(String), |
|
138 |
TeamColor(String, u8), |
|
139 |
HedgehogsNumber(String, u8), |
|
13422 | 140 |
ConfigEntry(String, Vec<String>), |
13450 | 141 |
Kicked, |
13423 | 142 |
RunGame, |
13428 | 143 |
ForwardEngineMessage(Vec<String>), |
13423 | 144 |
RoundFinished, |
14928 | 145 |
ReplayStart, |
13419 | 146 |
|
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
147 |
Info(Vec<String>), |
13416 | 148 |
ServerMessage(String), |
14804 | 149 |
ServerVars(Vec<String>), |
13775 | 150 |
Notice(String), |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
151 |
Warning(String), |
13416 | 152 |
Error(String), |
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
153 |
Unreachable, |
13775 | 154 |
|
155 |
//Deprecated messages |
|
14478 | 156 |
LegacyReady(bool, Vec<String>), |
12131 | 157 |
} |
12136 | 158 |
|
14805 | 159 |
fn special_chat(nick: &str, msg: String) -> HWServerMessage { |
14478 | 160 |
HWServerMessage::ChatMsg { |
14805 | 161 |
nick: nick.to_string(), |
14478 | 162 |
msg, |
163 |
} |
|
13450 | 164 |
} |
165 |
||
14805 | 166 |
pub fn server_chat(msg: String) -> HWServerMessage { |
167 |
special_chat("[server]", msg) |
|
168 |
} |
|
169 |
||
170 |
pub fn global_chat(msg: String) -> HWServerMessage { |
|
171 |
special_chat("(global notice)", msg) |
|
172 |
} |
|
173 |
||
14804 | 174 |
impl ServerVar { |
175 |
pub fn to_protocol(&self) -> Vec<String> { |
|
176 |
match self { |
|
177 |
ServerVar::MOTDNew(s) => vec!["MOTD_NEW".to_string(), s.clone()], |
|
178 |
ServerVar::MOTDOld(s) => vec!["MOTD_OLD".to_string(), s.clone()], |
|
179 |
ServerVar::LatestProto(n) => vec!["LATEST_PROTO".to_string(), n.to_string()], |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
||
13422 | 184 |
impl GameCfg { |
13439 | 185 |
pub fn to_protocol(&self) -> (String, Vec<String>) { |
15095 | 186 |
use crate::core::types::GameCfg::*; |
13422 | 187 |
match self { |
13439 | 188 |
FeatureSize(s) => ("FEATURE_SIZE".to_string(), vec![s.to_string()]), |
189 |
MapType(t) => ("MAP".to_string(), vec![t.to_string()]), |
|
190 |
MapGenerator(g) => ("MAPGEN".to_string(), vec![g.to_string()]), |
|
191 |
MazeSize(s) => ("MAZE_SIZE".to_string(), vec![s.to_string()]), |
|
192 |
Seed(s) => ("SEED".to_string(), vec![s.to_string()]), |
|
193 |
Template(t) => ("TEMPLATE".to_string(), vec![t.to_string()]), |
|
13422 | 194 |
|
13439 | 195 |
Ammo(n, None) => ("AMMO".to_string(), vec![n.to_string()]), |
196 |
Ammo(n, Some(s)) => ("AMMO".to_string(), vec![n.to_string(), s.to_string()]), |
|
13775 | 197 |
Scheme(n, s) if s.is_empty() => ("SCHEME".to_string(), vec![n.to_string()]), |
198 |
Scheme(n, s) => ("SCHEME".to_string(), { |
|
13422 | 199 |
let mut v = vec![n.to_string()]; |
13439 | 200 |
v.extend(s.clone().into_iter()); |
13422 | 201 |
v |
202 |
}), |
|
13439 | 203 |
Script(s) => ("SCRIPT".to_string(), vec![s.to_string()]), |
204 |
Theme(t) => ("THEME".to_string(), vec![t.to_string()]), |
|
14478 | 205 |
DrawnMap(m) => ("DRAWNMAP".to_string(), vec![m.to_string()]), |
13422 | 206 |
} |
207 |
} |
|
13439 | 208 |
|
209 |
pub fn to_server_msg(&self) -> HWServerMessage { |
|
210 |
use self::HWServerMessage::ConfigEntry; |
|
211 |
let (name, args) = self.to_protocol(); |
|
212 |
HWServerMessage::ConfigEntry(name, args) |
|
213 |
} |
|
13422 | 214 |
} |
215 |
||
14806
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
216 |
impl TeamInfo { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
217 |
pub fn to_protocol(&self) -> Vec<String> { |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
218 |
let mut info = vec![ |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
219 |
self.name.clone(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
220 |
self.grave.clone(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
221 |
self.fort.clone(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
222 |
self.voice_pack.clone(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
223 |
self.flag.clone(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
224 |
self.owner.clone(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
225 |
self.difficulty.to_string(), |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
226 |
]; |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
227 |
let hogs = self |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
228 |
.hedgehogs |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
229 |
.iter() |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
230 |
.flat_map(|h| once(h.name.clone()).chain(once(h.hat.clone()))); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
231 |
info.extend(hogs); |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
232 |
info |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
233 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
234 |
} |
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14805
diff
changeset
|
235 |
|
13416 | 236 |
macro_rules! const_braces { |
14478 | 237 |
($e: expr) => { |
238 |
"{}\n" |
|
239 |
}; |
|
13416 | 240 |
} |
241 |
||
242 |
macro_rules! msg { |
|
243 |
[$($part: expr),*] => { |
|
244 |
format!(concat!($(const_braces!($part)),*, "\n"), $($part),*); |
|
245 |
}; |
|
246 |
} |
|
247 |
||
13713 | 248 |
#[cfg(test)] |
13439 | 249 |
macro_rules! several { |
250 |
[$part: expr] => { once($part) }; |
|
251 |
[$part: expr, $($other: expr),*] => { once($part).chain(several![$($other),*]) }; |
|
252 |
} |
|
253 |
||
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
|
254 |
impl HWProtocolMessage { |
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
|
255 |
/** 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
|
256 |
* |
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
|
257 |
* 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
|
258 |
*/ |
13713 | 259 |
#[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
|
260 |
pub(crate) fn to_raw_protocol(&self) -> String { |
13432 | 261 |
use self::HWProtocolMessage::*; |
262 |
match self { |
|
263 |
Ping => msg!["PING"], |
|
264 |
Pong => msg!["PONG"], |
|
265 |
Quit(None) => msg!["QUIT"], |
|
266 |
Quit(Some(msg)) => msg!["QUIT", msg], |
|
267 |
Global(msg) => msg!["CMD", format!("GLOBAL {}", msg)], |
|
268 |
Watch(name) => msg!["CMD", format!("WATCH {}", name)], |
|
269 |
ToggleServerRegisteredOnly => msg!["CMD", "REGISTERED_ONLY"], |
|
14478 | 270 |
SuperPower => msg!["CMD", "SUPER_POWER"], |
13432 | 271 |
Info(info) => msg!["CMD", format!("INFO {}", info)], |
272 |
Nick(nick) => msg!("NICK", nick), |
|
273 |
Proto(version) => msg!["PROTO", version], |
|
274 |
Password(p, s) => msg!["PASSWORD", p, s], |
|
275 |
Checker(i, n, p) => msg!["CHECKER", i, n, p], |
|
276 |
List => msg!["LIST"], |
|
277 |
Chat(msg) => msg!["CHAT", msg], |
|
278 |
CreateRoom(name, None) => msg!["CREATE_ROOM", name], |
|
14478 | 279 |
CreateRoom(name, Some(password)) => msg!["CREATE_ROOM", name, password], |
13432 | 280 |
JoinRoom(name, None) => msg!["JOIN_ROOM", name], |
14478 | 281 |
JoinRoom(name, Some(password)) => msg!["JOIN_ROOM", name, password], |
13432 | 282 |
Follow(name) => msg!["FOLLOW", name], |
14478 | 283 |
Rnd(args) => { |
284 |
if args.is_empty() { |
|
285 |
msg!["CMD", "RND"] |
|
286 |
} else { |
|
287 |
msg!["CMD", format!("RND {}", args.join(" "))] |
|
288 |
} |
|
289 |
} |
|
13432 | 290 |
Kick(name) => msg!["KICK", name], |
291 |
Ban(name, reason, time) => msg!["BAN", name, reason, time], |
|
292 |
BanIP(ip, reason, time) => msg!["BAN_IP", ip, reason, time], |
|
14478 | 293 |
BanNick(nick, reason, time) => msg!("BAN_NICK", nick, reason, time), |
13432 | 294 |
BanList => msg!["BANLIST"], |
295 |
Unban(name) => msg!["UNBAN", name], |
|
14804 | 296 |
SetServerVar(var) => construct_message(&["SET_SERVER_VAR"], &var.to_protocol()), |
13432 | 297 |
GetServerVar => msg!["GET_SERVER_VAR"], |
298 |
RestartServer => msg!["CMD", "RESTART_SERVER YES"], |
|
299 |
Stats => msg!["CMD", "STATS"], |
|
300 |
Part(None) => msg!["PART"], |
|
301 |
Part(Some(msg)) => msg!["PART", msg], |
|
13439 | 302 |
Cfg(config) => { |
303 |
let (name, args) = config.to_protocol(); |
|
304 |
msg!["CFG", name, args.join("\n")] |
|
14478 | 305 |
} |
306 |
AddTeam(info) => msg![ |
|
307 |
"ADD_TEAM", |
|
308 |
info.name, |
|
309 |
info.color, |
|
310 |
info.grave, |
|
311 |
info.fort, |
|
312 |
info.voice_pack, |
|
313 |
info.flag, |
|
314 |
info.difficulty, |
|
315 |
info.hedgehogs |
|
316 |
.iter() |
|
317 |
.flat_map(|h| several![&h.name[..], &h.hat[..]]) |
|
318 |
.collect::<Vec<_>>() |
|
319 |
.join("\n") |
|
320 |
], |
|
13432 | 321 |
RemoveTeam(name) => msg!["REMOVE_TEAM", name], |
13439 | 322 |
SetHedgehogsNumber(team, number) => msg!["HH_NUM", team, number], |
323 |
SetTeamColor(team, color) => msg!["TEAM_COLOR", team, color], |
|
13432 | 324 |
ToggleReady => msg!["TOGGLE_READY"], |
325 |
StartGame => msg!["START_GAME"], |
|
326 |
EngineMessage(msg) => msg!["EM", msg], |
|
327 |
RoundFinished => msg!["ROUNDFINISHED"], |
|
14928 | 328 |
ReplayStart => msg!["REPLAY_START"], |
13432 | 329 |
ToggleRestrictJoin => msg!["TOGGLE_RESTRICT_JOINS"], |
330 |
ToggleRestrictTeams => msg!["TOGGLE_RESTRICT_TEAMS"], |
|
331 |
ToggleRegisteredOnly => msg!["TOGGLE_REGISTERED_ONLY"], |
|
332 |
RoomName(name) => msg!["ROOM_NAME", name], |
|
333 |
Delegate(name) => msg!["CMD", format!("DELEGATE {}", name)], |
|
334 |
TeamChat(msg) => msg!["TEAMCHAT", msg], |
|
14478 | 335 |
MaxTeams(count) => msg!["CMD", format!("MAXTEAMS {}", count)], |
13432 | 336 |
Fix => msg!["CMD", "FIX"], |
337 |
Unfix => msg!["CMD", "UNFIX"], |
|
338 |
Greeting(msg) => msg!["CMD", format!("GREETING {}", msg)], |
|
339 |
//CallVote(Option<(String, Option<String>)>) =>, ?? |
|
14478 | 340 |
Vote(msg) => msg!["CMD", format!("VOTE {}", if *msg { "YES" } else { "NO" })], |
341 |
ForceVote(msg) => msg!["CMD", format!("FORCE {}", if *msg { "YES" } else { "NO" })], |
|
13528 | 342 |
Save(name, location) => msg!["CMD", format!("SAVE {} {}", name, location)], |
343 |
Delete(name) => msg!["CMD", format!("DELETE {}", name)], |
|
344 |
SaveRoom(name) => msg!["CMD", format!("SAVEROOM {}", name)], |
|
345 |
LoadRoom(name) => msg!["CMD", format!("LOADROOM {}", name)], |
|
14478 | 346 |
_ => panic!("Protocol message not yet implemented"), |
13432 | 347 |
} |
348 |
} |
|
349 |
} |
|
350 |
||
13500 | 351 |
fn construct_message(header: &[&str], msg: &[String]) -> String { |
14371 | 352 |
let mut v: Vec<_> = header.iter().cloned().collect(); |
13419 | 353 |
v.extend(msg.iter().map(|s| &s[..])); |
354 |
v.push("\n"); |
|
355 |
v.join("\n") |
|
13416 | 356 |
} |
357 |
||
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
358 |
impl HWServerMessage { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
359 |
pub fn to_raw_protocol(&self) -> String { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12147
diff
changeset
|
360 |
use self::HWServerMessage::*; |
12136 | 361 |
match self { |
13416 | 362 |
Ping => msg!["PING"], |
363 |
Pong => msg!["PONG"], |
|
364 |
Connected(protocol_version) => msg![ |
|
365 |
"CONNECTED", |
|
13667 | 366 |
"Hedgewars server https://www.hedgewars.org/", |
14478 | 367 |
protocol_version |
368 |
], |
|
14856 | 369 |
Redirect(port) => msg!["REDIRECT", port], |
13416 | 370 |
Bye(msg) => msg!["BYE", msg], |
371 |
Nick(nick) => msg!["NICK", nick], |
|
372 |
Proto(proto) => msg!["PROTO", proto], |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14718
diff
changeset
|
373 |
AskPassword(salt) => msg!["ASKPASSWORD", salt], |
13774 | 374 |
ServerAuth(hash) => msg!["SERVER_AUTH", hash], |
13416 | 375 |
LobbyLeft(nick, msg) => msg!["LOBBY:LEFT", nick, msg], |
14478 | 376 |
LobbyJoined(nicks) => construct_message(&["LOBBY:JOINED"], &nicks), |
377 |
ClientFlags(flags, nicks) => construct_message(&["CLIENT_FLAGS", flags], &nicks), |
|
378 |
Rooms(info) => construct_message(&["ROOMS"], &info), |
|
379 |
RoomAdd(info) => construct_message(&["ROOM", "ADD"], &info), |
|
380 |
RoomJoined(nicks) => construct_message(&["JOINED"], &nicks), |
|
13416 | 381 |
RoomLeft(nick, msg) => msg!["LEFT", nick, msg], |
382 |
RoomRemove(name) => msg!["ROOM", "DEL", name], |
|
14478 | 383 |
RoomUpdated(name, info) => construct_message(&["ROOM", "UPD", name], &info), |
14808 | 384 |
Joining(name) => msg!["JOINING", name], |
14478 | 385 |
TeamAdd(info) => construct_message(&["ADD_TEAM"], &info), |
13419 | 386 |
TeamRemove(name) => msg!["REMOVE_TEAM", name], |
387 |
TeamAccepted(name) => msg!["TEAM_ACCEPTED", name], |
|
388 |
TeamColor(name, color) => msg!["TEAM_COLOR", name, color], |
|
389 |
HedgehogsNumber(name, number) => msg!["HH_NUM", name, number], |
|
14478 | 390 |
ConfigEntry(name, values) => construct_message(&["CFG", name], &values), |
13450 | 391 |
Kicked => msg!["KICKED"], |
13423 | 392 |
RunGame => msg!["RUN_GAME"], |
14478 | 393 |
ForwardEngineMessage(em) => construct_message(&["EM"], &em), |
13423 | 394 |
RoundFinished => msg!["ROUND_FINISHED"], |
14478 | 395 |
ChatMsg { nick, msg } => msg!["CHAT", nick, msg], |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14806
diff
changeset
|
396 |
Info(info) => construct_message(&["INFO"], &info), |
13416 | 397 |
ServerMessage(msg) => msg!["SERVER_MESSAGE", msg], |
14804 | 398 |
ServerVars(vars) => construct_message(&["SERVER_VARS"], &vars), |
13775 | 399 |
Notice(msg) => msg!["NOTICE", msg], |
13416 | 400 |
Warning(msg) => msg!["WARNING", msg], |
401 |
Error(msg) => msg!["ERROR", msg], |
|
13775 | 402 |
|
14478 | 403 |
LegacyReady(is_ready, nicks) => { |
404 |
construct_message(&[if *is_ready { "READY" } else { "NOT_READY" }], &nicks) |
|
405 |
} |
|
13775 | 406 |
|
13416 | 407 |
_ => msg!["ERROR", "UNIMPLEMENTED"], |
12136 | 408 |
} |
409 |
} |
|
410 |
} |