author | alfadur |
Sat, 27 Oct 2018 04:21:35 +0300 | |
changeset 14002 | e9ab9639a1af |
parent 13805 | 0463a4221327 |
child 14374 | e5db279308d7 |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
13666 | 3 |
use crate::{ |
4 |
server::{ |
|
5 |
server::HWServer, |
|
6 |
coretypes::ClientId, |
|
7 |
actions::{Action, Action::*} |
|
8 |
}, |
|
9 |
protocol::messages::{ |
|
10 |
HWProtocolMessage, |
|
11 |
HWServerMessage::* |
|
12 |
}, |
|
13 |
utils::is_name_illegal |
|
13416 | 14 |
}; |
13521 | 15 |
use super::common::rnd_reply; |
13805 | 16 |
use log::*; |
12147 | 17 |
|
13419 | 18 |
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
13666 | 19 |
use crate::protocol::messages::HWProtocolMessage::*; |
12147 | 20 |
match message { |
13416 | 21 |
CreateRoom(name, password) => { |
22 |
let actions = |
|
23 |
if is_name_illegal(&name) { |
|
24 |
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())] |
|
25 |
} else if server.has_room(&name) { |
|
26 |
vec![Warn("A room with the same name already exists.".to_string())] |
|
27 |
} else { |
|
28 |
let flags_msg = ClientFlags( |
|
29 |
"+hr".to_string(), |
|
13419 | 30 |
vec![server.clients[client_id].nick.clone()]); |
13416 | 31 |
vec![AddRoom(name, password), |
13419 | 32 |
flags_msg.send_self().action()] |
13416 | 33 |
}; |
13419 | 34 |
server.react(client_id, actions); |
13416 | 35 |
}, |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
36 |
Chat(msg) => { |
13521 | 37 |
let actions = vec![ChatMsg {nick: server.clients[client_id].nick.clone(), msg} |
38 |
.send_all().in_room(server.lobby_id).but_self().action()]; |
|
39 |
server.react(client_id, actions); |
|
12147 | 40 |
}, |
13666 | 41 |
JoinRoom(name, _password) => { |
42 |
let room = server.rooms.iter().find(|(_, r)| r.name == name); |
|
43 |
let room_id = room.map(|(_, r)| r.id); |
|
44 |
let nicks = server.clients.iter() |
|
45 |
.filter(|(_, c)| c.room_id == room_id) |
|
46 |
.map(|(_, c)| c.nick.clone()) |
|
47 |
.collect(); |
|
48 |
let c = &mut server.clients[client_id]; |
|
13427 | 49 |
|
13666 | 50 |
let actions = if let Some((_, r)) = room { |
51 |
if c.protocol_number != r.protocol_number { |
|
52 |
vec![Warn("Room version incompatible to your Hedgewars version!".to_string())] |
|
53 |
} else if r.is_join_restricted() { |
|
54 |
vec![Warn("Access denied. This room currently doesn't allow joining.".to_string())] |
|
55 |
} else if r.players_number == u8::max_value() { |
|
56 |
vec![Warn("This room is already full".to_string())] |
|
13427 | 57 |
} else { |
13666 | 58 |
vec![MoveToRoom(r.id), |
59 |
RoomJoined(nicks).send_self().action()] |
|
60 |
} |
|
61 |
} else { |
|
62 |
vec![Warn("No such room.".to_string())] |
|
63 |
}; |
|
13419 | 64 |
server.react(client_id, actions); |
12147 | 65 |
}, |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
66 |
Rnd(v) => { |
13521 | 67 |
server.react(client_id, vec![rnd_reply(&v).send_self().action()]); |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13444
diff
changeset
|
68 |
}, |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
69 |
List => warn!("Deprecated LIST message received"), |
12147 | 70 |
_ => warn!("Incorrect command in lobby state"), |
71 |
} |
|
72 |
} |