gameServer2/src/server/handlers/inroom.rs
author alfadur
Mon, 18 Jun 2018 09:22:53 -0400
changeset 13421 cdf69667593b
parent 13124 1e39b8749072
child 13424 81e0ed105f5d
permissions -rw-r--r--
partial room implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     1
use mio;
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     2
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     3
use server::{
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     4
    server::HWServer,
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     5
    actions::{Action, Action::*}
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     6
};
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     7
use protocol::messages::{
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     8
    HWProtocolMessage,
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     9
    HWServerMessage::*
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    10
};
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    11
use utils::is_name_illegal;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    12
use std::mem::swap;
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    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
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    15
    use protocol::messages::HWProtocolMessage::*;
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    16
    match message {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    17
        Part(None) => server.react(token, vec![
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    18
            MoveToLobby("part".to_string())]),
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    19
        Part(Some(msg)) => server.react(token, vec![
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    20
            MoveToLobby(format!("part: {}", msg))]),
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    21
        Chat(msg) => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    22
            let chat_msg;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    23
            let room_id;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    24
            {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    25
                let c = &mut server.clients[token];
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    26
                chat_msg = ChatMsg(c.nick.clone(), msg);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    27
                room_id = c.room_id;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    28
            }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    29
            let client_ids = server.other_clients_in_room(token);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    30
            server.react(token, vec![
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    31
                SendToSelected(client_ids, chat_msg)]);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    32
        },
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    33
        RoomName(new_name) => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    34
            let actions =
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    35
                if is_name_illegal(&new_name) {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    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())]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    37
                } else if server.has_room(&new_name) {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    38
                    vec![Warn("A room with the same name already exists.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    39
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    40
                    let mut old_name = new_name.clone();
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    41
                    if let (c, Some(r)) = server.client_and_room(token) {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    42
                        swap(&mut r.name, &mut old_name);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    43
                        vec![SendRoomUpdate(Some(old_name))]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    44
                    } else {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    45
                        Vec::new()
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    46
                    }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    47
                };
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    48
            server.react(token, actions);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    49
        }
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    50
        _ => warn!("Unimplemented!"),
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    51
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    52
}