author | alfadur |
Fri, 20 Jul 2018 22:14:20 +0300 | |
changeset 13528 | c8b626b0a3ad |
parent 13521 | e3ae9eea0689 |
child 13529 | 662f7df89d06 |
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 |
||
13500 | 38 |
fn by_msg(source: &[u8]) -> ByMsg { |
39 |
ByMsg {messages: source} |
|
13423 | 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]; |
|
13500 | 104 |
let chat_msg = ChatMsg {nick: c.nick.clone(), 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())] |
|
13520 | 130 |
} else if server.rooms[room_id].is_fixed() { |
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(); |
|
13520 | 136 |
swap(&mut server.rooms[room_id].name, &mut old_name); |
137 |
vec![SendRoomUpdate(Some(old_name))] |
|
13416 | 138 |
}; |
13419 | 139 |
server.react(client_id, actions); |
140 |
}, |
|
141 |
ToggleReady => { |
|
142 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13486 | 143 |
let flags = if c.is_ready() { |
13419 | 144 |
r.ready_players_number -= 1; |
145 |
"-r" |
|
146 |
} else { |
|
147 |
r.ready_players_number += 1; |
|
148 |
"+r" |
|
149 |
}; |
|
13486 | 150 |
let is_ready = !c.is_ready(); |
151 |
c.set_is_ready(is_ready); |
|
13447 | 152 |
let mut v = |
153 |
vec![ClientFlags(flags.to_string(), vec![c.nick.clone()]) |
|
154 |
.send_all().in_room(r.id).action()]; |
|
13494 | 155 |
if r.is_fixed() && r.ready_players_number == r.players_number { |
13447 | 156 |
v.push(StartRoomGame(r.id)) |
157 |
} |
|
158 |
v |
|
13419 | 159 |
} else { |
160 |
Vec::new() |
|
161 |
}; |
|
162 |
server.react(client_id, actions); |
|
13416 | 163 |
} |
13422 | 164 |
AddTeam(info) => { |
13419 | 165 |
let mut actions = Vec::new(); |
166 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
167 |
if r.teams.len() >= r.team_limit as usize { |
|
168 |
actions.push(Warn("Too many teams!".to_string())) |
|
169 |
} else if r.addable_hedgehogs() == 0 { |
|
170 |
actions.push(Warn("Too many hedgehogs!".to_string())) |
|
171 |
} else if r.find_team(|t| t.name == info.name) != None { |
|
172 |
actions.push(Warn("There's already a team with same name in the list.".to_string())) |
|
13423 | 173 |
} else if r.game_info.is_some() { |
13419 | 174 |
actions.push(Warn("Joining not possible: Round is in progress.".to_string())) |
13494 | 175 |
} else if r.is_team_add_restricted() { |
176 |
actions.push(Warn("This room currently does not allow adding new teams.".to_string())); |
|
13419 | 177 |
} else { |
13500 | 178 |
let team = r.add_team(c.id, *info); |
13419 | 179 |
c.teams_in_game += 1; |
180 |
c.clan = Some(team.color); |
|
181 |
actions.push(TeamAccepted(team.name.clone()) |
|
182 |
.send_self().action()); |
|
183 |
actions.push(TeamAdd(HWRoom::team_info(&c, team)) |
|
184 |
.send_all().in_room(room_id).but_self().action()); |
|
185 |
actions.push(TeamColor(team.name.clone(), team.color) |
|
186 |
.send_all().in_room(room_id).action()); |
|
187 |
actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
188 |
.send_all().in_room(room_id).action()); |
|
189 |
actions.push(SendRoomUpdate(None)); |
|
190 |
} |
|
191 |
} |
|
192 |
server.react(client_id, actions); |
|
193 |
}, |
|
194 |
RemoveTeam(name) => { |
|
195 |
let mut actions = Vec::new(); |
|
196 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
197 |
match r.find_team_owner(&name) { |
|
198 |
None => |
|
199 |
actions.push(Warn("Error: The team you tried to remove does not exist.".to_string())), |
|
200 |
Some((id, _)) if id != client_id => |
|
201 |
actions.push(Warn("You can't remove a team you don't own.".to_string())), |
|
202 |
Some((_, name)) => { |
|
203 |
c.teams_in_game -= 1; |
|
204 |
c.clan = r.find_team_color(c.id); |
|
205 |
actions.push(Action::RemoveTeam(name.to_string())); |
|
206 |
} |
|
207 |
} |
|
208 |
}; |
|
209 |
server.react(client_id, actions); |
|
210 |
}, |
|
211 |
SetHedgehogsNumber(team_name, number) => { |
|
212 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
213 |
let addable_hedgehogs = r.addable_hedgehogs(); |
|
214 |
if let Some((_, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) { |
|
13486 | 215 |
if !c.is_master() { |
13419 | 216 |
vec![ProtocolError("You're not the room master!".to_string())] |
217 |
} else if number < 1 || number > 8 |
|
218 |
|| number > addable_hedgehogs + team.hedgehogs_number { |
|
219 |
vec![HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
220 |
.send_self().action()] |
|
221 |
} else { |
|
222 |
team.hedgehogs_number = number; |
|
223 |
vec![HedgehogsNumber(team.name.clone(), number) |
|
224 |
.send_all().in_room(room_id).but_self().action()] |
|
225 |
} |
|
226 |
} else { |
|
227 |
vec![(Warn("No such team.".to_string()))] |
|
228 |
} |
|
229 |
} else { |
|
230 |
Vec::new() |
|
231 |
}; |
|
232 |
server.react(client_id, actions); |
|
233 |
}, |
|
234 |
SetTeamColor(team_name, color) => { |
|
235 |
let mut owner_id = None; |
|
236 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
237 |
if let Some((owner, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) { |
|
13486 | 238 |
if !c.is_master() { |
13419 | 239 |
vec![ProtocolError("You're not the room master!".to_string())] |
240 |
} else if false { |
|
241 |
Vec::new() |
|
242 |
} else { |
|
243 |
owner_id = Some(owner); |
|
244 |
team.color = color; |
|
245 |
vec![TeamColor(team.name.clone(), color) |
|
246 |
.send_all().in_room(room_id).but_self().action()] |
|
247 |
} |
|
248 |
} else { |
|
249 |
vec![(Warn("No such team.".to_string()))] |
|
250 |
} |
|
251 |
} else { |
|
252 |
Vec::new() |
|
253 |
}; |
|
254 |
||
255 |
if let Some(id) = owner_id { |
|
256 |
server.clients[id].clan = Some(color); |
|
257 |
} |
|
258 |
||
259 |
server.react(client_id, actions); |
|
13422 | 260 |
}, |
261 |
Cfg(cfg) => { |
|
262 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13494 | 263 |
if r.is_fixed() { |
13447 | 264 |
vec![Warn("Access denied.".to_string())] |
13486 | 265 |
} else if !c.is_master() { |
13422 | 266 |
vec![ProtocolError("You're not the room master!".to_string())] |
267 |
} else { |
|
13439 | 268 |
let v = vec![cfg.to_server_msg() |
269 |
.send_all().in_room(r.id).but_self().action()]; |
|
270 |
r.set_config(cfg); |
|
271 |
v |
|
13422 | 272 |
} |
273 |
} else { |
|
274 |
Vec::new() |
|
275 |
}; |
|
276 |
server.react(client_id, actions); |
|
13419 | 277 |
} |
13528 | 278 |
Save(name, location) => { |
279 |
let actions = vec![server_chat(format!("Room config saved as {}", name)) |
|
280 |
.send_all().in_room(room_id).action()]; |
|
281 |
server.rooms[room_id].save_config(name, location); |
|
282 |
server.react(client_id, actions); |
|
283 |
} |
|
284 |
Delete(name) => { |
|
285 |
let actions = if !server.rooms[room_id].delete_config(&name) { |
|
286 |
vec![Warn(format!("Save doesn't exist: {}", name))] |
|
287 |
} else { |
|
288 |
vec![server_chat(format!("Room config {} has been deleted", name)) |
|
289 |
.send_all().in_room(room_id).action()] |
|
290 |
}; |
|
291 |
server.react(client_id, actions); |
|
292 |
} |
|
13450 | 293 |
CallVote(None) => { |
294 |
server.react(client_id, vec![ |
|
13521 | 295 |
server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>".to_string()) |
13450 | 296 |
.send_self().action()]) |
297 |
} |
|
298 |
CallVote(Some(kind)) => { |
|
13492 | 299 |
let is_in_game = server.rooms[room_id].game_info.is_some(); |
13450 | 300 |
let error = match &kind { |
301 |
VoteType::Kick(nick) => { |
|
302 |
if server.find_client(&nick).filter(|c| c.room_id == Some(room_id)).is_some() { |
|
303 |
None |
|
304 |
} else { |
|
13521 | 305 |
Some("/callvote kick: No such user!".to_string()) |
13450 | 306 |
} |
307 |
}, |
|
308 |
VoteType::Map(None) => { |
|
13521 | 309 |
let names: Vec<_> = server.rooms[room_id].saves.keys().cloned().collect(); |
310 |
if names.is_empty() { |
|
311 |
Some("/callvote map: No maps saved in this room!".to_string()) |
|
312 |
} else { |
|
313 |
Some(format!("Available maps: {}", names.join(", "))) |
|
314 |
} |
|
13450 | 315 |
}, |
316 |
VoteType::Map(Some(name)) => { |
|
13521 | 317 |
if server.rooms[room_id].saves.get(&name[..]).is_some() { |
318 |
Some("/callvote map: No such map!".to_string()) |
|
319 |
} else { |
|
320 |
None |
|
321 |
} |
|
13450 | 322 |
}, |
323 |
VoteType::Pause => { |
|
324 |
if is_in_game { |
|
325 |
None |
|
326 |
} else { |
|
13521 | 327 |
Some("/callvote pause: No game in progress!".to_string()) |
13450 | 328 |
} |
329 |
}, |
|
330 |
VoteType::NewSeed => { |
|
331 |
None |
|
332 |
}, |
|
333 |
VoteType::HedgehogsPerTeam(number) => { |
|
334 |
match number { |
|
335 |
1...8 => None, |
|
13521 | 336 |
_ => Some("/callvote hedgehogs: Specify number from 1 to 8.".to_string()) |
13450 | 337 |
} |
338 |
}, |
|
339 |
}; |
|
340 |
match error { |
|
341 |
None => { |
|
13460 | 342 |
let msg = voting_description(&kind); |
13450 | 343 |
let voting = Voting::new(kind, server.room_clients(client_id)); |
13520 | 344 |
server.rooms[room_id].voting = Some(voting); |
13450 | 345 |
server.react(client_id, vec![ |
13521 | 346 |
server_chat(msg).send_all().in_room(room_id).action(), |
13450 | 347 |
AddVote{ vote: true, is_forced: false}]); |
348 |
} |
|
349 |
Some(msg) => { |
|
350 |
server.react(client_id, vec![ |
|
351 |
server_chat(msg).send_self().action()]) |
|
352 |
} |
|
353 |
} |
|
354 |
} |
|
355 |
Vote(vote) => { |
|
13520 | 356 |
server.react(client_id, vec![AddVote{ vote, is_forced: false }]); |
13450 | 357 |
} |
358 |
ForceVote(vote) => { |
|
13520 | 359 |
let is_forced = server.clients[client_id].is_admin(); |
360 |
server.react(client_id, vec![AddVote{ vote, is_forced }]); |
|
13450 | 361 |
} |
13494 | 362 |
ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly => { |
363 |
if server.clients[client_id].is_master() { |
|
364 |
server.rooms[room_id].flags.toggle(room_message_flag(&message)); |
|
365 |
} |
|
366 |
server.react(client_id, vec![SendRoomUpdate(None)]); |
|
367 |
} |
|
13423 | 368 |
StartGame => { |
13520 | 369 |
server.react(client_id, vec![StartRoomGame(room_id)]); |
13423 | 370 |
} |
371 |
EngineMessage(em) => { |
|
372 |
let mut actions = Vec::new(); |
|
373 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
374 |
if c.teams_in_game > 0 { |
|
375 |
let decoding = decode(&em[..]).unwrap(); |
|
376 |
let messages = by_msg(&decoding); |
|
13443 | 377 |
let valid = messages.filter(|m| is_msg_valid(m, &c.team_indices)); |
378 |
let non_empty = valid.clone().filter(|m| !is_msg_empty(m)); |
|
379 |
let sync_msg = valid.clone().filter(|m| is_msg_timed(m)) |
|
380 |
.last().map(|m| if is_msg_empty(m) {Some(encode(m))} else {None}); |
|
13423 | 381 |
|
382 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
|
383 |
if !em_response.is_empty() { |
|
13428 | 384 |
actions.push(ForwardEngineMessage(vec![em_response]) |
13423 | 385 |
.send_all().in_room(r.id).but_self().action()); |
386 |
} |
|
13427 | 387 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
388 |
if let Some(ref mut info) = r.game_info { |
|
13429 | 389 |
if !em_log.is_empty() { |
13428 | 390 |
info.msg_log.push(em_log); |
391 |
} |
|
13443 | 392 |
if let Some(msg) = sync_msg { |
393 |
info.sync_msg = msg; |
|
13427 | 394 |
} |
395 |
} |
|
13423 | 396 |
} |
397 |
} |
|
398 |
server.react(client_id, actions) |
|
399 |
} |
|
400 |
RoundFinished => { |
|
401 |
let mut actions = Vec::new(); |
|
402 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13486 | 403 |
if c.is_in_game() { |
404 |
c.set_is_in_game(false); |
|
13423 | 405 |
actions.push(ClientFlags("-g".to_string(), vec![c.nick.clone()]). |
406 |
send_all().in_room(r.id).action()); |
|
13426 | 407 |
if r.game_info.is_some() { |
408 |
for team in r.client_teams(c.id) { |
|
409 |
actions.push(SendTeamRemovalMessage(team.name.clone())); |
|
410 |
} |
|
13423 | 411 |
} |
412 |
} |
|
413 |
} |
|
414 |
server.react(client_id, actions) |
|
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13443
diff
changeset
|
415 |
}, |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
416 |
Rnd(v) => { |
13492 | 417 |
let result = rnd_reply(&v); |
418 |
let mut echo = vec!["/rnd".to_string()]; |
|
419 |
echo.extend(v.into_iter()); |
|
420 |
let chat_msg = ChatMsg { |
|
421 |
nick: server.clients[client_id].nick.clone(), |
|
422 |
msg: echo.join(" ") |
|
423 |
}; |
|
424 |
server.react(client_id, vec![ |
|
425 |
chat_msg.send_all().in_room(room_id).action(), |
|
426 |
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
|
427 |
}, |
13419 | 428 |
_ => warn!("Unimplemented!") |
12147 | 429 |
} |
430 |
} |