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