author | alfadur |
Thu, 21 Jun 2018 17:23:10 -0400 | |
changeset 13424 | 81e0ed105f5d |
parent 13421 | cdf69667593b |
child 13432 | 6f6a866c86a2 |
permissions | -rw-r--r-- |
12152 | 1 |
use mio; |
2 |
||
13421 | 3 |
use server::{ |
4 |
server::HWServer, |
|
13424 | 5 |
client::ClientId, |
13421 | 6 |
actions::{Action, Action::*} |
7 |
}; |
|
8 |
use protocol::messages::{ |
|
9 |
HWProtocolMessage, |
|
10 |
HWServerMessage::* |
|
11 |
}; |
|
12 |
use utils::is_name_illegal; |
|
12152 | 13 |
|
13424 | 14 |
pub fn handle(server: &mut HWServer, client_id: ClientId, message: HWProtocolMessage) { |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
15 |
use protocol::messages::HWProtocolMessage::*; |
12152 | 16 |
match message { |
13421 | 17 |
CreateRoom(name, password) => { |
18 |
let actions = |
|
19 |
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("A room with the same name already exists.".to_string())] |
|
23 |
} else { |
|
24 |
let flags_msg = ClientFlags( |
|
25 |
"+hr".to_string(), |
|
13424 | 26 |
vec![server.clients[client_id].nick.clone()]); |
13421 | 27 |
vec![AddRoom(name, password), |
13424 | 28 |
flags_msg.send_self().action()] |
13421 | 29 |
}; |
13424 | 30 |
server.react(client_id, actions); |
13421 | 31 |
}, |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
32 |
Chat(msg) => { |
13424 | 33 |
let chat_msg = ChatMsg(server.clients[client_id].nick.clone(), msg); |
34 |
server.react(client_id, vec![chat_msg.send_all().but_self().action()]); |
|
12152 | 35 |
}, |
13421 | 36 |
JoinRoom(name, password) => { |
37 |
let actions; |
|
38 |
{ |
|
39 |
let room = server.rooms.iter().find(|(_, r)| r.name == name); |
|
40 |
let room_id = room.map(|(_, r)| r.id); |
|
41 |
let nicks = server.clients.iter() |
|
42 |
.filter(|(_, c)| c.room_id == room_id) |
|
43 |
.map(|(_, c)| c.nick.clone()) |
|
44 |
.collect(); |
|
13424 | 45 |
let c = &mut server.clients[client_id]; |
13421 | 46 |
actions = match room { |
47 |
None => vec![Warn("No such room.".to_string())], |
|
48 |
Some((_, r)) => { |
|
49 |
if c.protocol_number != r.protocol_number { |
|
50 |
vec![Warn("Room version incompatible to your Hedgewars version!".to_string())] |
|
51 |
} else { |
|
52 |
vec![MoveToRoom(r.id), |
|
13424 | 53 |
RoomJoined(nicks).send_self().action()] |
13421 | 54 |
} |
55 |
} |
|
56 |
}; |
|
12152 | 57 |
} |
13424 | 58 |
server.react(client_id, actions); |
12152 | 59 |
}, |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12857
diff
changeset
|
60 |
List => warn!("Deprecated LIST message received"), |
12152 | 61 |
_ => warn!("Incorrect command in lobby state"), |
62 |
} |
|
63 |
} |