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