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