author | alfadur <mail@none> |
Wed, 06 Feb 2019 00:14:04 +0300 | |
changeset 14686 | 9f98086de1b6 |
parent 14672 | 6e6632068a33 |
child 14690 | f61ce544d436 |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
14457 | 3 |
use super::common::rnd_reply; |
13666 | 4 |
use crate::{ |
14457 | 5 |
protocol::messages::{HWProtocolMessage, HWServerMessage::*}, |
13666 | 6 |
server::{ |
14457 | 7 |
actions::{Action, Action::*}, |
14374 | 8 |
core::HWServer, |
13666 | 9 |
coretypes::ClientId, |
10 |
}, |
|
14457 | 11 |
utils::is_name_illegal, |
13416 | 12 |
}; |
13805 | 13 |
use log::*; |
12147 | 14 |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
15 |
pub fn handle( |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
16 |
server: &mut HWServer, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
17 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
18 |
response: &mut super::Response, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
19 |
message: HWProtocolMessage, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
20 |
) { |
13666 | 21 |
use crate::protocol::messages::HWProtocolMessage::*; |
12147 | 22 |
match message { |
13416 | 23 |
CreateRoom(name, password) => { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
24 |
if is_name_illegal(&name) { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
25 |
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 | 26 |
} else if server.has_room(&name) { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
27 |
response.add( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
28 |
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
|
29 |
); |
14457 | 30 |
} else { |
31 |
let flags_msg = ClientFlags( |
|
32 |
"+hr".to_string(), |
|
33 |
vec![server.clients[client_id].nick.clone()], |
|
34 |
); |
|
14504
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
35 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
36 |
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
|
37 |
let room = &server.rooms[room_id]; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
38 |
let client = &server.clients[client_id]; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
39 |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
40 |
response.add( |
14504
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
41 |
RoomAdd(room.info(Some(&client))) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
42 |
.send_all() |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
43 |
.with_protocol(room.protocol_number), |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
44 |
); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
45 |
response.add(flags_msg.send_self()); |
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(ClientFlags("+i".to_string(), vec![client.nick.clone()]).send_self()); |
14457 | 48 |
}; |
49 |
} |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
50 |
Chat(msg) => { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
51 |
response.add( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
52 |
ChatMsg { |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
53 |
nick: server.clients[client_id].nick.clone(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
54 |
msg, |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
55 |
} |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
56 |
.send_all() |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
57 |
.in_room(server.lobby_id) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
58 |
.but_self(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
59 |
); |
14457 | 60 |
} |
13666 | 61 |
JoinRoom(name, _password) => { |
62 |
let room = server.rooms.iter().find(|(_, r)| r.name == name); |
|
63 |
let room_id = room.map(|(_, r)| r.id); |
|
14457 | 64 |
let nicks = server |
65 |
.clients |
|
66 |
.iter() |
|
13666 | 67 |
.filter(|(_, c)| c.room_id == room_id) |
68 |
.map(|(_, c)| c.nick.clone()) |
|
69 |
.collect(); |
|
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
70 |
let client = &mut server.clients[client_id]; |
13427 | 71 |
|
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
72 |
if let Some((_, room)) = room { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
73 |
if client.protocol_number != room.protocol_number { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
74 |
response.add( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
75 |
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
|
76 |
.send_self(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
77 |
); |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
78 |
} else if room.is_join_restricted() { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
79 |
response.add( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
80 |
Warning( |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
81 |
"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
|
82 |
) |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
83 |
.send_self(), |
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
84 |
); |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
85 |
} else if room.players_number == u8::max_value() { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
86 |
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
|
87 |
} else if let Some(room_id) = room_id { |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
88 |
let nick = client.nick.clone(); |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
89 |
server.move_to_room(client_id, room_id); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
90 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
91 |
response.add(RoomJoined(vec![nick.clone()]).send_all().in_room(room_id)); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
92 |
response.add(ClientFlags("+i".to_string(), vec![nick]).send_all()); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
93 |
response.add(RoomJoined(nicks).send_self()); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
94 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
95 |
let room = &server.rooms[room_id]; |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
96 |
|
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
97 |
if !room.greeting.is_empty() { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
98 |
response.add( |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
99 |
ChatMsg { |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
100 |
nick: "[greeting]".to_string(), |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
101 |
msg: room.greeting.clone(), |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
102 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
103 |
.send_self(), |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
104 |
); |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14504
diff
changeset
|
105 |
} |
13666 | 106 |
} |
107 |
} else { |
|
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
108 |
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
|
109 |
} |
14457 | 110 |
} |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
111 |
Rnd(v) => { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
112 |
response.add(rnd_reply(&v).send_self()); |
14457 | 113 |
} |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
114 |
List => warn!("Deprecated LIST message received"), |
12147 | 115 |
_ => warn!("Incorrect command in lobby state"), |
116 |
} |
|
117 |
} |