20 |
21 |
21 pub fn run_action(server: &mut HWServer, token: mio::Token, poll: &mio::Poll, action: Action) { |
22 pub fn run_action(server: &mut HWServer, token: mio::Token, poll: &mio::Poll, action: Action) { |
22 match action { |
23 match action { |
23 SendMe(msg) => |
24 SendMe(msg) => |
24 server.send(token, &msg), |
25 server.send(token, &msg), |
|
26 SendAllButMe(msg) => { |
|
27 for c in server.clients.iter_mut() { |
|
28 if c.id != token { |
|
29 c.send_string(&msg) |
|
30 } |
|
31 } |
|
32 }, |
25 ByeClient(msg) => { |
33 ByeClient(msg) => { |
26 server.react(token, poll, vec![ |
34 server.react(token, poll, vec![ |
27 SendMe(Bye(&msg).to_raw_protocol()), |
35 SendMe(Bye(&msg).to_raw_protocol()), |
28 RemoveClient, |
36 RemoveClient, |
29 ]); |
37 ]); |
33 server.clients.remove(token); |
41 server.clients.remove(token); |
34 }, |
42 }, |
35 ReactProtocolMessage(msg) => |
43 ReactProtocolMessage(msg) => |
36 handlers::handle(server, token, poll, msg), |
44 handlers::handle(server, token, poll, msg), |
37 CheckRegistered => |
45 CheckRegistered => |
38 if server.clients[token].protocolNumber > 0 && server.clients[token].nick != "" { |
46 if server.clients[token].protocol_number > 0 && server.clients[token].nick != "" { |
|
47 server.react(token, poll, vec![ |
|
48 JoinLobby, |
|
49 ]); |
|
50 }, |
|
51 JoinLobby => { |
|
52 let joined_msg; |
|
53 { |
|
54 let mut lobby_nicks: Vec<&str> = Vec::new(); |
|
55 for c in server.clients.iter() { |
|
56 if c.room_id.is_some() { |
|
57 lobby_nicks.push(&c.nick); |
|
58 } |
|
59 } |
|
60 joined_msg = LobbyJoined(&lobby_nicks).to_raw_protocol(); |
|
61 } |
|
62 let everyone_msg = LobbyJoined(&[&server.clients[token].nick]).to_raw_protocol(); |
|
63 server.clients[token].room_id = Some(server.lobby_id); |
39 server.react(token, poll, vec![ |
64 server.react(token, poll, vec![ |
40 JoinLobby, |
65 SendAllButMe(everyone_msg), |
41 ]); |
66 SendMe(joined_msg), |
42 }, |
|
43 JoinLobby => { |
|
44 let msg_string = LobbyJoined(&[&server.clients[token].nick]).to_raw_protocol(); |
|
45 server.react(token, poll, vec![ |
|
46 SendMe(msg_string), |
|
47 ]); |
67 ]); |
48 }, |
68 }, |
49 //_ => unimplemented!(), |
69 //_ => unimplemented!(), |
50 } |
70 } |
51 } |
71 } |