author | alfadur |
Mon, 18 Jun 2018 09:22:53 -0400 | |
changeset 13421 | cdf69667593b |
parent 13124 | 1e39b8749072 |
child 13424 | 81e0ed105f5d |
permissions | -rw-r--r-- |
12152 | 1 |
use mio; |
2 |
||
13421 | 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; |
|
12 |
use std::mem::swap; |
|
12152 | 13 |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
14 |
pub fn handle(server: &mut HWServer, token: usize, message: HWProtocolMessage) { |
13421 | 15 |
use protocol::messages::HWProtocolMessage::*; |
12152 | 16 |
match message { |
13421 | 17 |
Part(None) => server.react(token, vec![ |
18 |
MoveToLobby("part".to_string())]), |
|
19 |
Part(Some(msg)) => server.react(token, vec![ |
|
20 |
MoveToLobby(format!("part: {}", msg))]), |
|
21 |
Chat(msg) => { |
|
22 |
let chat_msg; |
|
23 |
let room_id; |
|
24 |
{ |
|
25 |
let c = &mut server.clients[token]; |
|
26 |
chat_msg = ChatMsg(c.nick.clone(), msg); |
|
27 |
room_id = c.room_id; |
|
28 |
} |
|
29 |
let client_ids = server.other_clients_in_room(token); |
|
30 |
server.react(token, vec![ |
|
31 |
SendToSelected(client_ids, chat_msg)]); |
|
32 |
}, |
|
33 |
RoomName(new_name) => { |
|
34 |
let actions = |
|
35 |
if is_name_illegal(&new_name) { |
|
36 |
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())] |
|
37 |
} else if server.has_room(&new_name) { |
|
38 |
vec![Warn("A room with the same name already exists.".to_string())] |
|
39 |
} else { |
|
40 |
let mut old_name = new_name.clone(); |
|
41 |
if let (c, Some(r)) = server.client_and_room(token) { |
|
42 |
swap(&mut r.name, &mut old_name); |
|
43 |
vec![SendRoomUpdate(Some(old_name))] |
|
44 |
} else { |
|
45 |
Vec::new() |
|
46 |
} |
|
47 |
}; |
|
48 |
server.react(token, actions); |
|
49 |
} |
|
12152 | 50 |
_ => warn!("Unimplemented!"), |
51 |
} |
|
52 |
} |