author | Wuzzy <Wuzzy@disroot.org> |
Wed, 28 Jun 2023 18:57:00 +0200 | |
changeset 15967 | f362e82cddcf |
parent 15938 | ce47259d5c86 |
child 15989 | fb389df02e3e |
permissions | -rw-r--r-- |
15439 | 1 |
use super::{common::rnd_reply, strings::*}; |
15848 | 2 |
use crate::handlers::{actions::ToPendingMessage, checker}; |
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 |
|
15938 | 29 |
todo!("add kick/ban handlers"); |
30 |
||
12147 | 31 |
match message { |
15439 | 32 |
CreateRoom(name, password) => match server.create_room(client_id, name, password) { |
33 |
Err(CreateRoomError::InvalidName) => response.warn(ILLEGAL_ROOM_NAME), |
|
34 |
Err(CreateRoomError::AlreadyExists) => response.warn(ROOM_EXISTS), |
|
35 |
Ok((client, room)) => { |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
36 |
response.add( |
14504
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
37 |
RoomAdd(room.info(Some(&client))) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
38 |
.send_all() |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
39 |
.with_protocol(room.protocol_number), |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
40 |
); |
14797 | 41 |
response.add(RoomJoined(vec![client.nick.clone()]).send_self()); |
15439 | 42 |
response.add( |
43 |
ClientFlags( |
|
15533 | 44 |
add_flags(&[Flags::RoomMaster, Flags::Ready, Flags::InRoom]), |
15439 | 45 |
vec![client.nick.clone()], |
46 |
) |
|
15533 | 47 |
.send_all(), |
14782 | 48 |
); |
15439 | 49 |
} |
50 |
}, |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
51 |
Chat(msg) => { |
15938 | 52 |
todo!("add client quiet flag"); |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
53 |
response.add( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
54 |
ChatMsg { |
15533 | 55 |
nick: server.client(client_id).nick.clone(), |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
56 |
msg, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
57 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
58 |
.send_all() |
14694 | 59 |
.in_lobby() |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
60 |
.but_self(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
61 |
); |
14457 | 62 |
} |
15533 | 63 |
JoinRoom(name, password) => { |
64 |
match server.join_room_by_name(client_id, &name, password.as_deref()) { |
|
65 |
Err(error) => super::common::get_room_join_error(error, response), |
|
66 |
Ok((client, room, room_clients)) => { |
|
67 |
super::common::get_room_join_data(client, room, room_clients, response) |
|
68 |
} |
|
15439 | 69 |
} |
15533 | 70 |
} |
15439 | 71 |
Follow(nick) => { |
72 |
if let Some(client) = server.find_client(&nick) { |
|
73 |
if let Some(room_id) = client.room_id { |
|
15533 | 74 |
match server.join_room(client_id, room_id, None) { |
15439 | 75 |
Err(error) => super::common::get_room_join_error(error, response), |
76 |
Ok((client, room, room_clients)) => { |
|
77 |
super::common::get_room_join_data(client, room, room_clients, response) |
|
78 |
} |
|
79 |
} |
|
80 |
} else { |
|
81 |
response.warn(NO_ROOM); |
|
13666 | 82 |
} |
83 |
} else { |
|
15439 | 84 |
response.warn(NO_USER); |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
85 |
} |
14457 | 86 |
} |
15439 | 87 |
SetServerVar(var) => match server.set_var(client_id, var) { |
88 |
Err(AccessError()) => response.warn(ACCESS_DENIED), |
|
89 |
Ok(()) => response.add(server_chat(VARIABLE_UPDATED.to_string()).send_self()), |
|
90 |
}, |
|
91 |
GetServerVar => match server.get_vars(client_id) { |
|
92 |
Err(AccessError()) => response.warn(ACCESS_DENIED), |
|
93 |
Ok(vars) => { |
|
94 |
response.add( |
|
95 |
ServerVars(vars.iter().flat_map(|v| v.to_protocol()).collect()).send_self(), |
|
96 |
); |
|
14787 | 97 |
} |
15439 | 98 |
}, |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
99 |
Rnd(v) => { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
100 |
response.add(rnd_reply(&v).send_self()); |
14457 | 101 |
} |
15439 | 102 |
Stats => match server.get_used_protocols(client_id) { |
103 |
Err(AccessError()) => response.warn(ACCESS_DENIED), |
|
104 |
Ok(protocols) => { |
|
105 |
let mut html = Vec::with_capacity(protocols.len() + 2); |
|
14789 | 106 |
|
15439 | 107 |
html.push("<table>".to_string()); |
108 |
for protocol in protocols { |
|
109 |
html.push(format!( |
|
110 |
"<tr><td>{}</td><td>{}</td><td>{}</td></tr>", |
|
111 |
super::utils::protocol_version_string(protocol), |
|
15526
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15482
diff
changeset
|
112 |
server.protocol_client_ids(protocol).count(), |
24f692e791d3
disallow mutable rooms outside the server
alfadur <mail@none>
parents:
15482
diff
changeset
|
113 |
server.protocol_room_ids(protocol).count() |
15439 | 114 |
)); |
115 |
} |
|
116 |
html.push("</table>".to_string()); |
|
117 |
||
118 |
response.add(Warning(html.join("")).send_self()); |
|
14789 | 119 |
} |
15439 | 120 |
}, |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
121 |
List => warn!("Deprecated LIST message received"), |
12147 | 122 |
_ => warn!("Incorrect command in lobby state"), |
123 |
} |
|
124 |
} |