author | alfadur |
Mon, 16 Jul 2018 22:32:04 +0300 | |
changeset 13526 | ba5211dddb21 |
parent 13525 | 1ee192f13456 |
child 13528 | 8c5dd562c9f7 |
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, |
11 |
room::HWRoom, |
|
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 |
||
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 |
||
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 |
||
13526 | 84 |
pub fn handle(server: &mut HWServer, client_id: ClientId, room_id: RoomId, message: HWProtocolMessage) { |
13421 | 85 |
use protocol::messages::HWProtocolMessage::*; |
12152 | 86 |
match message { |
13424 | 87 |
Part(None) => server.react(client_id, vec![ |
13421 | 88 |
MoveToLobby("part".to_string())]), |
13424 | 89 |
Part(Some(msg)) => server.react(client_id, vec![ |
13421 | 90 |
MoveToLobby(format!("part: {}", msg))]), |
91 |
Chat(msg) => { |
|
13424 | 92 |
let actions = { |
93 |
let c = &mut server.clients[client_id]; |
|
13449
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13448
diff
changeset
|
94 |
let chat_msg = ChatMsg {nick: c.nick.clone(), msg: msg}; |
13526 | 95 |
vec![chat_msg.send_all().in_room(room_id).but_self().action()] |
13424 | 96 |
}; |
97 |
server.react(client_id, actions); |
|
13421 | 98 |
}, |
13482 | 99 |
Fix => { |
100 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13525 | 101 |
if c.is_admin() { r.is_fixed = true } |
13482 | 102 |
} |
103 |
} |
|
104 |
Unfix => { |
|
105 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13525 | 106 |
if c.is_admin() { r.is_fixed = false } |
13482 | 107 |
} |
108 |
} |
|
109 |
Greeting(text) => { |
|
110 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13525 | 111 |
if c.is_admin() || c.is_master() && !r.is_fixed { |
13482 | 112 |
r.greeting = text |
113 |
} |
|
114 |
} |
|
115 |
} |
|
13421 | 116 |
RoomName(new_name) => { |
117 |
let actions = |
|
118 |
if is_name_illegal(&new_name) { |
|
119 |
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())] |
|
13482 | 120 |
} else if server.room(client_id).map(|r| r.is_fixed).unwrap_or(false) { |
121 |
vec![Warn("Access denied.".to_string())] |
|
13421 | 122 |
} else if server.has_room(&new_name) { |
123 |
vec![Warn("A room with the same name already exists.".to_string())] |
|
124 |
} else { |
|
125 |
let mut old_name = new_name.clone(); |
|
13427 | 126 |
if let (_, Some(r)) = server.client_and_room(client_id) { |
13421 | 127 |
swap(&mut r.name, &mut old_name); |
128 |
vec![SendRoomUpdate(Some(old_name))] |
|
129 |
} else { |
|
130 |
Vec::new() |
|
131 |
} |
|
132 |
}; |
|
13424 | 133 |
server.react(client_id, actions); |
134 |
}, |
|
135 |
ToggleReady => { |
|
136 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13525 | 137 |
let flags = if c.is_ready() { |
13424 | 138 |
r.ready_players_number -= 1; |
139 |
"-r" |
|
140 |
} else { |
|
141 |
r.ready_players_number += 1; |
|
142 |
"+r" |
|
143 |
}; |
|
13525 | 144 |
let is_ready = !c.is_ready(); |
145 |
c.set_is_ready(is_ready); |
|
13482 | 146 |
let mut v = |
147 |
vec![ClientFlags(flags.to_string(), vec![c.nick.clone()]) |
|
148 |
.send_all().in_room(r.id).action()]; |
|
149 |
if r.is_fixed && r.ready_players_number as u32 == r.players_number { |
|
150 |
v.push(StartRoomGame(r.id)) |
|
151 |
} |
|
152 |
v |
|
13424 | 153 |
} else { |
154 |
Vec::new() |
|
155 |
}; |
|
156 |
server.react(client_id, actions); |
|
13421 | 157 |
} |
13427 | 158 |
AddTeam(info) => { |
13424 | 159 |
let mut actions = Vec::new(); |
160 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
161 |
if r.teams.len() >= r.team_limit as usize { |
|
162 |
actions.push(Warn("Too many teams!".to_string())) |
|
163 |
} else if r.addable_hedgehogs() == 0 { |
|
164 |
actions.push(Warn("Too many hedgehogs!".to_string())) |
|
165 |
} else if r.find_team(|t| t.name == info.name) != None { |
|
166 |
actions.push(Warn("There's already a team with same name in the list.".to_string())) |
|
13428 | 167 |
} else if r.game_info.is_some() { |
13424 | 168 |
actions.push(Warn("Joining not possible: Round is in progress.".to_string())) |
169 |
} else { |
|
170 |
let team = r.add_team(c.id, info); |
|
171 |
c.teams_in_game += 1; |
|
172 |
c.clan = Some(team.color); |
|
173 |
actions.push(TeamAccepted(team.name.clone()) |
|
174 |
.send_self().action()); |
|
175 |
actions.push(TeamAdd(HWRoom::team_info(&c, team)) |
|
176 |
.send_all().in_room(room_id).but_self().action()); |
|
177 |
actions.push(TeamColor(team.name.clone(), team.color) |
|
178 |
.send_all().in_room(room_id).action()); |
|
179 |
actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
180 |
.send_all().in_room(room_id).action()); |
|
181 |
actions.push(SendRoomUpdate(None)); |
|
182 |
} |
|
183 |
} |
|
184 |
server.react(client_id, actions); |
|
185 |
}, |
|
186 |
RemoveTeam(name) => { |
|
187 |
let mut actions = Vec::new(); |
|
188 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
189 |
match r.find_team_owner(&name) { |
|
190 |
None => |
|
191 |
actions.push(Warn("Error: The team you tried to remove does not exist.".to_string())), |
|
192 |
Some((id, _)) if id != client_id => |
|
193 |
actions.push(Warn("You can't remove a team you don't own.".to_string())), |
|
194 |
Some((_, name)) => { |
|
195 |
c.teams_in_game -= 1; |
|
196 |
c.clan = r.find_team_color(c.id); |
|
197 |
actions.push(Action::RemoveTeam(name.to_string())); |
|
198 |
} |
|
199 |
} |
|
200 |
}; |
|
201 |
server.react(client_id, actions); |
|
202 |
}, |
|
203 |
SetHedgehogsNumber(team_name, number) => { |
|
204 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
205 |
let addable_hedgehogs = r.addable_hedgehogs(); |
|
206 |
if let Some((_, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) { |
|
13525 | 207 |
if !c.is_master() { |
13424 | 208 |
vec![ProtocolError("You're not the room master!".to_string())] |
209 |
} else if number < 1 || number > 8 |
|
210 |
|| number > addable_hedgehogs + team.hedgehogs_number { |
|
211 |
vec![HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
212 |
.send_self().action()] |
|
213 |
} else { |
|
214 |
team.hedgehogs_number = number; |
|
215 |
vec![HedgehogsNumber(team.name.clone(), number) |
|
216 |
.send_all().in_room(room_id).but_self().action()] |
|
217 |
} |
|
218 |
} else { |
|
219 |
vec![(Warn("No such team.".to_string()))] |
|
220 |
} |
|
221 |
} else { |
|
222 |
Vec::new() |
|
223 |
}; |
|
224 |
server.react(client_id, actions); |
|
225 |
}, |
|
226 |
SetTeamColor(team_name, color) => { |
|
227 |
let mut owner_id = None; |
|
228 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
229 |
if let Some((owner, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) { |
|
13525 | 230 |
if !c.is_master() { |
13424 | 231 |
vec![ProtocolError("You're not the room master!".to_string())] |
232 |
} else if false { |
|
233 |
Vec::new() |
|
234 |
} else { |
|
235 |
owner_id = Some(owner); |
|
236 |
team.color = color; |
|
237 |
vec![TeamColor(team.name.clone(), color) |
|
238 |
.send_all().in_room(room_id).but_self().action()] |
|
239 |
} |
|
240 |
} else { |
|
241 |
vec![(Warn("No such team.".to_string()))] |
|
242 |
} |
|
243 |
} else { |
|
244 |
Vec::new() |
|
245 |
}; |
|
246 |
||
247 |
if let Some(id) = owner_id { |
|
248 |
server.clients[id].clan = Some(color); |
|
249 |
} |
|
250 |
||
251 |
server.react(client_id, actions); |
|
13427 | 252 |
}, |
253 |
Cfg(cfg) => { |
|
254 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13482 | 255 |
if r.is_fixed { |
256 |
vec![Warn("Access denied.".to_string())] |
|
13525 | 257 |
} else if !c.is_master() { |
13427 | 258 |
vec![ProtocolError("You're not the room master!".to_string())] |
259 |
} else { |
|
13444 | 260 |
let v = vec![cfg.to_server_msg() |
261 |
.send_all().in_room(r.id).but_self().action()]; |
|
262 |
r.set_config(cfg); |
|
263 |
v |
|
13427 | 264 |
} |
265 |
} else { |
|
266 |
Vec::new() |
|
267 |
}; |
|
268 |
server.react(client_id, actions); |
|
13424 | 269 |
} |
13483 | 270 |
CallVote(None) => { |
271 |
server.react(client_id, vec![ |
|
272 |
server_chat("Available callvote commands: kick <nickname>, map <name>, pause, newseed, hedgehogs <number>") |
|
273 |
.send_self().action()]) |
|
274 |
} |
|
275 |
CallVote(Some(kind)) => { |
|
13526 | 276 |
let is_in_game = server.rooms[room_id].game_info.is_some(); |
13483 | 277 |
let error = match &kind { |
278 |
VoteType::Kick(nick) => { |
|
279 |
if server.find_client(&nick).filter(|c| c.room_id == Some(room_id)).is_some() { |
|
280 |
None |
|
281 |
} else { |
|
282 |
Some("/callvote kick: No such user!") |
|
283 |
} |
|
284 |
}, |
|
285 |
VoteType::Map(None) => { |
|
286 |
Some("/callvote map: Not implemented") |
|
287 |
}, |
|
288 |
VoteType::Map(Some(name)) => { |
|
289 |
Some("/callvote map: Not implemented") |
|
290 |
}, |
|
291 |
VoteType::Pause => { |
|
292 |
if is_in_game { |
|
293 |
None |
|
294 |
} else { |
|
295 |
Some("/callvote pause: No game in progress!") |
|
296 |
} |
|
297 |
}, |
|
298 |
VoteType::NewSeed => { |
|
299 |
None |
|
300 |
}, |
|
301 |
VoteType::HedgehogsPerTeam(number) => { |
|
302 |
match number { |
|
303 |
1...8 => None, |
|
304 |
_ => Some("/callvote hedgehogs: Specify number from 1 to 8.") |
|
305 |
} |
|
306 |
}, |
|
307 |
}; |
|
308 |
match error { |
|
309 |
None => { |
|
13485 | 310 |
let msg = voting_description(&kind); |
13483 | 311 |
let voting = Voting::new(kind, server.room_clients(client_id)); |
312 |
server.room(client_id).unwrap().voting = Some(voting); |
|
313 |
server.react(client_id, vec![ |
|
13485 | 314 |
server_chat(&msg).send_all().in_room(room_id).action(), |
13483 | 315 |
AddVote{ vote: true, is_forced: false}]); |
316 |
} |
|
317 |
Some(msg) => { |
|
318 |
server.react(client_id, vec![ |
|
319 |
server_chat(msg).send_self().action()]) |
|
320 |
} |
|
321 |
} |
|
322 |
} |
|
323 |
Vote(vote) => { |
|
324 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
325 |
vec![AddVote{ vote, is_forced: false }] |
|
326 |
} else { |
|
327 |
Vec::new() |
|
328 |
}; |
|
329 |
server.react(client_id, actions); |
|
330 |
} |
|
331 |
ForceVote(vote) => { |
|
332 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13525 | 333 |
vec![AddVote{ vote, is_forced: c.is_admin()} ] |
13483 | 334 |
} else { |
335 |
Vec::new() |
|
336 |
}; |
|
337 |
server.react(client_id, actions); |
|
338 |
} |
|
13428 | 339 |
StartGame => { |
340 |
let actions = if let (_, Some(r)) = server.client_and_room(client_id) { |
|
341 |
vec![StartRoomGame(r.id)] |
|
342 |
} else { |
|
343 |
Vec::new() |
|
344 |
}; |
|
345 |
server.react(client_id, actions); |
|
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); |
|
13448 | 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}); |
|
13428 | 357 |
|
358 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
|
359 |
if !em_response.is_empty() { |
|
13433 | 360 |
actions.push(ForwardEngineMessage(vec![em_response]) |
13428 | 361 |
.send_all().in_room(r.id).but_self().action()); |
362 |
} |
|
13432 | 363 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
364 |
if let Some(ref mut info) = r.game_info { |
|
13434 | 365 |
if !em_log.is_empty() { |
13433 | 366 |
info.msg_log.push(em_log); |
367 |
} |
|
13448 | 368 |
if let Some(msg) = sync_msg { |
369 |
info.sync_msg = msg; |
|
13432 | 370 |
} |
371 |
} |
|
13428 | 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) { |
|
13525 | 379 |
if c.is_in_game() { |
380 |
c.set_is_in_game(false); |
|
13428 | 381 |
actions.push(ClientFlags("-g".to_string(), vec![c.nick.clone()]). |
382 |
send_all().in_room(r.id).action()); |
|
13431 | 383 |
if r.game_info.is_some() { |
384 |
for team in r.client_teams(c.id) { |
|
385 |
actions.push(SendTeamRemovalMessage(team.name.clone())); |
|
386 |
} |
|
13428 | 387 |
} |
388 |
} |
|
389 |
} |
|
390 |
server.react(client_id, actions) |
|
13449
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13448
diff
changeset
|
391 |
}, |
13450
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13449
diff
changeset
|
392 |
Rnd(v) => { |
13526 | 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()]) |
|
13450
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13449
diff
changeset
|
403 |
}, |
13424 | 404 |
_ => warn!("Unimplemented!") |
12152 | 405 |
} |
406 |
} |