author | Wuzzy <almikes@aol.com> |
Wed, 27 Sep 2017 04:28:34 +0200 | |
changeset 12554 | d8af6f9b51c6 |
parent 12153 | 8591375271b8 |
child 12857 | bd35cb2302b3 |
permissions | -rw-r--r-- |
12149 | 1 |
use mio; |
2 |
use std::io::Write; |
|
3 |
use std::io; |
|
4 |
||
5 |
use super::server::HWServer; |
|
12152 | 6 |
use super::server::HWRoom; |
12149 | 7 |
use protocol::messages::HWProtocolMessage; |
8 |
use protocol::messages::HWServerMessage::*; |
|
9 |
use super::handlers; |
|
12143 | 10 |
|
11 |
pub enum Action { |
|
12 |
SendMe(String), |
|
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
13 |
SendAllButMe(String), |
12144 | 14 |
RemoveClient, |
15 |
ByeClient(String), |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
16 |
ReactProtocolMessage(HWProtocolMessage), |
12150 | 17 |
CheckRegistered, |
18 |
JoinLobby, |
|
12152 | 19 |
AddRoom(String, Option<String>), |
20 |
Warn(String), |
|
12143 | 21 |
} |
12149 | 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), |
|
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
29 |
SendAllButMe(msg) => { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
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:
12150
diff
changeset
|
31 |
if c.id != token { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
32 |
c.send_string(&msg) |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
33 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
34 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
35 |
}, |
12149 | 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), |
|
12150 | 48 |
CheckRegistered => |
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
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:
12150
diff
changeset
|
50 |
server.react(token, poll, vec![ |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
51 |
JoinLobby, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
52 |
]); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
53 |
}, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
54 |
JoinLobby => { |
12152 | 55 |
server.clients[token].room_id = Some(server.lobby_id); |
56 |
||
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
57 |
let joined_msg; |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
58 |
{ |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
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:
12150
diff
changeset
|
60 |
for c in server.clients.iter() { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
61 |
if c.room_id.is_some() { |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
62 |
lobby_nicks.push(&c.nick); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
63 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
64 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
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:
12150
diff
changeset
|
66 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
67 |
let everyone_msg = LobbyJoined(&[&server.clients[token].nick]).to_raw_protocol(); |
12150 | 68 |
server.react(token, poll, vec![ |
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
69 |
SendAllButMe(everyone_msg), |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
70 |
SendMe(joined_msg), |
12150 | 71 |
]); |
72 |
}, |
|
12152 | 73 |
AddRoom(name, password) => { |
74 |
let room_id = server.rooms.insert(HWRoom::new()).ok().expect("Cannot add room"); |
|
12153 | 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 |
||
12152 | 86 |
}, |
87 |
Warn(msg) => { |
|
88 |
run_action(server, token, poll, SendMe(Warning(&msg).to_raw_protocol())); |
|
89 |
} |
|
12149 | 90 |
//_ => unimplemented!(), |
91 |
} |
|
92 |
} |