63 |
63 |
64 fn is_msg_empty(msg: &[u8]) -> bool { |
64 fn is_msg_empty(msg: &[u8]) -> bool { |
65 msg.get(1).filter(|t| **t == b'+').is_some() |
65 msg.get(1).filter(|t| **t == b'+').is_some() |
66 } |
66 } |
67 |
67 |
|
68 fn is_msg_timed(msg: &[u8]) -> bool { |
|
69 msg.get(1).filter(|t| !NON_TIMED_MESSAGES.contains(t)).is_some() |
|
70 } |
|
71 |
68 pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
72 pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
69 use protocol::messages::HWProtocolMessage::*; |
73 use protocol::messages::HWProtocolMessage::*; |
70 match message { |
74 match message { |
71 Part(None) => server.react(client_id, vec![ |
75 Part(None) => server.react(client_id, vec![ |
72 MoveToLobby("part".to_string())]), |
76 MoveToLobby("part".to_string())]), |
243 let mut actions = Vec::new(); |
247 let mut actions = Vec::new(); |
244 if let (c, Some(r)) = server.client_and_room(client_id) { |
248 if let (c, Some(r)) = server.client_and_room(client_id) { |
245 if c.teams_in_game > 0 { |
249 if c.teams_in_game > 0 { |
246 let decoding = decode(&em[..]).unwrap(); |
250 let decoding = decode(&em[..]).unwrap(); |
247 let messages = by_msg(&decoding); |
251 let messages = by_msg(&decoding); |
248 let valid = messages.clone().filter(|m| is_msg_valid(m, &c.team_indices)); |
252 let valid = messages.filter(|m| is_msg_valid(m, &c.team_indices)); |
249 let non_empty = messages.filter(|m| !is_msg_empty(m)); |
253 let non_empty = valid.clone().filter(|m| !is_msg_empty(m)); |
250 let last_msg = None; |
254 let sync_msg = valid.clone().filter(|m| is_msg_timed(m)) |
|
255 .last().map(|m| if is_msg_empty(m) {Some(encode(m))} else {None}); |
251 |
256 |
252 let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
257 let em_response = encode(&valid.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
253 if !em_response.is_empty() { |
258 if !em_response.is_empty() { |
254 actions.push(ForwardEngineMessage(vec![em_response]) |
259 actions.push(ForwardEngineMessage(vec![em_response]) |
255 .send_all().in_room(r.id).but_self().action()); |
260 .send_all().in_room(r.id).but_self().action()); |
257 let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
262 let em_log = encode(&non_empty.flat_map(|msg| msg).cloned().collect::<Vec<_>>()); |
258 if let Some(ref mut info) = r.game_info { |
263 if let Some(ref mut info) = r.game_info { |
259 if !em_log.is_empty() { |
264 if !em_log.is_empty() { |
260 info.msg_log.push(em_log); |
265 info.msg_log.push(em_log); |
261 } |
266 } |
262 if last_msg.is_some() { |
267 if let Some(msg) = sync_msg { |
263 info.last_msg = last_msg; |
268 info.sync_msg = msg; |
264 } |
269 } |
265 } |
270 } |
266 } |
271 } |
267 } |
272 } |
268 server.react(client_id, actions) |
273 server.react(client_id, actions) |