author | unc0rr |
Mon, 06 Feb 2017 17:54:27 +0300 | |
changeset 12148 | 8591375271b8 |
parent 12147 | 03ccb89820f3 |
child 12852 | bd35cb2302b3 |
permissions | -rw-r--r-- |
12144 | 1 |
use mio; |
2 |
use std::io::Write; |
|
3 |
use std::io; |
|
4 |
||
5 |
use super::server::HWServer; |
|
12147 | 6 |
use super::server::HWRoom; |
12144 | 7 |
use protocol::messages::HWProtocolMessage; |
8 |
use protocol::messages::HWServerMessage::*; |
|
9 |
use super::handlers; |
|
12138 | 10 |
|
11 |
pub enum Action { |
|
12 |
SendMe(String), |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
13 |
SendAllButMe(String), |
12139 | 14 |
RemoveClient, |
15 |
ByeClient(String), |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
16 |
ReactProtocolMessage(HWProtocolMessage), |
12145 | 17 |
CheckRegistered, |
18 |
JoinLobby, |
|
12147 | 19 |
AddRoom(String, Option<String>), |
20 |
Warn(String), |
|
12138 | 21 |
} |
12144 | 22 |
|
23 |
use self::Action::*; |
|
24 |
||
25 |
pub fn run_action(server: &mut HWServer, token: mio::Token, poll: &mio::Poll, action: Action) { |
|
26 |
match action { |
|
27 |
SendMe(msg) => |
|
28 |
server.send(token, &msg), |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
29 |
SendAllButMe(msg) => { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
30 |
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
|
31 |
if c.id != token { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
32 |
c.send_string(&msg) |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
33 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
34 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
35 |
}, |
12144 | 36 |
ByeClient(msg) => { |
37 |
server.react(token, poll, vec![ |
|
38 |
SendMe(Bye(&msg).to_raw_protocol()), |
|
39 |
RemoveClient, |
|
40 |
]); |
|
41 |
}, |
|
42 |
RemoveClient => { |
|
43 |
server.clients[token].deregister(poll); |
|
44 |
server.clients.remove(token); |
|
45 |
}, |
|
46 |
ReactProtocolMessage(msg) => |
|
47 |
handlers::handle(server, token, poll, msg), |
|
12145 | 48 |
CheckRegistered => |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
49 |
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
|
50 |
server.react(token, poll, vec![ |
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 |
]); |
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 |
JoinLobby => { |
12147 | 55 |
server.clients[token].room_id = Some(server.lobby_id); |
56 |
||
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
57 |
let joined_msg; |
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 |
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
|
60 |
for c in server.clients.iter() { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
61 |
if c.room_id.is_some() { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
62 |
lobby_nicks.push(&c.nick); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
63 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
64 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
65 |
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
|
66 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
67 |
let everyone_msg = LobbyJoined(&[&server.clients[token].nick]).to_raw_protocol(); |
12145 | 68 |
server.react(token, poll, vec![ |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
69 |
SendAllButMe(everyone_msg), |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
70 |
SendMe(joined_msg), |
12145 | 71 |
]); |
72 |
}, |
|
12147 | 73 |
AddRoom(name, password) => { |
74 |
let room_id = server.rooms.insert(HWRoom::new()).ok().expect("Cannot add room"); |
|
12148 | 75 |
{ |
76 |
let r = &mut server.rooms[room_id]; |
|
77 |
let c = &mut server.clients[token]; |
|
78 |
r.name = name; |
|
79 |
r.password = password; |
|
80 |
r.id = room_id.clone(); |
|
81 |
r.ready_players_number = 1; |
|
82 |
r.protocol_number = c.protocol_number; |
|
83 |
c.room_id = Some(room_id); |
|
84 |
} |
|
85 |
||
12147 | 86 |
}, |
87 |
Warn(msg) => { |
|
88 |
run_action(server, token, poll, SendMe(Warning(&msg).to_raw_protocol())); |
|
89 |
} |
|
12144 | 90 |
//_ => unimplemented!(), |
91 |
} |
|
92 |
} |