4 use super::{ |
4 use super::{ |
5 actions::{Action, Action::*}, |
5 actions::{Action, Action::*}, |
6 core::HWServer, |
6 core::HWServer, |
7 coretypes::ClientId, |
7 coretypes::ClientId, |
8 }; |
8 }; |
9 use crate::protocol::messages::{HWProtocolMessage, HWServerMessage::*}; |
9 use crate::{ |
|
10 protocol::messages::{HWProtocolMessage, HWServerMessage::*}, |
|
11 server::actions::PendingMessage, |
|
12 }; |
10 use log::*; |
13 use log::*; |
11 |
14 |
12 mod checker; |
15 mod checker; |
13 mod common; |
16 mod common; |
14 mod inroom; |
17 mod inroom; |
15 mod lobby; |
18 mod lobby; |
16 mod loggingin; |
19 mod loggingin; |
17 |
20 |
18 pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
21 pub struct Response { |
|
22 client_id: ClientId, |
|
23 messages: Vec<PendingMessage>, |
|
24 } |
|
25 |
|
26 impl Response { |
|
27 pub fn new(client_id: ClientId) -> Self { |
|
28 Self { |
|
29 client_id, |
|
30 messages: vec![], |
|
31 } |
|
32 } |
|
33 |
|
34 pub fn client_id(&self) -> ClientId { |
|
35 self.client_id |
|
36 } |
|
37 |
|
38 pub fn add(&mut self, message: PendingMessage) { |
|
39 self.messages.push(message) |
|
40 } |
|
41 } |
|
42 |
|
43 pub fn handle( |
|
44 server: &mut HWServer, |
|
45 client_id: ClientId, |
|
46 response: &mut Response, |
|
47 message: HWProtocolMessage, |
|
48 ) { |
19 match message { |
49 match message { |
20 HWProtocolMessage::Ping => server.react(client_id, vec![Pong.send_self().action()]), |
50 HWProtocolMessage::Ping => { |
|
51 response.add(Pong.send_self()); |
|
52 server.react(client_id, vec![Pong.send_self().action()]) |
|
53 } |
21 HWProtocolMessage::Quit(Some(msg)) => { |
54 HWProtocolMessage::Quit(Some(msg)) => { |
22 server.react(client_id, vec![ByeClient("User quit: ".to_string() + &msg)]) |
55 server.react(client_id, vec![ByeClient("User quit: ".to_string() + &msg)]) |
23 } |
56 } |
24 HWProtocolMessage::Quit(None) => { |
57 HWProtocolMessage::Quit(None) => { |
25 server.react(client_id, vec![ByeClient("User quit".to_string())]) |
58 server.react(client_id, vec![ByeClient("User quit".to_string())]) |
26 } |
59 } |
27 HWProtocolMessage::Malformed => warn!("Malformed/unknown message"), |
60 HWProtocolMessage::Malformed => warn!("Malformed/unknown message"), |
28 HWProtocolMessage::Empty => warn!("Empty message"), |
61 HWProtocolMessage::Empty => warn!("Empty message"), |
29 _ => match server.clients[client_id].room_id { |
62 _ => match server.clients[client_id].room_id { |
30 None => loggingin::handle(server, client_id, message), |
63 None => loggingin::handle(server, client_id, response, message), |
31 Some(id) if id == server.lobby_id => lobby::handle(server, client_id, message), |
64 Some(id) if id == server.lobby_id => { |
32 Some(id) => inroom::handle(server, client_id, id, message), |
65 lobby::handle(server, client_id, response, message) |
|
66 } |
|
67 Some(id) => inroom::handle(server, client_id, response, id, message), |
33 }, |
68 }, |
34 } |
69 } |
35 } |
70 } |