author | alfadur |
Thu, 11 Apr 2019 21:20:41 +0300 | |
changeset 14789 | 18240b308505 |
parent 14787 | 0e64acbc3f8b |
child 14797 | ce2268ae261f |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
14457 | 3 |
use super::common::rnd_reply; |
13666 | 4 |
use crate::{ |
14782 | 5 |
protocol::messages::{ |
14789 | 6 |
add_flags, remove_flags, server_chat, HWProtocolMessage, HWServerMessage::*, |
7 |
ProtocolFlags as Flags, |
|
14782 | 8 |
}, |
14783 | 9 |
server::{ |
14787 | 10 |
client::HWClient, |
14783 | 11 |
core::HWServer, |
12 |
coretypes::{ClientId, ServerVar}, |
|
13 |
}, |
|
14457 | 14 |
utils::is_name_illegal, |
13416 | 15 |
}; |
13805 | 16 |
use log::*; |
14789 | 17 |
use std::{collections::HashSet, convert::identity}; |
12147 | 18 |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
19 |
pub fn handle( |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
20 |
server: &mut HWServer, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
21 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
22 |
response: &mut super::Response, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
23 |
message: HWProtocolMessage, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
24 |
) { |
13666 | 25 |
use crate::protocol::messages::HWProtocolMessage::*; |
12147 | 26 |
match message { |
13416 | 27 |
CreateRoom(name, password) => { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
28 |
if is_name_illegal(&name) { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
29 |
response.add(Warning("Illegal room name! A room name must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string()).send_self()); |
14457 | 30 |
} else if server.has_room(&name) { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
31 |
response.add( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
32 |
Warning("A room with the same name already exists.".to_string()).send_self(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
33 |
); |
14457 | 34 |
} else { |
35 |
let flags_msg = ClientFlags( |
|
14782 | 36 |
add_flags(&[Flags::RoomMaster, Flags::Ready]), |
14457 | 37 |
vec![server.clients[client_id].nick.clone()], |
38 |
); |
|
14504
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
39 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
40 |
let room_id = server.create_room(client_id, name, password); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
41 |
let room = &server.rooms[room_id]; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
42 |
let client = &server.clients[client_id]; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
43 |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
44 |
response.add( |
14504
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
45 |
RoomAdd(room.info(Some(&client))) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
46 |
.send_all() |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
47 |
.with_protocol(room.protocol_number), |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
48 |
); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
49 |
response.add(flags_msg.send_self()); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
50 |
|
14782 | 51 |
response.add( |
52 |
ClientFlags(add_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_self(), |
|
53 |
); |
|
14457 | 54 |
}; |
55 |
} |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
56 |
Chat(msg) => { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
57 |
response.add( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
58 |
ChatMsg { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
59 |
nick: server.clients[client_id].nick.clone(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
60 |
msg, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
61 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
62 |
.send_all() |
14694 | 63 |
.in_lobby() |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
64 |
.but_self(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
65 |
); |
14457 | 66 |
} |
13666 | 67 |
JoinRoom(name, _password) => { |
68 |
let room = server.rooms.iter().find(|(_, r)| r.name == name); |
|
69 |
let room_id = room.map(|(_, r)| r.id); |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
70 |
|
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
71 |
let client = &mut server.clients[client_id]; |
13427 | 72 |
|
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
73 |
if let Some((_, room)) = room { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
74 |
if client.protocol_number != room.protocol_number { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
75 |
response.add( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
76 |
Warning("Room version incompatible to your Hedgewars version!".to_string()) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
77 |
.send_self(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
78 |
); |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
79 |
} else if room.is_join_restricted() { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
80 |
response.add( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
81 |
Warning( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
82 |
"Access denied. This room currently doesn't allow joining.".to_string(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
83 |
) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
84 |
.send_self(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
85 |
); |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
86 |
} else if room.players_number == u8::max_value() { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
87 |
response.add(Warning("This room is already full".to_string()).send_self()); |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
88 |
} else if let Some(room_id) = room_id { |
14787 | 89 |
super::common::enter_room(server, client_id, room_id, response); |
13666 | 90 |
} |
91 |
} else { |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
92 |
response.add(Warning("No such room.".to_string()).send_self()); |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
93 |
} |
14457 | 94 |
} |
14787 | 95 |
Follow(nick) => { |
96 |
if let Some(HWClient { |
|
97 |
room_id: Some(room_id), |
|
98 |
.. |
|
99 |
}) = server.find_client(&nick) |
|
100 |
{ |
|
101 |
let room = &server.rooms[*room_id]; |
|
102 |
response.add(Joining(room.name.clone()).send_self()); |
|
103 |
super::common::enter_room(server, client_id, *room_id, response); |
|
104 |
} |
|
105 |
} |
|
14783 | 106 |
SetServerVar(var) => { |
107 |
if !server.clients[client_id].is_admin() { |
|
108 |
response.add(Warning("Access denied.".to_string()).send_self()); |
|
109 |
} else { |
|
110 |
match var { |
|
111 |
ServerVar::MOTDNew(msg) => server.greetings.for_latest_protocol = msg, |
|
112 |
ServerVar::MOTDOld(msg) => server.greetings.for_old_protocols = msg, |
|
113 |
ServerVar::LatestProto(n) => server.latest_protocol = n, |
|
114 |
} |
|
115 |
} |
|
116 |
} |
|
117 |
GetServerVar => { |
|
118 |
if !server.clients[client_id].is_admin() { |
|
119 |
response.add(Warning("Access denied.".to_string()).send_self()); |
|
120 |
} else { |
|
121 |
let vars: Vec<_> = [ |
|
122 |
ServerVar::MOTDNew(server.greetings.for_latest_protocol.clone()), |
|
123 |
ServerVar::MOTDOld(server.greetings.for_old_protocols.clone()), |
|
124 |
ServerVar::LatestProto(server.latest_protocol), |
|
125 |
] |
|
126 |
.iter() |
|
127 |
.flat_map(|v| v.to_protocol()) |
|
128 |
.collect(); |
|
129 |
response.add(ServerVars(vars).send_self()); |
|
130 |
} |
|
131 |
} |
|
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
132 |
Rnd(v) => { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
133 |
response.add(rnd_reply(&v).send_self()); |
14457 | 134 |
} |
14789 | 135 |
Stats => { |
136 |
let mut protocols: HashSet<_> = server |
|
137 |
.clients |
|
138 |
.iter() |
|
139 |
.map(|(_, c)| c.protocol_number) |
|
140 |
.chain(server.rooms.iter().map(|(_, r)| r.protocol_number)) |
|
141 |
.collect(); |
|
142 |
let mut protocols: Vec<_> = protocols.drain().collect(); |
|
143 |
protocols.sort(); |
|
144 |
||
145 |
let mut html = Vec::with_capacity(protocols.len() + 2); |
|
146 |
||
147 |
html.push("<table>".to_string()); |
|
148 |
for protocol in protocols { |
|
149 |
html.push(format!( |
|
150 |
"<tr><td>{}</td><td>{}</td><td>{}</td></tr>", |
|
151 |
super::utils::protocol_version_string(protocol), |
|
152 |
server.protocol_clients(protocol).count(), |
|
153 |
server.protocol_rooms(protocol).count() |
|
154 |
)); |
|
155 |
} |
|
156 |
html.push("</table>".to_string()); |
|
157 |
||
158 |
response.add(Warning(html.join("")).send_self()); |
|
159 |
} |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
160 |
List => warn!("Deprecated LIST message received"), |
12147 | 161 |
_ => warn!("Incorrect command in lobby state"), |
162 |
} |
|
163 |
} |