author | alfadur |
Tue, 17 Jul 2018 16:03:34 +0300 | |
changeset 13529 | 5359ff75da3a |
parent 13528 | 8c5dd562c9f7 |
child 13530 | d126d9a646ac |
permissions | -rw-r--r-- |
12152 | 1 |
use mio; |
2 |
||
13421 | 3 |
use protocol::messages::{ |
4 |
HWProtocolMessage, |
|
13483 | 5 |
HWServerMessage::*, |
6 |
server_chat |
|
13421 | 7 |
}; |
13424 | 8 |
use server::{ |
13526 | 9 |
coretypes::{ClientId, RoomId, Voting, VoteType}, |
13424 | 10 |
server::HWServer, |
13528 | 11 |
room::{HWRoom, RoomFlags}, |
13424 | 12 |
actions::{Action, Action::*} |
13 |
}; |
|
13421 | 14 |
use utils::is_name_illegal; |
15 |
use std::mem::swap; |
|
13428 | 16 |
use base64::{encode, decode}; |
13526 | 17 |
use super::common::rnd_reply; |
13428 | 18 |
|
19 |
#[derive(Clone)] |
|
20 |
struct ByMsg<'a> { |
|
21 |
messages: &'a[u8] |
|
22 |
} |
|
23 |
||
24 |
impl <'a> Iterator for ByMsg<'a> { |
|
25 |
type Item = &'a[u8]; |
|
26 |
||
27 |
fn next(&mut self) -> Option<<Self as Iterator>::Item> { |
|
28 |
if let Some(size) = self.messages.get(0) { |
|
29 |
let (msg, next) = self.messages.split_at(*size as usize + 1); |
|
30 |
self.messages = next; |
|
31 |
Some(msg) |
|
32 |
} else { |
|
33 |
None |
|
34 |
} |
|
35 |
} |
|
36 |
} |
|
37 |
||
13529 | 38 |
fn by_msg(source: &[u8]) -> ByMsg { |
39 |
ByMsg {messages: source} |
|
13428 | 40 |
} |
41 |
||
42 |
const VALID_MESSAGES: &[u8] = |
|
43 |
b"M#+LlRrUuDdZzAaSjJ,NpPwtgfhbc12345\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A"; |
|
44 |
const NON_TIMED_MESSAGES: &[u8] = b"M#hb"; |
|
45 |
||
13434 | 46 |
#[cfg(canhazslicepatterns)] |
13428 | 47 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool { |
13429 | 48 |
match msg { |
49 |
[size, typ, body..] => VALID_MESSAGES.contains(typ) |
|
50 |
&& match body { |
|
13428 | 51 |
[1...8, team, ..] if *typ == b'h' => team_indices.contains(team), |
52 |
_ => *typ != b'h' |
|
13429 | 53 |
}, |
54 |
_ => false |
|
13428 | 55 |
} |
56 |
} |
|
57 |
||
13434 | 58 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool { |
59 |
if let Some(typ) = msg.get(1) { |
|
60 |
VALID_MESSAGES.contains(typ) |
|
61 |
} else { |
|
62 |
false |
|
63 |
} |
|
64 |
} |
|
65 |
||
13428 | 66 |
fn is_msg_empty(msg: &[u8]) -> bool { |
13434 | 67 |
msg.get(1).filter(|t| **t == b'+').is_some() |
13428 | 68 |
} |
12152 | 69 |
|
13448 | 70 |
fn is_msg_timed(msg: &[u8]) -> bool { |
71 |
msg.get(1).filter(|t| !NON_TIMED_MESSAGES.contains(t)).is_some() |
|
72 |
} |
|
73 |
||
13485 | 74 |
fn voting_description(kind: &VoteType) -> String { |
75 |
format!("New voting started: {}", match kind { |
|
76 |
VoteType::Kick(nick) => format!("kick {}", nick), |
|
77 |
VoteType::Map(name) => format!("map {}", name.as_ref().unwrap()), |
|
78 |
VoteType::Pause => "pause".to_string(), |
|
79 |
VoteType::NewSeed => "new seed".to_string(), |
|
80 |
VoteType::HedgehogsPerTeam(number) => format!("hedgehogs per team: {}", number) |
|
81 |
}) |
|
82 |
} |
|
83 |
||
13528 | 84 |
fn room_message_flag(msg: &HWProtocolMessage) -> RoomFlags { |
85 |
use protocol::messages::HWProtocolMessage::*; |
|
86 |
match msg { |
|
87 |
ToggleRestrictJoin => RoomFlags::RESTRICTED_JOIN, |
|
88 |
ToggleRestrictTeams => RoomFlags::RESTRICTED_TEAM_ADD, |
|
89 |
ToggleRegisteredOnly => RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS, |
|
90 |
_ => RoomFlags::empty() |
|
91 |
} |
|
92 |
} |
|
93 |
||
13526 | 94 |
pub fn handle(server: &mut HWServer, client_id: ClientId, room_id: RoomId, message: HWProtocolMessage) { |
13421 | 95 |
use protocol::messages::HWProtocolMessage::*; |
12152 | 96 |
match message { |
13424 | 97 |
Part(None) => server.react(client_id, vec![ |
13421 | 98 |
MoveToLobby("part".to_string())]), |
13424 | 99 |
Part(Some(msg)) => server.react(client_id, vec![ |
13421 | 100 |
MoveToLobby(format!("part: {}", msg))]), |
101 |
Chat(msg) => { |
|
13529 | 102 |
|
13424 | 103 |
let actions = { |
104 |
let c = &mut server.clients[client_id]; |
|
13529 | 105 |
let chat_msg = ChatMsg {nick: c.nick.clone(), msg}; |
13526 | 106 |
vec![chat_msg.send_all().in_room(room_id).but_self().action()] |
13424 | 107 |
}; |
108 |
server.react(client_id, actions); |
|
13421 | 109 |
}, |
13482 | 110 |
Fix => { |
111 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13528 | 112 |
if c.is_admin() { r.set_is_fixed(true) } |
13482 | 113 |
} |
114 |
} |
|
115 |
Unfix => { |
|
116 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13528 | 117 |
if c.is_admin() { r.set_is_fixed(false) } |
13482 | 118 |
} |
119 |
} |
|
120 |
Greeting(text) => { |
|
121 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13528 | 122 |
if c.is_admin() || c.is_master() && !r.is_fixed() { |
13482 | 123 |
r.greeting = text |
124 |
} |
|
125 |
} |
|
126 |
} |
|
13421 | 127 |
RoomName(new_name) => { |
128 |
let actions = |
|
129 |
if is_name_illegal(&new_name) { |
|
130 |
vec![Warn("Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string())] |
|
13528 | 131 |
} else if server.room(client_id).map(|r| r.is_fixed()).unwrap_or(false) { |
13482 | 132 |
vec![Warn("Access denied.".to_string())] |
13421 | 133 |
} else if server.has_room(&new_name) { |
134 |
vec![Warn("A room with the same name already exists.".to_string())] |
|
135 |
} else { |
|
136 |
let mut old_name = new_name.clone(); |
|
13427 | 137 |
if let (_, Some(r)) = server.client_and_room(client_id) { |
13421 | 138 |
swap(&mut r.name, &mut old_name); |
139 |
vec![SendRoomUpdate(Some(old_name))] |
|
140 |
} else { |
|
141 |
Vec::new() |
|
142 |
} |
|
143 |
}; |
|
13424 | 144 |
server.react(client_id, actions); |
145 |
}, |
|
146 |
ToggleReady => { |
|
147 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13525 | 148 |
let flags = if c.is_ready() { |
13424 | 149 |
r.ready_players_number -= 1; |
150 |
"-r" |
|
151 |
} else { |
|
152 |
r.ready_players_number += 1; |
|
153 |
"+r" |
|
154 |
}; |
|
13525 | 155 |
let is_ready = !c.is_ready(); |
156 |
c.set_is_ready(is_ready); |
|
13482 | 157 |
let mut v = |
158 |
vec![ClientFlags(flags.to_string(), vec![c.nick.clone()]) |
|
159 |
.send_all().in_room(r.id).action()]; |
|
13528 | 160 |
if r.is_fixed() && r.ready_players_number == r.players_number { |
13482 | 161 |
v.push(StartRoomGame(r.id)) |
162 |
} |
|
163 |
v |
|
13424 | 164 |
} else { |
165 |
Vec::new() |
|
166 |
}; |
|
167 |
server.react(client_id, actions); |
|
13421 | 168 |
} |
13427 | 169 |
AddTeam(info) => { |
13424 | 170 |
let mut actions = Vec::new(); |
171 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
172 |
if r.teams.len() >= r.team_limit as usize { |
|
173 |
actions.push(Warn("Too many teams!".to_string())) |
|
174 |
} else if r.addable_hedgehogs() == 0 { |
|
175 |
actions.push(Warn("Too many hedgehogs!".to_string())) |
|
176 |
} else if r.find_team(|t| t.name == info.name) != None { |
|
177 |
actions.push(Warn("There's already a team with same name in the list.".to_string())) |
|
13428 | 178 |
} else if r.game_info.is_some() { |
13424 | 179 |
actions.push(Warn("Joining not possible: Round is in progress.".to_string())) |
13528 | 180 |
} else if r.is_team_add_restricted() { |
181 |
actions.push(Warn("This room currently does not allow adding new teams.".to_string())); |
|
13424 | 182 |
} else { |
13529 | 183 |
let team = r.add_team(c.id, *info); |
13424 | 184 |
c.teams_in_game += 1; |
185 |
c.clan = Some(team.color); |
|
186 |
actions.push(TeamAccepted(team.name.clone()) |
|
187 |
.send_self().action()); |
|
188 |
actions.push(TeamAdd(HWRoom::team_info(&c, team)) |
|
189 |
.send_all().in_room(room_id).but_self().action()); |
|
190 |
actions.push(TeamColor(team.name.clone(), team.color) |
|
191 |
.send_all().in_room(room_id).action()); |
|
192 |
actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
193 |
.send_all().in_room(room_id).action()); |
|
194 |
actions.push(SendRoomUpdate(None)); |
|
195 |
} |
|
196 |
} |
|
197 |
server.react(client_id, actions); |
|
198 |
}, |
|
199 |
RemoveTeam(name) => { |
|
200 |
let mut actions = Vec::new(); |
|
201 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
202 |
match r.find_team_owner(&name) { |
|
203 |
None => |
|
204 |
actions.push(Warn("Error: The team you tried to remove does not exist.".to_string())), |
|
205 |
Some((id, _)) if id != client_id => |
|
206 |
actions.push(Warn("You can't remove a team you don't own.".to_string())), |
|
207 |
Some((_, name)) => { |
|
208 |
c.teams_in_game -= 1; |
|
209 |
c.clan = r.find_team_color(c.id); |
|
210 |
actions.push(Action::RemoveTeam(name.to_string())); |
|
211 |
} |
|
212 |
} |
|
213 |
}; |
|
214 |
server.react(client_id, actions); |
|
215 |
}, |
|
216 |
SetHedgehogsNumber(team_name, number) => { |
|
217 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
218 |
let addable_hedgehogs = r.addable_hedgehogs(); |
|
219 |
if let Some((_, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) { |
|
13525 | 220 |
if !c.is_master() { |
13424 | 221 |
vec![ProtocolError("You're not the room master!".to_string())] |
222 |
} else if number < 1 || number > 8 |
|
223 |
|| number > addable_hedgehogs + team.hedgehogs_number { |
|
224 |
vec![HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
225 |
.send_self().action()] |
|
226 |
} else { |
|
227 |
team.hedgehogs_number = number; |
|
228 |
vec![HedgehogsNumber(team.name.clone(), number) |
|
229 |
.send_all().in_room(room_id).but_self().action()] |
|
230 |
} |
|
231 |
} else { |
|
232 |
vec![(Warn("No such team.".to_string()))] |
|
233 |
} |
|
234 |
} else { |
|
235 |
Vec::new() |
|
236 |
}; |
|
237 |
server.react(client_id, actions); |
|
238 |
}, |
|
239 |
SetTeamColor(team_name, color) => { |
|
240 |
let mut owner_id = None; |
|
241 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
242 |
if let Some((owner, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) { |
|
13525 | 243 |
if !c.is_master() { |
13424 | 244 |
vec![ProtocolError("You're not the room master!".to_string())] |
245 |
} else if false { |
|
246 |
Vec::new() |
|
247 |
} else { |
|
248 |
owner_id = Some(owner); |
|
249 |
team.color = color; |
|
250 |
vec![TeamColor(team.name.clone(), color) |
|
251 |
.send_all().in_room(room_id).but_self().action()] |
|
252 |
} |
|
253 |
} else { |
|
254 |
vec![(Warn("No such team.".to_string()))] |
|
255 |
} |
|
256 |
} else { |
|
257 |
Vec::new() |
|
258 |
}; |
|
259 |
||
260 |
if let Some(id) = owner_id { |
|
261 |
server.clients[id].clan = Some(color); |
|
262 |
} |
|
263 |
||
264 |
server.react(client_id, actions); |
|
13427 | 265 |
}, |
266 |
Cfg(cfg) => { |
|
267 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13528 | 268 |
if r.is_fixed() { |
13482 | 269 |
vec![Warn("Access denied.".to_string())] |
13525 | 270 |
} else if !c.is_master() { |
13427 | 271 |
vec![ProtocolError("You're not the room master!".to_string())] |
272 |
} else { |
|
13444 | 273 |
let v = vec![cfg.to_server_msg() |
274 |
.send_all().in_room(r.id).but_self().action()]; |
|
275 |
r.set_config(cfg); |
|
276 |
v |
|
13427 | 277 |
} |
278 |
} else { |
|
279 |
Vec::new() |
|
280 |
}; |
|
281 |
server.react(client_id, actions); |
|
13424 | 282 |
} |
13483 | 283 |
CallVote(None) => { |
284 |
server.react(client_id, vec![ |
|
285 |
server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>") |
|
286 |
.send_self().action()]) |
|
287 |
} |
|
288 |
CallVote(Some(kind)) => { |
|
13526 | 289 |
let is_in_game = server.rooms[room_id].game_info.is_some(); |
13483 | 290 |
let error = match &kind { |
291 |
VoteType::Kick(nick) => { |
|
292 |
if server.find_client(&nick).filter(|c| c.room_id == Some(room_id)).is_some() { |
|
293 |
None |
|
294 |
} else { |
|
295 |
Some("/callvote kick: No such user!") |
|
296 |
} |
|
297 |
}, |
|
298 |
VoteType::Map(None) => { |
|
299 |
Some("/callvote map: Not implemented") |
|
300 |
}, |
|
301 |
VoteType::Map(Some(name)) => { |
|
302 |
Some("/callvote map: Not implemented") |
|
303 |
}, |
|
304 |
VoteType::Pause => { |
|
305 |
if is_in_game { |
|
306 |
None |
|
307 |
} else { |
|
308 |
Some("/callvote pause: No game in progress!") |
|
309 |
} |
|
310 |
}, |
|
311 |
VoteType::NewSeed => { |
|
312 |
None |
|
313 |
}, |
|
314 |
VoteType::HedgehogsPerTeam(number) => { |
|
315 |
match number { |
|
316 |
1...8 => None, |
|
317 |
_ => Some("/callvote hedgehogs: Specify number from 1 to 8.") |
|
318 |
} |
|
319 |
}, |
|
320 |
}; |
|
321 |
match error { |
|
322 |
None => { |
|
13485 | 323 |
let msg = voting_description(&kind); |
13483 | 324 |
let voting = Voting::new(kind, server.room_clients(client_id)); |
325 |
server.room(client_id).unwrap().voting = Some(voting); |
|
326 |
server.react(client_id, vec![ |
|
13485 | 327 |
server_chat(&msg).send_all().in_room(room_id).action(), |
13483 | 328 |
AddVote{ vote: true, is_forced: false}]); |
329 |
} |
|
330 |
Some(msg) => { |
|
331 |
server.react(client_id, vec![ |
|
332 |
server_chat(msg).send_self().action()]) |
|
333 |
} |
|
334 |
} |
|
335 |
} |
|
336 |
Vote(vote) => { |
|
337 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
338 |
vec![AddVote{ vote, is_forced: false }] |
|
339 |
} else { |
|
340 |
Vec::new() |
|
341 |
}; |
|
342 |
server.react(client_id, actions); |
|
343 |
} |
|
344 |
ForceVote(vote) => { |
|
345 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13525 | 346 |
vec![AddVote{ vote, is_forced: c.is_admin()} ] |
13483 | 347 |
} else { |
348 |
Vec::new() |
|
349 |
}; |
|
350 |
server.react(client_id, actions); |
|
351 |
} |
|
13528 | 352 |
ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly => { |
353 |
if server.clients[client_id].is_master() { |
|
354 |
server.rooms[room_id].flags.toggle(room_message_flag(&message)); |
|
355 |
} |
|
356 |
server.react(client_id, vec![SendRoomUpdate(None)]); |
|
357 |
} |
|
13428 | 358 |
StartGame => { |
359 |
let actions = if let (_, Some(r)) = server.client_and_room(client_id) { |
|
360 |
vec![StartRoomGame(r.id)] |
|
361 |
} else { |
|
362 |
Vec::new() |
|
363 |
}; |
|
364 |
server.react(client_id, actions); |
|
365 |
} |
|
366 |
EngineMessage(em) => { |
|
367 |
let mut actions = Vec::new(); |
|
368 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
369 |
if c.teams_in_game > 0 { |
|
370 |
let decoding = decode(&em[..]).unwrap(); |
|
371 |
let messages = by_msg(&decoding); |
|
13448 | 372 |
let valid = messages.filter(|m| is_msg_valid(m, &c.team_indices)); |
373 |
let non_empty = valid.clone().filter(|m| !is_msg_empty(m)); |
|
374 |
let sync_msg = valid.clone().filter(|m| is_msg_timed(m)) |
|
375 |
.last().map(|m| if is_msg_empty(m) {Some(encode(m))} else {None}); |
|
13428 | 376 |
|
377 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
|
378 |
if !em_response.is_empty() { |
|
13433 | 379 |
actions.push(ForwardEngineMessage(vec![em_response]) |
13428 | 380 |
.send_all().in_room(r.id).but_self().action()); |
381 |
} |
|
13432 | 382 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
383 |
if let Some(ref mut info) = r.game_info { |
|
13434 | 384 |
if !em_log.is_empty() { |
13433 | 385 |
info.msg_log.push(em_log); |
386 |
} |
|
13448 | 387 |
if let Some(msg) = sync_msg { |
388 |
info.sync_msg = msg; |
|
13432 | 389 |
} |
390 |
} |
|
13428 | 391 |
} |
392 |
} |
|
393 |
server.react(client_id, actions) |
|
394 |
} |
|
395 |
RoundFinished => { |
|
396 |
let mut actions = Vec::new(); |
|
397 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13525 | 398 |
if c.is_in_game() { |
399 |
c.set_is_in_game(false); |
|
13428 | 400 |
actions.push(ClientFlags("-g".to_string(), vec![c.nick.clone()]). |
401 |
send_all().in_room(r.id).action()); |
|
13431 | 402 |
if r.game_info.is_some() { |
403 |
for team in r.client_teams(c.id) { |
|
404 |
actions.push(SendTeamRemovalMessage(team.name.clone())); |
|
405 |
} |
|
13428 | 406 |
} |
407 |
} |
|
408 |
} |
|
409 |
server.react(client_id, actions) |
|
13449
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13448
diff
changeset
|
410 |
}, |
13450
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13449
diff
changeset
|
411 |
Rnd(v) => { |
13526 | 412 |
let result = rnd_reply(&v); |
413 |
let mut echo = vec!["/rnd".to_string()]; |
|
414 |
echo.extend(v.into_iter()); |
|
415 |
let chat_msg = ChatMsg { |
|
416 |
nick: server.clients[client_id].nick.clone(), |
|
417 |
msg: echo.join(" ") |
|
418 |
}; |
|
419 |
server.react(client_id, vec![ |
|
420 |
chat_msg.send_all().in_room(room_id).action(), |
|
421 |
result.send_all().in_room(room_id).action()]) |
|
13450
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13449
diff
changeset
|
422 |
}, |
13424 | 423 |
_ => warn!("Unimplemented!") |
12152 | 424 |
} |
425 |
} |