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