gameServer2/src/server/handlers/lobby.rs
author alfadur
Mon, 18 Jun 2018 09:22:53 -0400
changeset 13416 cdf69667593b
parent 13119 1e39b8749072
child 13419 81e0ed105f5d
permissions -rw-r--r--
partial room implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     1
use mio;
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     2
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     3
use server::{
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     4
    server::HWServer,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     5
    actions::{Action, Action::*}
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     6
};
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     7
use protocol::messages::{
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     8
    HWProtocolMessage,
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     9
    HWServerMessage::*
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    10
};
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    11
use utils::is_name_illegal;
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    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
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    15
    match message {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    16
        CreateRoom(name, password) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    17
            let actions =
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    18
                if is_name_illegal(&name) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    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())]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    20
                } else if server.has_room(&name) {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    21
                    vec![Warn("A room with the same name already exists.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    22
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    23
                    let flags_msg = ClientFlags(
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    24
                        "+hr".to_string(),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    25
                        vec![server.clients[token].nick.clone()]);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    26
                    vec![AddRoom(name, password),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    27
                         SendMe(flags_msg)]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    28
                };
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    29
            server.react(token, actions);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    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
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    34
        },
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    35
        JoinRoom(name, password) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    36
            let actions;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    37
            {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    38
                let room = server.rooms.iter().find(|(_, r)| r.name == name);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    39
                let room_id = room.map(|(_, r)| r.id);
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    40
                let nicks = server.clients.iter()
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    41
                    .filter(|(_, c)| c.room_id == room_id)
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    42
                    .map(|(_, c)| c.nick.clone())
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    43
                    .collect();
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    44
                let c = &mut server.clients[token];
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    45
                actions = match room {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    46
                    None => vec![Warn("No such room.".to_string())],
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    47
                    Some((_, r)) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    48
                        if c.protocol_number != r.protocol_number {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    49
                            vec![Warn("Room version incompatible to your Hedgewars version!".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    50
                        } else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    51
                            vec![MoveToRoom(r.id),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    52
                                 SendMe(RoomJoined(nicks))]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    53
                        }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    54
                    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    55
                };
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    56
            }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    57
            server.react(token, actions);
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    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
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    60
        _ => warn!("Incorrect command in lobby state"),
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    61
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    62
}