author | alfadur |
Fri, 20 Jul 2018 00:02:52 +0300 | |
changeset 13532 | e3ae9eea0689 |
parent 13531 | 3f69acc7e268 |
child 13533 | c8b626b0a3ad |
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) => { |
|
13424 | 102 |
let actions = { |
103 |
let c = &mut server.clients[client_id]; |
|
13529 | 104 |
let chat_msg = ChatMsg {nick: c.nick.clone(), msg}; |
13526 | 105 |
vec![chat_msg.send_all().in_room(room_id).but_self().action()] |
13424 | 106 |
}; |
107 |
server.react(client_id, actions); |
|
13421 | 108 |
}, |
13482 | 109 |
Fix => { |
110 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13528 | 111 |
if c.is_admin() { r.set_is_fixed(true) } |
13482 | 112 |
} |
113 |
} |
|
114 |
Unfix => { |
|
115 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13528 | 116 |
if c.is_admin() { r.set_is_fixed(false) } |
13482 | 117 |
} |
118 |
} |
|
119 |
Greeting(text) => { |
|
120 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13528 | 121 |
if c.is_admin() || c.is_master() && !r.is_fixed() { |
13482 | 122 |
r.greeting = text |
123 |
} |
|
124 |
} |
|
125 |
} |
|
13421 | 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())] |
|
13531 | 130 |
} else if server.rooms[room_id].is_fixed() { |
13482 | 131 |
vec![Warn("Access denied.".to_string())] |
13421 | 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(); |
|
13531 | 136 |
swap(&mut server.rooms[room_id].name, &mut old_name); |
137 |
vec![SendRoomUpdate(Some(old_name))] |
|
13421 | 138 |
}; |
13424 | 139 |
server.react(client_id, actions); |
140 |
}, |
|
141 |
ToggleReady => { |
|
142 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13525 | 143 |
let flags = if c.is_ready() { |
13424 | 144 |
r.ready_players_number -= 1; |
145 |
"-r" |
|
146 |
} else { |
|
147 |
r.ready_players_number += 1; |
|
148 |
"+r" |
|
149 |
}; |
|
13525 | 150 |
let is_ready = !c.is_ready(); |
151 |
c.set_is_ready(is_ready); |
|
13482 | 152 |
let mut v = |
153 |
vec![ClientFlags(flags.to_string(), vec![c.nick.clone()]) |
|
154 |
.send_all().in_room(r.id).action()]; |
|
13528 | 155 |
if r.is_fixed() && r.ready_players_number == r.players_number { |
13482 | 156 |
v.push(StartRoomGame(r.id)) |
157 |
} |
|
158 |
v |
|
13424 | 159 |
} else { |
160 |
Vec::new() |
|
161 |
}; |
|
162 |
server.react(client_id, actions); |
|
13421 | 163 |
} |
13427 | 164 |
AddTeam(info) => { |
13424 | 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())) |
|
13428 | 173 |
} else if r.game_info.is_some() { |
13424 | 174 |
actions.push(Warn("Joining not possible: Round is in progress.".to_string())) |
13528 | 175 |
} else if r.is_team_add_restricted() { |
176 |
actions.push(Warn("This room currently does not allow adding new teams.".to_string())); |
|
13424 | 177 |
} else { |
13529 | 178 |
let team = r.add_team(c.id, *info); |
13424 | 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) { |
|
13525 | 215 |
if !c.is_master() { |
13424 | 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) { |
|
13525 | 238 |
if !c.is_master() { |
13424 | 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); |
|
13427 | 260 |
}, |
261 |
Cfg(cfg) => { |
|
262 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13528 | 263 |
if r.is_fixed() { |
13482 | 264 |
vec![Warn("Access denied.".to_string())] |
13525 | 265 |
} else if !c.is_master() { |
13427 | 266 |
vec![ProtocolError("You're not the room master!".to_string())] |
267 |
} else { |
|
13444 | 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 |
|
13427 | 272 |
} |
273 |
} else { |
|
274 |
Vec::new() |
|
275 |
}; |
|
276 |
server.react(client_id, actions); |
|
13424 | 277 |
} |
13483 | 278 |
CallVote(None) => { |
279 |
server.react(client_id, vec![ |
|
13532 | 280 |
server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>".to_string()) |
13483 | 281 |
.send_self().action()]) |
282 |
} |
|
283 |
CallVote(Some(kind)) => { |
|
13526 | 284 |
let is_in_game = server.rooms[room_id].game_info.is_some(); |
13483 | 285 |
let error = match &kind { |
286 |
VoteType::Kick(nick) => { |
|
287 |
if server.find_client(&nick).filter(|c| c.room_id == Some(room_id)).is_some() { |
|
288 |
None |
|
289 |
} else { |
|
13532 | 290 |
Some("/callvote kick: No such user!".to_string()) |
13483 | 291 |
} |
292 |
}, |
|
293 |
VoteType::Map(None) => { |
|
13532 | 294 |
let names: Vec<_> = server.rooms[room_id].saves.keys().cloned().collect(); |
295 |
if names.is_empty() { |
|
296 |
Some("/callvote map: No maps saved in this room!".to_string()) |
|
297 |
} else { |
|
298 |
Some(format!("Available maps: {}", names.join(", "))) |
|
299 |
} |
|
13483 | 300 |
}, |
301 |
VoteType::Map(Some(name)) => { |
|
13532 | 302 |
if server.rooms[room_id].saves.get(&name[..]).is_some() { |
303 |
Some("/callvote map: No such map!".to_string()) |
|
304 |
} else { |
|
305 |
None |
|
306 |
} |
|
13483 | 307 |
}, |
308 |
VoteType::Pause => { |
|
309 |
if is_in_game { |
|
310 |
None |
|
311 |
} else { |
|
13532 | 312 |
Some("/callvote pause: No game in progress!".to_string()) |
13483 | 313 |
} |
314 |
}, |
|
315 |
VoteType::NewSeed => { |
|
316 |
None |
|
317 |
}, |
|
318 |
VoteType::HedgehogsPerTeam(number) => { |
|
319 |
match number { |
|
320 |
1...8 => None, |
|
13532 | 321 |
_ => Some("/callvote hedgehogs: Specify number from 1 to 8.".to_string()) |
13483 | 322 |
} |
323 |
}, |
|
324 |
}; |
|
325 |
match error { |
|
326 |
None => { |
|
13485 | 327 |
let msg = voting_description(&kind); |
13483 | 328 |
let voting = Voting::new(kind, server.room_clients(client_id)); |
13531 | 329 |
server.rooms[room_id].voting = Some(voting); |
13483 | 330 |
server.react(client_id, vec![ |
13532 | 331 |
server_chat(msg).send_all().in_room(room_id).action(), |
13483 | 332 |
AddVote{ vote: true, is_forced: false}]); |
333 |
} |
|
334 |
Some(msg) => { |
|
335 |
server.react(client_id, vec![ |
|
336 |
server_chat(msg).send_self().action()]) |
|
337 |
} |
|
338 |
} |
|
339 |
} |
|
340 |
Vote(vote) => { |
|
13531 | 341 |
server.react(client_id, vec![AddVote{ vote, is_forced: false }]); |
13483 | 342 |
} |
343 |
ForceVote(vote) => { |
|
13531 | 344 |
let is_forced = server.clients[client_id].is_admin(); |
345 |
server.react(client_id, vec![AddVote{ vote, is_forced }]); |
|
13483 | 346 |
} |
13528 | 347 |
ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly => { |
348 |
if server.clients[client_id].is_master() { |
|
349 |
server.rooms[room_id].flags.toggle(room_message_flag(&message)); |
|
350 |
} |
|
351 |
server.react(client_id, vec![SendRoomUpdate(None)]); |
|
352 |
} |
|
13428 | 353 |
StartGame => { |
13531 | 354 |
server.react(client_id, vec![StartRoomGame(room_id)]); |
13428 | 355 |
} |
356 |
EngineMessage(em) => { |
|
357 |
let mut actions = Vec::new(); |
|
358 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
359 |
if c.teams_in_game > 0 { |
|
360 |
let decoding = decode(&em[..]).unwrap(); |
|
361 |
let messages = by_msg(&decoding); |
|
13448 | 362 |
let valid = messages.filter(|m| is_msg_valid(m, &c.team_indices)); |
363 |
let non_empty = valid.clone().filter(|m| !is_msg_empty(m)); |
|
364 |
let sync_msg = valid.clone().filter(|m| is_msg_timed(m)) |
|
365 |
.last().map(|m| if is_msg_empty(m) {Some(encode(m))} else {None}); |
|
13428 | 366 |
|
367 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
|
368 |
if !em_response.is_empty() { |
|
13433 | 369 |
actions.push(ForwardEngineMessage(vec![em_response]) |
13428 | 370 |
.send_all().in_room(r.id).but_self().action()); |
371 |
} |
|
13432 | 372 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
373 |
if let Some(ref mut info) = r.game_info { |
|
13434 | 374 |
if !em_log.is_empty() { |
13433 | 375 |
info.msg_log.push(em_log); |
376 |
} |
|
13448 | 377 |
if let Some(msg) = sync_msg { |
378 |
info.sync_msg = msg; |
|
13432 | 379 |
} |
380 |
} |
|
13428 | 381 |
} |
382 |
} |
|
383 |
server.react(client_id, actions) |
|
384 |
} |
|
385 |
RoundFinished => { |
|
386 |
let mut actions = Vec::new(); |
|
387 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13525 | 388 |
if c.is_in_game() { |
389 |
c.set_is_in_game(false); |
|
13428 | 390 |
actions.push(ClientFlags("-g".to_string(), vec![c.nick.clone()]). |
391 |
send_all().in_room(r.id).action()); |
|
13431 | 392 |
if r.game_info.is_some() { |
393 |
for team in r.client_teams(c.id) { |
|
394 |
actions.push(SendTeamRemovalMessage(team.name.clone())); |
|
395 |
} |
|
13428 | 396 |
} |
397 |
} |
|
398 |
} |
|
399 |
server.react(client_id, actions) |
|
13449
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13448
diff
changeset
|
400 |
}, |
13450
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13449
diff
changeset
|
401 |
Rnd(v) => { |
13526 | 402 |
let result = rnd_reply(&v); |
403 |
let mut echo = vec!["/rnd".to_string()]; |
|
404 |
echo.extend(v.into_iter()); |
|
405 |
let chat_msg = ChatMsg { |
|
406 |
nick: server.clients[client_id].nick.clone(), |
|
407 |
msg: echo.join(" ") |
|
408 |
}; |
|
409 |
server.react(client_id, vec![ |
|
410 |
chat_msg.send_all().in_room(room_id).action(), |
|
411 |
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
|
412 |
}, |
13424 | 413 |
_ => warn!("Unimplemented!") |
12152 | 414 |
} |
415 |
} |