author | alfadur |
Thu, 19 Jul 2018 22:50:46 +0300 | |
changeset 13526 | 3f69acc7e268 |
parent 13525 | d126d9a646ac |
child 13527 | e3ae9eea0689 |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
13416 | 3 |
use protocol::messages::{ |
4 |
HWProtocolMessage, |
|
13478 | 5 |
HWServerMessage::*, |
6 |
server_chat |
|
13416 | 7 |
}; |
13419 | 8 |
use server::{ |
13521 | 9 |
coretypes::{ClientId, RoomId, Voting, VoteType}, |
13419 | 10 |
server::HWServer, |
13523 | 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}; |
13521 | 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 |
||
13524 | 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 |
||
13480 | 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 |
||
13523 | 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 |
||
13521 | 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]; |
|
13524 | 104 |
let chat_msg = ChatMsg {nick: c.nick.clone(), msg}; |
13521 | 105 |
vec![chat_msg.send_all().in_room(room_id).but_self().action()] |
13419 | 106 |
}; |
107 |
server.react(client_id, actions); |
|
13416 | 108 |
}, |
13477 | 109 |
Fix => { |
110 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13523 | 111 |
if c.is_admin() { r.set_is_fixed(true) } |
13477 | 112 |
} |
113 |
} |
|
114 |
Unfix => { |
|
115 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13523 | 116 |
if c.is_admin() { r.set_is_fixed(false) } |
13477 | 117 |
} |
118 |
} |
|
119 |
Greeting(text) => { |
|
120 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13523 | 121 |
if c.is_admin() || c.is_master() && !r.is_fixed() { |
13477 | 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())] |
|
13526 | 130 |
} else if server.rooms[room_id].is_fixed() { |
13477 | 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(); |
|
13526 | 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) { |
|
13520 | 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 |
}; |
|
13520 | 150 |
let is_ready = !c.is_ready(); |
151 |
c.set_is_ready(is_ready); |
|
13477 | 152 |
let mut v = |
153 |
vec![ClientFlags(flags.to_string(), vec![c.nick.clone()]) |
|
154 |
.send_all().in_room(r.id).action()]; |
|
13523 | 155 |
if r.is_fixed() && r.ready_players_number == r.players_number { |
13477 | 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())) |
13523 | 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 { |
13524 | 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) { |
|
13520 | 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) { |
|
13520 | 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) { |
|
13523 | 263 |
if r.is_fixed() { |
13477 | 264 |
vec![Warn("Access denied.".to_string())] |
13520 | 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 |
} |
13478 | 278 |
CallVote(None) => { |
279 |
server.react(client_id, vec![ |
|
280 |
server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>") |
|
281 |
.send_self().action()]) |
|
282 |
} |
|
283 |
CallVote(Some(kind)) => { |
|
13521 | 284 |
let is_in_game = server.rooms[room_id].game_info.is_some(); |
13478 | 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 { |
|
290 |
Some("/callvote kick: No such user!") |
|
291 |
} |
|
292 |
}, |
|
293 |
VoteType::Map(None) => { |
|
294 |
Some("/callvote map: Not implemented") |
|
295 |
}, |
|
296 |
VoteType::Map(Some(name)) => { |
|
297 |
Some("/callvote map: Not implemented") |
|
298 |
}, |
|
299 |
VoteType::Pause => { |
|
300 |
if is_in_game { |
|
301 |
None |
|
302 |
} else { |
|
303 |
Some("/callvote pause: No game in progress!") |
|
304 |
} |
|
305 |
}, |
|
306 |
VoteType::NewSeed => { |
|
307 |
None |
|
308 |
}, |
|
309 |
VoteType::HedgehogsPerTeam(number) => { |
|
310 |
match number { |
|
311 |
1...8 => None, |
|
312 |
_ => Some("/callvote hedgehogs: Specify number from 1 to 8.") |
|
313 |
} |
|
314 |
}, |
|
315 |
}; |
|
316 |
match error { |
|
317 |
None => { |
|
13480 | 318 |
let msg = voting_description(&kind); |
13478 | 319 |
let voting = Voting::new(kind, server.room_clients(client_id)); |
13526 | 320 |
server.rooms[room_id].voting = Some(voting); |
13478 | 321 |
server.react(client_id, vec![ |
13480 | 322 |
server_chat(&msg).send_all().in_room(room_id).action(), |
13478 | 323 |
AddVote{ vote: true, is_forced: false}]); |
324 |
} |
|
325 |
Some(msg) => { |
|
326 |
server.react(client_id, vec![ |
|
327 |
server_chat(msg).send_self().action()]) |
|
328 |
} |
|
329 |
} |
|
330 |
} |
|
331 |
Vote(vote) => { |
|
13526 | 332 |
server.react(client_id, vec![AddVote{ vote, is_forced: false }]); |
13478 | 333 |
} |
334 |
ForceVote(vote) => { |
|
13526 | 335 |
let is_forced = server.clients[client_id].is_admin(); |
336 |
server.react(client_id, vec![AddVote{ vote, is_forced }]); |
|
13478 | 337 |
} |
13523 | 338 |
ToggleRestrictJoin | ToggleRestrictTeams | ToggleRegisteredOnly => { |
339 |
if server.clients[client_id].is_master() { |
|
340 |
server.rooms[room_id].flags.toggle(room_message_flag(&message)); |
|
341 |
} |
|
342 |
server.react(client_id, vec![SendRoomUpdate(None)]); |
|
343 |
} |
|
13423 | 344 |
StartGame => { |
13526 | 345 |
server.react(client_id, vec![StartRoomGame(room_id)]); |
13423 | 346 |
} |
347 |
EngineMessage(em) => { |
|
348 |
let mut actions = Vec::new(); |
|
349 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
350 |
if c.teams_in_game > 0 { |
|
351 |
let decoding = decode(&em[..]).unwrap(); |
|
352 |
let messages = by_msg(&decoding); |
|
13443 | 353 |
let valid = messages.filter(|m| is_msg_valid(m, &c.team_indices)); |
354 |
let non_empty = valid.clone().filter(|m| !is_msg_empty(m)); |
|
355 |
let sync_msg = valid.clone().filter(|m| is_msg_timed(m)) |
|
356 |
.last().map(|m| if is_msg_empty(m) {Some(encode(m))} else {None}); |
|
13423 | 357 |
|
358 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
|
359 |
if !em_response.is_empty() { |
|
13428 | 360 |
actions.push(ForwardEngineMessage(vec![em_response]) |
13423 | 361 |
.send_all().in_room(r.id).but_self().action()); |
362 |
} |
|
13427 | 363 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
364 |
if let Some(ref mut info) = r.game_info { |
|
13429 | 365 |
if !em_log.is_empty() { |
13428 | 366 |
info.msg_log.push(em_log); |
367 |
} |
|
13443 | 368 |
if let Some(msg) = sync_msg { |
369 |
info.sync_msg = msg; |
|
13427 | 370 |
} |
371 |
} |
|
13423 | 372 |
} |
373 |
} |
|
374 |
server.react(client_id, actions) |
|
375 |
} |
|
376 |
RoundFinished => { |
|
377 |
let mut actions = Vec::new(); |
|
378 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13520 | 379 |
if c.is_in_game() { |
380 |
c.set_is_in_game(false); |
|
13423 | 381 |
actions.push(ClientFlags("-g".to_string(), vec![c.nick.clone()]). |
382 |
send_all().in_room(r.id).action()); |
|
13426 | 383 |
if r.game_info.is_some() { |
384 |
for team in r.client_teams(c.id) { |
|
385 |
actions.push(SendTeamRemovalMessage(team.name.clone())); |
|
386 |
} |
|
13423 | 387 |
} |
388 |
} |
|
389 |
} |
|
390 |
server.react(client_id, actions) |
|
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13443
diff
changeset
|
391 |
}, |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
392 |
Rnd(v) => { |
13521 | 393 |
let result = rnd_reply(&v); |
394 |
let mut echo = vec!["/rnd".to_string()]; |
|
395 |
echo.extend(v.into_iter()); |
|
396 |
let chat_msg = ChatMsg { |
|
397 |
nick: server.clients[client_id].nick.clone(), |
|
398 |
msg: echo.join(" ") |
|
399 |
}; |
|
400 |
server.react(client_id, vec![ |
|
401 |
chat_msg.send_all().in_room(room_id).action(), |
|
402 |
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
|
403 |
}, |
13419 | 404 |
_ => warn!("Unimplemented!") |
12147 | 405 |
} |
406 |
} |