author | alfadur |
Tue, 01 Oct 2019 23:48:32 +0300 | |
changeset 15439 | a158ff8f84ef |
parent 15075 | e935b1ad23f3 |
child 15482 | 4cc9ec732392 |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
15439 | 3 |
use super::{common::rnd_reply, strings::*}; |
13666 | 4 |
use crate::{ |
15075 | 5 |
core::{ |
6 |
client::HwClient, |
|
15439 | 7 |
server::{AccessError, CreateRoomError, HwServer, JoinRoomError}, |
15075 | 8 |
types::{ClientId, ServerVar}, |
14782 | 9 |
}, |
15075 | 10 |
protocol::messages::{ |
11 |
add_flags, remove_flags, server_chat, HwProtocolMessage, HwServerMessage::*, |
|
12 |
ProtocolFlags as Flags, |
|
14783 | 13 |
}, |
14457 | 14 |
utils::is_name_illegal, |
13416 | 15 |
}; |
13805 | 16 |
use log::*; |
14789 | 17 |
use std::{collections::HashSet, convert::identity}; |
12147 | 18 |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
19 |
pub fn handle( |
15075 | 20 |
server: &mut HwServer, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
21 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
22 |
response: &mut super::Response, |
15075 | 23 |
message: HwProtocolMessage, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
24 |
) { |
15075 | 25 |
use crate::protocol::messages::HwProtocolMessage::*; |
15439 | 26 |
|
12147 | 27 |
match message { |
15439 | 28 |
CreateRoom(name, password) => match server.create_room(client_id, name, password) { |
29 |
Err(CreateRoomError::InvalidName) => response.warn(ILLEGAL_ROOM_NAME), |
|
30 |
Err(CreateRoomError::AlreadyExists) => response.warn(ROOM_EXISTS), |
|
31 |
Ok((client, room)) => { |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
32 |
response.add( |
14504
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
33 |
RoomAdd(room.info(Some(&client))) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
34 |
.send_all() |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
35 |
.with_protocol(room.protocol_number), |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
36 |
); |
14797 | 37 |
response.add(RoomJoined(vec![client.nick.clone()]).send_self()); |
15439 | 38 |
response.add( |
39 |
ClientFlags( |
|
40 |
add_flags(&[Flags::RoomMaster, Flags::Ready]), |
|
41 |
vec![client.nick.clone()], |
|
42 |
) |
|
43 |
.send_self(), |
|
44 |
); |
|
14782 | 45 |
response.add( |
46 |
ClientFlags(add_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_self(), |
|
47 |
); |
|
15439 | 48 |
} |
49 |
}, |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
50 |
Chat(msg) => { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
51 |
response.add( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
52 |
ChatMsg { |
15439 | 53 |
nick: server.get_client_nick(client_id).to_string(), |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
54 |
msg, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
55 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
56 |
.send_all() |
14694 | 57 |
.in_lobby() |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
58 |
.but_self(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
59 |
); |
14457 | 60 |
} |
15439 | 61 |
JoinRoom(name, _password) => match server.join_room_by_name(client_id, &name) { |
62 |
Err(error) => super::common::get_room_join_error(error, response), |
|
63 |
Ok((client, room, room_clients)) => { |
|
64 |
super::common::get_room_join_data(client, room, room_clients, response) |
|
65 |
} |
|
66 |
}, |
|
67 |
Follow(nick) => { |
|
68 |
if let Some(client) = server.find_client(&nick) { |
|
69 |
if let Some(room_id) = client.room_id { |
|
70 |
match server.join_room(client_id, room_id) { |
|
71 |
Err(error) => super::common::get_room_join_error(error, response), |
|
72 |
Ok((client, room, room_clients)) => { |
|
73 |
super::common::get_room_join_data(client, room, room_clients, response) |
|
74 |
} |
|
75 |
} |
|
76 |
} else { |
|
77 |
response.warn(NO_ROOM); |
|
13666 | 78 |
} |
79 |
} else { |
|
15439 | 80 |
response.warn(NO_USER); |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
81 |
} |
14457 | 82 |
} |
15439 | 83 |
SetServerVar(var) => match server.set_var(client_id, var) { |
84 |
Err(AccessError()) => response.warn(ACCESS_DENIED), |
|
85 |
Ok(()) => response.add(server_chat(VARIABLE_UPDATED.to_string()).send_self()), |
|
86 |
}, |
|
87 |
GetServerVar => match server.get_vars(client_id) { |
|
88 |
Err(AccessError()) => response.warn(ACCESS_DENIED), |
|
89 |
Ok(vars) => { |
|
90 |
response.add( |
|
91 |
ServerVars(vars.iter().flat_map(|v| v.to_protocol()).collect()).send_self(), |
|
92 |
); |
|
14787 | 93 |
} |
15439 | 94 |
}, |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
95 |
Rnd(v) => { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
96 |
response.add(rnd_reply(&v).send_self()); |
14457 | 97 |
} |
15439 | 98 |
Stats => match server.get_used_protocols(client_id) { |
99 |
Err(AccessError()) => response.warn(ACCESS_DENIED), |
|
100 |
Ok(protocols) => { |
|
101 |
let mut html = Vec::with_capacity(protocols.len() + 2); |
|
14789 | 102 |
|
15439 | 103 |
html.push("<table>".to_string()); |
104 |
for protocol in protocols { |
|
105 |
html.push(format!( |
|
106 |
"<tr><td>{}</td><td>{}</td><td>{}</td></tr>", |
|
107 |
super::utils::protocol_version_string(protocol), |
|
108 |
server.protocol_clients(protocol).count(), |
|
109 |
server.protocol_rooms(protocol).count() |
|
110 |
)); |
|
111 |
} |
|
112 |
html.push("</table>".to_string()); |
|
113 |
||
114 |
response.add(Warning(html.join("")).send_self()); |
|
14789 | 115 |
} |
15439 | 116 |
}, |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
117 |
List => warn!("Deprecated LIST message received"), |
12147 | 118 |
_ => warn!("Incorrect command in lobby state"), |
119 |
} |
|
120 |
} |