author | Wuzzy <Wuzzy2@mail.ru> |
Fri, 16 Mar 2018 16:35:08 +0100 | |
changeset 13227 | 26c739440bef |
parent 13119 | 1e39b8749072 |
child 13416 | cdf69667593b |
permissions | -rw-r--r-- |
12144 | 1 |
use mio; |
2 |
use std::io::Write; |
|
3 |
use std::io; |
|
4 |
||
5 |
use super::server::HWServer; |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
6 |
use super::room::HWRoom; |
12144 | 7 |
use protocol::messages::HWProtocolMessage; |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
8 |
use protocol::messages::HWServerMessage; |
12144 | 9 |
use protocol::messages::HWServerMessage::*; |
10 |
use super::handlers; |
|
12138 | 11 |
|
12 |
pub enum Action { |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
13 |
SendMe(HWServerMessage), |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
14 |
SendAllButMe(HWServerMessage), |
12139 | 15 |
RemoveClient, |
16 |
ByeClient(String), |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
17 |
ReactProtocolMessage(HWProtocolMessage), |
12145 | 18 |
CheckRegistered, |
19 |
JoinLobby, |
|
12147 | 20 |
AddRoom(String, Option<String>), |
21 |
Warn(String), |
|
12138 | 22 |
} |
12144 | 23 |
|
24 |
use self::Action::*; |
|
25 |
||
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
26 |
pub fn run_action(server: &mut HWServer, token: usize, action: Action) { |
12144 | 27 |
match action { |
28 |
SendMe(msg) => |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
29 |
server.send_self(token, msg), |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
30 |
SendAllButMe(msg) => { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
31 |
server.send_others(token, msg) |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
32 |
}, |
12144 | 33 |
ByeClient(msg) => { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
34 |
server.react(token, vec![ |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
35 |
SendMe(Bye(msg)), |
12144 | 36 |
RemoveClient, |
37 |
]); |
|
38 |
}, |
|
39 |
RemoveClient => { |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
40 |
server.removed_clients.push(token); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
41 |
if server.clients.contains(token) { |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
42 |
server.clients.remove(token); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
43 |
} |
12144 | 44 |
}, |
45 |
ReactProtocolMessage(msg) => |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
46 |
handlers::handle(server, token, msg), |
12145 | 47 |
CheckRegistered => |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
48 |
if server.clients[token].protocol_number > 0 && server.clients[token].nick != "" { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
49 |
server.react(token, vec![ |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
50 |
JoinLobby, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
51 |
]); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
52 |
}, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
53 |
JoinLobby => { |
12147 | 54 |
server.clients[token].room_id = Some(server.lobby_id); |
55 |
||
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
56 |
let joined_msg; |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
57 |
{ |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
58 |
let mut lobby_nicks = Vec::new(); |
12852 | 59 |
for (_, c) in server.clients.iter() { |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
60 |
if c.room_id.is_some() { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
61 |
lobby_nicks.push(c.nick.clone()); |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
62 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
63 |
} |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
64 |
joined_msg = LobbyJoined(lobby_nicks); |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
65 |
} |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
66 |
let everyone_msg = LobbyJoined(vec![server.clients[token].nick.clone()]); |
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
67 |
server.react(token, vec![ |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
68 |
SendAllButMe(everyone_msg), |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
69 |
SendMe(joined_msg), |
12145 | 70 |
]); |
71 |
}, |
|
12147 | 72 |
AddRoom(name, password) => { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
73 |
let room_id = server.add_room();; |
12148 | 74 |
{ |
75 |
let r = &mut server.rooms[room_id]; |
|
76 |
let c = &mut server.clients[token]; |
|
77 |
r.name = name; |
|
78 |
r.password = password; |
|
79 |
r.ready_players_number = 1; |
|
80 |
r.protocol_number = c.protocol_number; |
|
81 |
c.room_id = Some(room_id); |
|
82 |
} |
|
12147 | 83 |
}, |
84 |
Warn(msg) => { |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
85 |
run_action(server, token,SendMe(Warning(msg))); |
12147 | 86 |
} |
12144 | 87 |
//_ => unimplemented!(), |
88 |
} |
|
89 |
} |