author | alfadur <mail@none> |
Fri, 28 Dec 2018 03:10:05 +0300 | |
changeset 14504 | 6cc0fce249f9 |
parent 14457 | 98ef2913ec73 |
child 14671 | 455865ccd36c |
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 |
|
13419 | 15 |
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
13666 | 16 |
use crate::protocol::messages::HWProtocolMessage::*; |
12147 | 17 |
match message { |
13416 | 18 |
CreateRoom(name, password) => { |
14457 | 19 |
let actions = if is_name_illegal(&name) { |
20 |
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())] |
|
21 |
} else if server.has_room(&name) { |
|
22 |
vec![Warn( |
|
23 |
"A room with the same name already exists.".to_string(), |
|
24 |
)] |
|
25 |
} else { |
|
26 |
let flags_msg = ClientFlags( |
|
27 |
"+hr".to_string(), |
|
28 |
vec![server.clients[client_id].nick.clone()], |
|
29 |
); |
|
14504
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
30 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
31 |
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
|
32 |
let room = &server.rooms[room_id]; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
33 |
let client = &server.clients[client_id]; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
34 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
35 |
vec![ |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
36 |
RoomAdd(room.info(Some(&client))) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
37 |
.send_all() |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
38 |
.with_protocol(room.protocol_number) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
39 |
.action(), |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
40 |
flags_msg.send_self().action(), |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
41 |
] |
14457 | 42 |
}; |
14504
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
43 |
server.react(client_id, actions) |
14457 | 44 |
} |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
45 |
Chat(msg) => { |
14457 | 46 |
let actions = vec![ChatMsg { |
47 |
nick: server.clients[client_id].nick.clone(), |
|
48 |
msg, |
|
49 |
} |
|
50 |
.send_all() |
|
51 |
.in_room(server.lobby_id) |
|
52 |
.but_self() |
|
53 |
.action()]; |
|
13521 | 54 |
server.react(client_id, actions); |
14457 | 55 |
} |
13666 | 56 |
JoinRoom(name, _password) => { |
57 |
let room = server.rooms.iter().find(|(_, r)| r.name == name); |
|
58 |
let room_id = room.map(|(_, r)| r.id); |
|
14457 | 59 |
let nicks = server |
60 |
.clients |
|
61 |
.iter() |
|
13666 | 62 |
.filter(|(_, c)| c.room_id == room_id) |
63 |
.map(|(_, c)| c.nick.clone()) |
|
64 |
.collect(); |
|
65 |
let c = &mut server.clients[client_id]; |
|
13427 | 66 |
|
13666 | 67 |
let actions = if let Some((_, r)) = room { |
68 |
if c.protocol_number != r.protocol_number { |
|
14457 | 69 |
vec![Warn( |
70 |
"Room version incompatible to your Hedgewars version!".to_string(), |
|
71 |
)] |
|
13666 | 72 |
} else if r.is_join_restricted() { |
14457 | 73 |
vec![Warn( |
74 |
"Access denied. This room currently doesn't allow joining.".to_string(), |
|
75 |
)] |
|
13666 | 76 |
} else if r.players_number == u8::max_value() { |
77 |
vec![Warn("This room is already full".to_string())] |
|
13427 | 78 |
} else { |
14457 | 79 |
vec![MoveToRoom(r.id), RoomJoined(nicks).send_self().action()] |
13666 | 80 |
} |
81 |
} else { |
|
82 |
vec![Warn("No such room.".to_string())] |
|
83 |
}; |
|
13419 | 84 |
server.react(client_id, actions); |
14457 | 85 |
} |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
86 |
Rnd(v) => { |
13521 | 87 |
server.react(client_id, vec![rnd_reply(&v).send_self().action()]); |
14457 | 88 |
} |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
89 |
List => warn!("Deprecated LIST message received"), |
12147 | 90 |
_ => warn!("Incorrect command in lobby state"), |
91 |
} |
|
92 |
} |