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