gameServer2/src/server/handlers/loggingin.rs
author alfadur
Mon, 18 Jun 2018 09:22:53 -0400
changeset 13421 cdf69667593b
parent 13124 1e39b8749072
child 13424 81e0ed105f5d
permissions -rw-r--r--
partial room implementation
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
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     3
use server::server::HWServer;
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     4
use server::actions::Action;
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     5
use server::actions::Action::*;
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     6
use protocol::messages::HWProtocolMessage;
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     7
use protocol::messages::HWServerMessage::*;
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     8
use utils::is_name_illegal;
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
     9
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
    10
pub fn handle(server: & mut HWServer, token: usize, message: HWProtocolMessage) {
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    11
    match message {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    12
        HWProtocolMessage::Nick(nick) => {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    13
            let actions;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    14
            {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    15
                let client = &mut server.clients[token];
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    16
                debug!("{} {}", nick, is_name_illegal(&nick));
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    17
                actions = if client.room_id != None {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    18
                    unreachable!()
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    19
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    20
                else if !client.nick.is_empty() {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    21
                    vec![ProtocolError("Nickname already provided.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    22
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    23
                else if     is_name_illegal(&nick) {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    24
                    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
    25
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    26
                else {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    27
                    client.nick = nick.clone();
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    28
                    vec![SendMe(Nick(nick)), CheckRegistered]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    29
                };
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    30
            }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    31
            server.react(token, actions);
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    32
        },
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    33
        HWProtocolMessage::Proto(proto) => {
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    34
            let actions;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    35
            {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    36
                let client = &mut server.clients[token];
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    37
                actions = if client.protocol_number != 0 {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    38
                    vec![ProtocolError("Protocol already known.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    39
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    40
                else if proto == 0 {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    41
                    vec![ProtocolError("Bad number.".to_string())]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    42
                }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    43
                else {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    44
                    client.protocol_number = proto;
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    45
                    vec![SendMe(Proto(proto)), CheckRegistered]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    46
                };
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    47
            }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
    48
            server.react(token, actions);
12152
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    49
        },
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    50
        _ => warn!("Incorrect command in logging-in state"),
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    51
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    52
}