author | Wuzzy <Wuzzy2@mail.ru> |
Tue, 17 Jul 2018 21:17:33 +0200 | |
changeset 13506 | bdac56fb6f1a |
parent 13460 | fb37745c5bca |
child 13486 | 1ee192f13456 |
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::{ |
13450 | 9 |
coretypes::{ClientId, Voting, VoteType}, |
13419 | 10 |
server::HWServer, |
11 |
room::HWRoom, |
|
12 |
actions::{Action, Action::*} |
|
13 |
}; |
|
13416 | 14 |
use utils::is_name_illegal; |
15 |
use std::mem::swap; |
|
13423 | 16 |
use base64::{encode, decode}; |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
17 |
use super::common::rnd_action; |
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 |
||
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 |
||
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 |
||
13419 | 84 |
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
13416 | 85 |
use protocol::messages::HWProtocolMessage::*; |
12147 | 86 |
match message { |
13419 | 87 |
Part(None) => server.react(client_id, vec![ |
13416 | 88 |
MoveToLobby("part".to_string())]), |
13419 | 89 |
Part(Some(msg)) => server.react(client_id, vec![ |
13416 | 90 |
MoveToLobby(format!("part: {}", msg))]), |
91 |
Chat(msg) => { |
|
13419 | 92 |
let actions = { |
93 |
let c = &mut server.clients[client_id]; |
|
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13443
diff
changeset
|
94 |
let chat_msg = ChatMsg {nick: c.nick.clone(), msg: msg}; |
13419 | 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); |
|
13416 | 102 |
}, |
13447 | 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 |
} |
|
13416 | 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())] |
|
13447 | 124 |
} else if server.room(client_id).map(|r| r.is_fixed).unwrap_or(false) { |
125 |
vec![Warn("Access denied.".to_string())] |
|
13416 | 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(); |
|
13422 | 130 |
if let (_, Some(r)) = server.client_and_room(client_id) { |
13416 | 131 |
swap(&mut r.name, &mut old_name); |
132 |
vec![SendRoomUpdate(Some(old_name))] |
|
133 |
} else { |
|
134 |
Vec::new() |
|
135 |
} |
|
136 |
}; |
|
13419 | 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; |
|
13447 | 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 |
|
13419 | 156 |
} else { |
157 |
Vec::new() |
|
158 |
}; |
|
159 |
server.react(client_id, actions); |
|
13416 | 160 |
} |
13422 | 161 |
AddTeam(info) => { |
13419 | 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())) |
|
13423 | 171 |
} else if r.game_info.is_some() { |
13419 | 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); |
|
13422 | 258 |
}, |
259 |
Cfg(cfg) => { |
|
260 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13447 | 261 |
if r.is_fixed { |
262 |
vec![Warn("Access denied.".to_string())] |
|
263 |
} else if !c.is_master { |
|
13422 | 264 |
vec![ProtocolError("You're not the room master!".to_string())] |
265 |
} else { |
|
13439 | 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 |
|
13422 | 270 |
} |
271 |
} else { |
|
272 |
Vec::new() |
|
273 |
}; |
|
274 |
server.react(client_id, actions); |
|
13419 | 275 |
} |
13450 | 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 => { |
|
13460 | 317 |
let msg = voting_description(&kind); |
13450 | 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![ |
|
13460 | 321 |
server_chat(&msg).send_all().in_room(room_id).action(), |
13450 | 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 |
} |
|
13423 | 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); |
|
13443 | 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}); |
|
13423 | 364 |
|
365 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
|
366 |
if !em_response.is_empty() { |
|
13428 | 367 |
actions.push(ForwardEngineMessage(vec![em_response]) |
13423 | 368 |
.send_all().in_room(r.id).but_self().action()); |
369 |
} |
|
13427 | 370 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
371 |
if let Some(ref mut info) = r.game_info { |
|
13429 | 372 |
if !em_log.is_empty() { |
13428 | 373 |
info.msg_log.push(em_log); |
374 |
} |
|
13443 | 375 |
if let Some(msg) = sync_msg { |
376 |
info.sync_msg = msg; |
|
13427 | 377 |
} |
378 |
} |
|
13423 | 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()); |
|
13426 | 390 |
if r.game_info.is_some() { |
391 |
for team in r.client_teams(c.id) { |
|
392 |
actions.push(SendTeamRemovalMessage(team.name.clone())); |
|
393 |
} |
|
13423 | 394 |
} |
395 |
} |
|
396 |
} |
|
397 |
server.react(client_id, actions) |
|
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13443
diff
changeset
|
398 |
}, |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
399 |
Rnd(v) => { |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
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:
13444
diff
changeset
|
401 |
server.react(client_id, actions) |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
402 |
}, |
13419 | 403 |
_ => warn!("Unimplemented!") |
12147 | 404 |
} |
405 |
} |