author | Marcin Mielniczuk <marmistrz.dev@zoho.eu> |
Fri, 06 Jul 2018 16:59:52 +0200 | |
changeset 13445 | d3c86ade3d4d |
parent 13444 | 914f9b970f4d |
child 13447 | f748a72432f2 |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
13416 | 3 |
use protocol::messages::{ |
4 |
HWProtocolMessage, |
|
5 |
HWServerMessage::* |
|
6 |
}; |
|
13419 | 7 |
use server::{ |
8 |
server::HWServer, |
|
9 |
client::ClientId, |
|
10 |
room::HWRoom, |
|
11 |
actions::{Action, Action::*} |
|
12 |
}; |
|
13416 | 13 |
use utils::is_name_illegal; |
14 |
use std::mem::swap; |
|
13423 | 15 |
use base64::{encode, decode}; |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
16 |
use super::common::rnd_action; |
13423 | 17 |
|
18 |
#[derive(Clone)] |
|
19 |
struct ByMsg<'a> { |
|
20 |
messages: &'a[u8] |
|
21 |
} |
|
22 |
||
23 |
impl <'a> Iterator for ByMsg<'a> { |
|
24 |
type Item = &'a[u8]; |
|
25 |
||
26 |
fn next(&mut self) -> Option<<Self as Iterator>::Item> { |
|
27 |
if let Some(size) = self.messages.get(0) { |
|
28 |
let (msg, next) = self.messages.split_at(*size as usize + 1); |
|
29 |
self.messages = next; |
|
30 |
Some(msg) |
|
31 |
} else { |
|
32 |
None |
|
33 |
} |
|
34 |
} |
|
35 |
} |
|
36 |
||
37 |
fn by_msg(source: &Vec<u8>) -> ByMsg { |
|
38 |
ByMsg {messages: &source[..]} |
|
39 |
} |
|
40 |
||
41 |
const VALID_MESSAGES: &[u8] = |
|
42 |
b"M#+LlRrUuDdZzAaSjJ,NpPwtgfhbc12345\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A"; |
|
43 |
const NON_TIMED_MESSAGES: &[u8] = b"M#hb"; |
|
44 |
||
13429 | 45 |
#[cfg(canhazslicepatterns)] |
13423 | 46 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool { |
13424 | 47 |
match msg { |
48 |
[size, typ, body..] => VALID_MESSAGES.contains(typ) |
|
49 |
&& match body { |
|
13423 | 50 |
[1...8, team, ..] if *typ == b'h' => team_indices.contains(team), |
51 |
_ => *typ != b'h' |
|
13424 | 52 |
}, |
53 |
_ => false |
|
13423 | 54 |
} |
55 |
} |
|
56 |
||
13429 | 57 |
fn is_msg_valid(msg: &[u8], team_indices: &[u8]) -> bool { |
58 |
if let Some(typ) = msg.get(1) { |
|
59 |
VALID_MESSAGES.contains(typ) |
|
60 |
} else { |
|
61 |
false |
|
62 |
} |
|
63 |
} |
|
64 |
||
13423 | 65 |
fn is_msg_empty(msg: &[u8]) -> bool { |
13429 | 66 |
msg.get(1).filter(|t| **t == b'+').is_some() |
13423 | 67 |
} |
12147 | 68 |
|
13443 | 69 |
fn is_msg_timed(msg: &[u8]) -> bool { |
70 |
msg.get(1).filter(|t| !NON_TIMED_MESSAGES.contains(t)).is_some() |
|
71 |
} |
|
72 |
||
13419 | 73 |
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
13416 | 74 |
use protocol::messages::HWProtocolMessage::*; |
12147 | 75 |
match message { |
13419 | 76 |
Part(None) => server.react(client_id, vec![ |
13416 | 77 |
MoveToLobby("part".to_string())]), |
13419 | 78 |
Part(Some(msg)) => server.react(client_id, vec![ |
13416 | 79 |
MoveToLobby(format!("part: {}", msg))]), |
80 |
Chat(msg) => { |
|
13419 | 81 |
let actions = { |
82 |
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
|
83 |
let chat_msg = ChatMsg {nick: c.nick.clone(), msg: msg}; |
13419 | 84 |
if let Some(room_id) = c.room_id { |
85 |
vec![chat_msg.send_all().in_room(room_id).but_self().action()] |
|
86 |
} else { |
|
87 |
Vec::new() |
|
88 |
} |
|
89 |
}; |
|
90 |
server.react(client_id, actions); |
|
13416 | 91 |
}, |
92 |
RoomName(new_name) => { |
|
93 |
let actions = |
|
94 |
if is_name_illegal(&new_name) { |
|
95 |
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())] |
|
96 |
} else if server.has_room(&new_name) { |
|
97 |
vec![Warn("A room with the same name already exists.".to_string())] |
|
98 |
} else { |
|
99 |
let mut old_name = new_name.clone(); |
|
13422 | 100 |
if let (_, Some(r)) = server.client_and_room(client_id) { |
13416 | 101 |
swap(&mut r.name, &mut old_name); |
102 |
vec![SendRoomUpdate(Some(old_name))] |
|
103 |
} else { |
|
104 |
Vec::new() |
|
105 |
} |
|
106 |
}; |
|
13419 | 107 |
server.react(client_id, actions); |
108 |
}, |
|
109 |
ToggleReady => { |
|
110 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
111 |
let flags = if c.is_ready { |
|
112 |
r.ready_players_number -= 1; |
|
113 |
"-r" |
|
114 |
} else { |
|
115 |
r.ready_players_number += 1; |
|
116 |
"+r" |
|
117 |
}; |
|
118 |
c.is_ready = !c.is_ready; |
|
119 |
vec![ClientFlags(flags.to_string(), vec![c.nick.clone()]) |
|
120 |
.send_all().in_room(r.id).action()] |
|
121 |
} else { |
|
122 |
Vec::new() |
|
123 |
}; |
|
124 |
server.react(client_id, actions); |
|
13416 | 125 |
} |
13422 | 126 |
AddTeam(info) => { |
13419 | 127 |
let mut actions = Vec::new(); |
128 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
129 |
let room_id = r.id; |
|
130 |
if r.teams.len() >= r.team_limit as usize { |
|
131 |
actions.push(Warn("Too many teams!".to_string())) |
|
132 |
} else if r.addable_hedgehogs() == 0 { |
|
133 |
actions.push(Warn("Too many hedgehogs!".to_string())) |
|
134 |
} else if r.find_team(|t| t.name == info.name) != None { |
|
135 |
actions.push(Warn("There's already a team with same name in the list.".to_string())) |
|
13423 | 136 |
} else if r.game_info.is_some() { |
13419 | 137 |
actions.push(Warn("Joining not possible: Round is in progress.".to_string())) |
138 |
} else { |
|
139 |
let team = r.add_team(c.id, info); |
|
140 |
c.teams_in_game += 1; |
|
141 |
c.clan = Some(team.color); |
|
142 |
actions.push(TeamAccepted(team.name.clone()) |
|
143 |
.send_self().action()); |
|
144 |
actions.push(TeamAdd(HWRoom::team_info(&c, team)) |
|
145 |
.send_all().in_room(room_id).but_self().action()); |
|
146 |
actions.push(TeamColor(team.name.clone(), team.color) |
|
147 |
.send_all().in_room(room_id).action()); |
|
148 |
actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
149 |
.send_all().in_room(room_id).action()); |
|
150 |
actions.push(SendRoomUpdate(None)); |
|
151 |
} |
|
152 |
} |
|
153 |
server.react(client_id, actions); |
|
154 |
}, |
|
155 |
RemoveTeam(name) => { |
|
156 |
let mut actions = Vec::new(); |
|
157 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
158 |
match r.find_team_owner(&name) { |
|
159 |
None => |
|
160 |
actions.push(Warn("Error: The team you tried to remove does not exist.".to_string())), |
|
161 |
Some((id, _)) if id != client_id => |
|
162 |
actions.push(Warn("You can't remove a team you don't own.".to_string())), |
|
163 |
Some((_, name)) => { |
|
164 |
c.teams_in_game -= 1; |
|
165 |
c.clan = r.find_team_color(c.id); |
|
166 |
actions.push(Action::RemoveTeam(name.to_string())); |
|
167 |
} |
|
168 |
} |
|
169 |
}; |
|
170 |
server.react(client_id, actions); |
|
171 |
}, |
|
172 |
SetHedgehogsNumber(team_name, number) => { |
|
173 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
174 |
let room_id = r.id; |
|
175 |
let addable_hedgehogs = r.addable_hedgehogs(); |
|
176 |
if let Some((_, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) { |
|
177 |
if !c.is_master { |
|
178 |
vec![ProtocolError("You're not the room master!".to_string())] |
|
179 |
} else if number < 1 || number > 8 |
|
180 |
|| number > addable_hedgehogs + team.hedgehogs_number { |
|
181 |
vec![HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
182 |
.send_self().action()] |
|
183 |
} else { |
|
184 |
team.hedgehogs_number = number; |
|
185 |
vec![HedgehogsNumber(team.name.clone(), number) |
|
186 |
.send_all().in_room(room_id).but_self().action()] |
|
187 |
} |
|
188 |
} else { |
|
189 |
vec![(Warn("No such team.".to_string()))] |
|
190 |
} |
|
191 |
} else { |
|
192 |
Vec::new() |
|
193 |
}; |
|
194 |
server.react(client_id, actions); |
|
195 |
}, |
|
196 |
SetTeamColor(team_name, color) => { |
|
197 |
let mut owner_id = None; |
|
198 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
199 |
let room_id = r.id; |
|
200 |
if let Some((owner, mut team)) = r.find_team_and_owner_mut(|t| t.name == team_name) { |
|
201 |
if !c.is_master { |
|
202 |
vec![ProtocolError("You're not the room master!".to_string())] |
|
203 |
} else if false { |
|
204 |
Vec::new() |
|
205 |
} else { |
|
206 |
owner_id = Some(owner); |
|
207 |
team.color = color; |
|
208 |
vec![TeamColor(team.name.clone(), color) |
|
209 |
.send_all().in_room(room_id).but_self().action()] |
|
210 |
} |
|
211 |
} else { |
|
212 |
vec![(Warn("No such team.".to_string()))] |
|
213 |
} |
|
214 |
} else { |
|
215 |
Vec::new() |
|
216 |
}; |
|
217 |
||
218 |
if let Some(id) = owner_id { |
|
219 |
server.clients[id].clan = Some(color); |
|
220 |
} |
|
221 |
||
222 |
server.react(client_id, actions); |
|
13422 | 223 |
}, |
224 |
Cfg(cfg) => { |
|
225 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
226 |
if !c.is_master { |
|
227 |
vec![ProtocolError("You're not the room master!".to_string())] |
|
228 |
} else { |
|
13439 | 229 |
let v = vec![cfg.to_server_msg() |
230 |
.send_all().in_room(r.id).but_self().action()]; |
|
231 |
r.set_config(cfg); |
|
232 |
v |
|
13422 | 233 |
} |
234 |
} else { |
|
235 |
Vec::new() |
|
236 |
}; |
|
237 |
server.react(client_id, actions); |
|
13419 | 238 |
} |
13423 | 239 |
StartGame => { |
240 |
let actions = if let (_, Some(r)) = server.client_and_room(client_id) { |
|
241 |
vec![StartRoomGame(r.id)] |
|
242 |
} else { |
|
243 |
Vec::new() |
|
244 |
}; |
|
245 |
server.react(client_id, actions); |
|
246 |
} |
|
247 |
EngineMessage(em) => { |
|
248 |
let mut actions = Vec::new(); |
|
249 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
250 |
if c.teams_in_game > 0 { |
|
251 |
let decoding = decode(&em[..]).unwrap(); |
|
252 |
let messages = by_msg(&decoding); |
|
13443 | 253 |
let valid = messages.filter(|m| is_msg_valid(m, &c.team_indices)); |
254 |
let non_empty = valid.clone().filter(|m| !is_msg_empty(m)); |
|
255 |
let sync_msg = valid.clone().filter(|m| is_msg_timed(m)) |
|
256 |
.last().map(|m| if is_msg_empty(m) {Some(encode(m))} else {None}); |
|
13423 | 257 |
|
258 |
let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
|
259 |
if !em_response.is_empty() { |
|
13428 | 260 |
actions.push(ForwardEngineMessage(vec![em_response]) |
13423 | 261 |
.send_all().in_room(r.id).but_self().action()); |
262 |
} |
|
13427 | 263 |
let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
264 |
if let Some(ref mut info) = r.game_info { |
|
13429 | 265 |
if !em_log.is_empty() { |
13428 | 266 |
info.msg_log.push(em_log); |
267 |
} |
|
13443 | 268 |
if let Some(msg) = sync_msg { |
269 |
info.sync_msg = msg; |
|
13427 | 270 |
} |
271 |
} |
|
13423 | 272 |
} |
273 |
} |
|
274 |
server.react(client_id, actions) |
|
275 |
} |
|
276 |
RoundFinished => { |
|
277 |
let mut actions = Vec::new(); |
|
278 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
279 |
if c.is_in_game { |
|
280 |
c.is_in_game = false; |
|
281 |
actions.push(ClientFlags("-g".to_string(), vec![c.nick.clone()]). |
|
282 |
send_all().in_room(r.id).action()); |
|
13426 | 283 |
if r.game_info.is_some() { |
284 |
for team in r.client_teams(c.id) { |
|
285 |
actions.push(SendTeamRemovalMessage(team.name.clone())); |
|
286 |
} |
|
13423 | 287 |
} |
288 |
} |
|
289 |
} |
|
290 |
server.react(client_id, actions) |
|
13444
914f9b970f4d
Implement server-side logic for Rnd
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13443
diff
changeset
|
291 |
}, |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
292 |
Rnd(v) => { |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
293 |
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
|
294 |
server.react(client_id, actions) |
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
295 |
}, |
13419 | 296 |
_ => warn!("Unimplemented!") |
12147 | 297 |
} |
298 |
} |