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