gameServer2/src/server/handlers/loggingin.rs
author alfadur
Fri, 06 Jul 2018 21:03:03 +0300
changeset 13482 f748a72432f2
parent 13424 81e0ed105f5d
child 13483 d79795acaa73
permissions -rw-r--r--
Implement greetings & fixed rooms
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
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
     3
use server::{
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
     4
    server::HWServer,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
     5
    client::ClientId,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
     6
    actions::{Action, Action::*}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
     7
};
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
     8
use protocol::messages::{
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
     9
    HWProtocolMessage, HWServerMessage::*
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    10
};
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    11
use utils::is_name_illegal;
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    12
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    13
pub fn handle(server: & mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    14
    match message {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    15
        HWProtocolMessage::Nick(nick) => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    16
            let actions;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    17
            {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    18
                let client = &mut server.clients[client_id];
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    19
                debug!("{} {}", nick, is_name_illegal(&nick));
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    20
                actions = if client.room_id != None {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    21
                    unreachable!()
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    22
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    23
                else if !client.nick.is_empty() {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    24
                    vec![ProtocolError("Nickname already provided.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    25
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    26
                else if     is_name_illegal(&nick) {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    27
                    vec![ByeClient("Illegal nickname! Nicknames 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
    28
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    29
                else {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    30
                    client.nick = nick.clone();
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    31
                    vec![Nick(nick).send_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    32
                         CheckRegistered]
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    33
                };
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    34
            }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    35
            server.react(client_id, actions);
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    36
        },
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    37
        HWProtocolMessage::Proto(proto) => {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    38
            let actions;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    39
            {
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    40
                let client = &mut server.clients[client_id];
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    41
                actions = if client.protocol_number != 0 {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    42
                    vec![ProtocolError("Protocol already known.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    43
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    44
                else if proto == 0 {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    45
                    vec![ProtocolError("Bad number.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    46
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    47
                else {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    48
                    client.protocol_number = proto;
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    49
                    vec![Proto(proto).send_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    50
                         CheckRegistered]
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    51
                };
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    52
            }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
    53
            server.react(client_id, actions);
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    54
        },
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    55
        _ => warn!("Incorrect command in logging-in state"),
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    56
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    57
}