gameServer2/src/server/handlers/lobby.rs
author alfadur
Thu, 21 Jun 2018 17:23:10 -0400
changeset 13424 81e0ed105f5d
parent 13421 cdf69667593b
child 13432 6f6a866c86a2
permissions -rw-r--r--
implementation of team related messages
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,
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
     5
    client::ClientId,
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     6
    actions::{Action, Action::*}
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     7
};
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     8
use protocol::messages::{
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     9
    HWProtocolMessage,
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    10
    HWServerMessage::*
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    11
};
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    12
use utils::is_name_illegal;
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    13
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    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
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    16
    match message {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    17
        CreateRoom(name, password) => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    18
            let actions =
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    19
                if is_name_illegal(&name) {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    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())]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    21
                } else if server.has_room(&name) {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    22
                    vec![Warn("A room with the same name already exists.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    23
                } else {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    24
                    let flags_msg = ClientFlags(
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    25
                        "+hr".to_string(),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    26
                        vec![server.clients[client_id].nick.clone()]);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    27
                    vec![AddRoom(name, password),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    28
                         flags_msg.send_self().action()]
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    29
                };
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    30
            server.react(client_id, actions);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    31
        },
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    32
        Chat(msg) => {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    33
            let chat_msg = ChatMsg(server.clients[client_id].nick.clone(), msg);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    34
            server.react(client_id, vec![chat_msg.send_all().but_self().action()]);
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    35
        },
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    36
        JoinRoom(name, password) => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    37
            let actions;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    38
            {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    39
                let room = server.rooms.iter().find(|(_, r)| r.name == name);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    40
                let room_id = room.map(|(_, r)| r.id);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    41
                let nicks = server.clients.iter()
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    42
                    .filter(|(_, c)| c.room_id == room_id)
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    43
                    .map(|(_, c)| c.nick.clone())
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    44
                    .collect();
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    45
                let c = &mut server.clients[client_id];
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    46
                actions = match room {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    47
                    None => vec![Warn("No such room.".to_string())],
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    48
                    Some((_, r)) => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    49
                        if c.protocol_number != r.protocol_number {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    50
                            vec![Warn("Room version incompatible to your Hedgewars version!".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    51
                        } else {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    52
                            vec![MoveToRoom(r.id),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    53
                                 RoomJoined(nicks).send_self().action()]
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    54
                        }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    55
                    }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    56
                };
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    57
            }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    58
            server.react(client_id, actions);
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    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
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    61
        _ => warn!("Incorrect command in lobby state"),
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    62
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    63
}