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