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