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