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