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