author | alfadur |
Thu, 28 Jun 2018 00:10:15 +0300 | |
changeset 13434 | 4c5ed27b1ff8 |
parent 13432 | 6f6a866c86a2 |
child 13449 | 914f9b970f4d |
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]; |
13432 | 46 |
|
47 |
actions = if let Some((_, r)) = room { |
|
48 |
if c.protocol_number != r.protocol_number { |
|
49 |
vec![Warn("Room version incompatible to your Hedgewars version!".to_string())] |
|
50 |
} else { |
|
51 |
vec![MoveToRoom(r.id), |
|
52 |
RoomJoined(nicks).send_self().action()] |
|
13421 | 53 |
} |
13432 | 54 |
} else { |
55 |
vec![Warn("No such room.".to_string())] |
|
13421 | 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 |
} |