author | unc0rr |
Fri, 27 Jan 2017 23:03:31 +0300 | |
changeset 12146 | 8d8fb85bc09c |
parent 12145 | a482c7a5f6e3 |
child 12147 | 03ccb89820f3 |
permissions | -rw-r--r-- |
12144 | 1 |
use mio; |
2 |
use std::io::Write; |
|
3 |
use std::io; |
|
4 |
||
5 |
use super::server::HWServer; |
|
6 |
use protocol::messages::HWProtocolMessage; |
|
7 |
use protocol::messages::HWServerMessage::*; |
|
8 |
use super::handlers; |
|
12138 | 9 |
|
10 |
pub enum Action { |
|
11 |
SendMe(String), |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
12 |
SendAllButMe(String), |
12139 | 13 |
RemoveClient, |
14 |
ByeClient(String), |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
15 |
ReactProtocolMessage(HWProtocolMessage), |
12145 | 16 |
CheckRegistered, |
17 |
JoinLobby, |
|
12138 | 18 |
} |
12144 | 19 |
|
20 |
use self::Action::*; |
|
21 |
||
22 |
pub fn run_action(server: &mut HWServer, token: mio::Token, poll: &mio::Poll, action: Action) { |
|
23 |
match action { |
|
24 |
SendMe(msg) => |
|
25 |
server.send(token, &msg), |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
26 |
SendAllButMe(msg) => { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
27 |
for c in server.clients.iter_mut() { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
28 |
if c.id != token { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
29 |
c.send_string(&msg) |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
30 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
31 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
32 |
}, |
12144 | 33 |
ByeClient(msg) => { |
34 |
server.react(token, poll, vec![ |
|
35 |
SendMe(Bye(&msg).to_raw_protocol()), |
|
36 |
RemoveClient, |
|
37 |
]); |
|
38 |
}, |
|
39 |
RemoveClient => { |
|
40 |
server.clients[token].deregister(poll); |
|
41 |
server.clients.remove(token); |
|
42 |
}, |
|
43 |
ReactProtocolMessage(msg) => |
|
44 |
handlers::handle(server, token, poll, msg), |
|
12145 | 45 |
CheckRegistered => |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
46 |
if server.clients[token].protocol_number > 0 && server.clients[token].nick != "" { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
47 |
server.react(token, poll, vec![ |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
48 |
JoinLobby, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
49 |
]); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
50 |
}, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
51 |
JoinLobby => { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
52 |
let joined_msg; |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
53 |
{ |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
54 |
let mut lobby_nicks: Vec<&str> = Vec::new(); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
55 |
for c in server.clients.iter() { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
56 |
if c.room_id.is_some() { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
57 |
lobby_nicks.push(&c.nick); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
58 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
59 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
60 |
joined_msg = LobbyJoined(&lobby_nicks).to_raw_protocol(); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
61 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
62 |
let everyone_msg = LobbyJoined(&[&server.clients[token].nick]).to_raw_protocol(); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
63 |
server.clients[token].room_id = Some(server.lobby_id); |
12145 | 64 |
server.react(token, poll, vec![ |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
65 |
SendAllButMe(everyone_msg), |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
66 |
SendMe(joined_msg), |
12145 | 67 |
]); |
68 |
}, |
|
12144 | 69 |
//_ => unimplemented!(), |
70 |
} |
|
71 |
} |