gameServer2/src/server/handlers/loggingin.rs
author alfadur
Mon, 27 Aug 2018 22:28:56 +0300
changeset 13709 e335daaa77a9
parent 13666 09f4a30e50cc
child 13771 4664da990556
permissions -rw-r--r--
Add hogs per team named constant that absolutely no one asked for
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
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     3
use crate::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     4
    server::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     5
        server::HWServer,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     6
        coretypes::ClientId,
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     7
        actions::{Action, Action::*}
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     8
    },
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     9
    protocol::messages::{
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    10
        HWProtocolMessage, HWServerMessage::*
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    11
    },
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    12
    utils::is_name_illegal
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    13
};
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    14
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    15
pub fn handle(server: & mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    16
    match message {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    17
        HWProtocolMessage::Nick(nick) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    18
            let client = &mut server.clients[client_id];
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    19
            debug!("{} {}", nick, is_name_illegal(&nick));
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    20
            let actions = if client.room_id != None {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    21
                unreachable!()
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    22
            }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    23
            else if !client.nick.is_empty() {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    24
                vec![ProtocolError("Nickname already provided.".to_string())]
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    25
            }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    26
            else if     is_name_illegal(&nick) {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
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())]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    28
            }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    29
            else {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    30
                client.nick = nick.clone();
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    31
                vec![Nick(nick).send_self().action(),
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    32
                     CheckRegistered]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    33
            };
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
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) => {
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    38
            let client = &mut server.clients[client_id];
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    39
            let actions = if client.protocol_number != 0 {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    40
                vec![ProtocolError("Protocol already known.".to_string())]
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    41
            }
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    42
            else if proto == 0 {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    43
                vec![ProtocolError("Bad number.".to_string())]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    44
            }
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    45
            else {
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    46
                client.protocol_number = proto;
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    47
                vec![Proto(proto).send_self().action(),
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    48
                     CheckRegistered]
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
    49
            };
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    50
            server.react(client_id, actions);
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    51
        },
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    52
        _ => warn!("Incorrect command in logging-in state"),
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    53
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    54
}