gameServer2/src/server/handlers/loggingin.rs
author alfadur
Fri, 20 Jul 2018 22:14:20 +0300
changeset 13528 c8b626b0a3ad
parent 13450 d79795acaa73
child 13666 09f4a30e50cc
permissions -rw-r--r--
Implement save/delete commands
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
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
     3
use server::{
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
     4
    server::HWServer,
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13419
diff changeset
     5
    coretypes::ClientId,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
     6
    actions::{Action, Action::*}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
     7
};
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
     8
use protocol::messages::{
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
     9
    HWProtocolMessage, HWServerMessage::*
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    10
};
13416
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
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    13
pub fn handle(server: & mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    14
    match message {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    15
        HWProtocolMessage::Nick(nick) => {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    16
            let actions;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    17
            {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    18
                let client = &mut server.clients[client_id];
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    19
                debug!("{} {}", nick, is_name_illegal(&nick));
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    20
                actions = if client.room_id != None {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    21
                    unreachable!()
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    22
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    23
                else if !client.nick.is_empty() {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    24
                    vec![ProtocolError("Nickname already provided.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    25
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    26
                else if     is_name_illegal(&nick) {
cdf69667593b partial room implementation
alfadur
parents: 13119
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: 13119
diff changeset
    28
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    29
                else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    30
                    client.nick = nick.clone();
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    31
                    vec![Nick(nick).send_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    32
                         CheckRegistered]
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    33
                };
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    34
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    35
            server.react(client_id, actions);
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    36
        },
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    37
        HWProtocolMessage::Proto(proto) => {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    38
            let actions;
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    39
            {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    40
                let client = &mut server.clients[client_id];
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    41
                actions = if client.protocol_number != 0 {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    42
                    vec![ProtocolError("Protocol already known.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    43
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    44
                else if proto == 0 {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    45
                    vec![ProtocolError("Bad number.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    46
                }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    47
                else {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    48
                    client.protocol_number = proto;
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    49
                    vec![Proto(proto).send_self().action(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    50
                         CheckRegistered]
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    51
                };
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    52
            }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    53
            server.react(client_id, actions);
12147
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
}