author | alfadur |
Thu, 11 Apr 2019 01:42:14 +0300 | |
changeset 14787 | 0e64acbc3f8b |
parent 14785 | a1077e8d26f4 |
child 14789 | 18240b308505 |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
14457 | 3 |
use super::common::rnd_reply; |
13666 | 4 |
use crate::{ |
14782 | 5 |
protocol::messages::{ |
6 |
add_flags, remove_flags, HWProtocolMessage, HWServerMessage::*, ProtocolFlags as Flags, |
|
7 |
}, |
|
14783 | 8 |
server::{ |
14787 | 9 |
client::HWClient, |
14783 | 10 |
core::HWServer, |
11 |
coretypes::{ClientId, ServerVar}, |
|
12 |
}, |
|
14457 | 13 |
utils::is_name_illegal, |
13416 | 14 |
}; |
13805 | 15 |
use log::*; |
12147 | 16 |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
17 |
pub fn handle( |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
18 |
server: &mut HWServer, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
19 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
20 |
response: &mut super::Response, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
21 |
message: HWProtocolMessage, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
22 |
) { |
13666 | 23 |
use crate::protocol::messages::HWProtocolMessage::*; |
12147 | 24 |
match message { |
13416 | 25 |
CreateRoom(name, password) => { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
26 |
if is_name_illegal(&name) { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
27 |
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 | 28 |
} else if server.has_room(&name) { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
29 |
response.add( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
30 |
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
|
31 |
); |
14457 | 32 |
} else { |
33 |
let flags_msg = ClientFlags( |
|
14782 | 34 |
add_flags(&[Flags::RoomMaster, Flags::Ready]), |
14457 | 35 |
vec![server.clients[client_id].nick.clone()], |
36 |
); |
|
14504
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
37 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
38 |
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
|
39 |
let room = &server.rooms[room_id]; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
40 |
let client = &server.clients[client_id]; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
41 |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
42 |
response.add( |
14504
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
43 |
RoomAdd(room.info(Some(&client))) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
44 |
.send_all() |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
45 |
.with_protocol(room.protocol_number), |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
46 |
); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
47 |
response.add(flags_msg.send_self()); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
48 |
|
14782 | 49 |
response.add( |
50 |
ClientFlags(add_flags(&[Flags::InRoom]), vec![client.nick.clone()]).send_self(), |
|
51 |
); |
|
14457 | 52 |
}; |
53 |
} |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
54 |
Chat(msg) => { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
55 |
response.add( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
56 |
ChatMsg { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
57 |
nick: server.clients[client_id].nick.clone(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
58 |
msg, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
59 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
60 |
.send_all() |
14694 | 61 |
.in_lobby() |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
62 |
.but_self(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
63 |
); |
14457 | 64 |
} |
13666 | 65 |
JoinRoom(name, _password) => { |
66 |
let room = server.rooms.iter().find(|(_, r)| r.name == name); |
|
67 |
let room_id = room.map(|(_, r)| r.id); |
|
14785
a1077e8d26f4
implement watch message apart from replay deserializing
alfadur
parents:
14783
diff
changeset
|
68 |
|
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
69 |
let client = &mut server.clients[client_id]; |
13427 | 70 |
|
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
71 |
if let Some((_, room)) = room { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
72 |
if client.protocol_number != room.protocol_number { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
73 |
response.add( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
74 |
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
|
75 |
.send_self(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
76 |
); |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
77 |
} else if room.is_join_restricted() { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
78 |
response.add( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
79 |
Warning( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
80 |
"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
|
81 |
) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
82 |
.send_self(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
83 |
); |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
84 |
} else if room.players_number == u8::max_value() { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
85 |
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
|
86 |
} else if let Some(room_id) = room_id { |
14787 | 87 |
super::common::enter_room(server, client_id, room_id, response); |
13666 | 88 |
} |
89 |
} else { |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
90 |
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
|
91 |
} |
14457 | 92 |
} |
14787 | 93 |
Follow(nick) => { |
94 |
if let Some(HWClient { |
|
95 |
room_id: Some(room_id), |
|
96 |
.. |
|
97 |
}) = server.find_client(&nick) |
|
98 |
{ |
|
99 |
let room = &server.rooms[*room_id]; |
|
100 |
response.add(Joining(room.name.clone()).send_self()); |
|
101 |
super::common::enter_room(server, client_id, *room_id, response); |
|
102 |
} |
|
103 |
} |
|
14783 | 104 |
SetServerVar(var) => { |
105 |
if !server.clients[client_id].is_admin() { |
|
106 |
response.add(Warning("Access denied.".to_string()).send_self()); |
|
107 |
} else { |
|
108 |
match var { |
|
109 |
ServerVar::MOTDNew(msg) => server.greetings.for_latest_protocol = msg, |
|
110 |
ServerVar::MOTDOld(msg) => server.greetings.for_old_protocols = msg, |
|
111 |
ServerVar::LatestProto(n) => server.latest_protocol = n, |
|
112 |
} |
|
113 |
} |
|
114 |
} |
|
115 |
GetServerVar => { |
|
116 |
if !server.clients[client_id].is_admin() { |
|
117 |
response.add(Warning("Access denied.".to_string()).send_self()); |
|
118 |
} else { |
|
119 |
let vars: Vec<_> = [ |
|
120 |
ServerVar::MOTDNew(server.greetings.for_latest_protocol.clone()), |
|
121 |
ServerVar::MOTDOld(server.greetings.for_old_protocols.clone()), |
|
122 |
ServerVar::LatestProto(server.latest_protocol), |
|
123 |
] |
|
124 |
.iter() |
|
125 |
.flat_map(|v| v.to_protocol()) |
|
126 |
.collect(); |
|
127 |
response.add(ServerVars(vars).send_self()); |
|
128 |
} |
|
129 |
} |
|
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
130 |
Rnd(v) => { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
131 |
response.add(rnd_reply(&v).send_self()); |
14457 | 132 |
} |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
133 |
List => warn!("Deprecated LIST message received"), |
12147 | 134 |
_ => warn!("Incorrect command in lobby state"), |
135 |
} |
|
136 |
} |