gameServer2/src/server/handlers/loggingin.rs
changeset 13666 09f4a30e50cc
parent 13478 d79795acaa73
child 13798 4664da990556
equal deleted inserted replaced
13665:5664650befcd 13666:09f4a30e50cc
     1 use mio;
     1 use mio;
     2 
     2 
     3 use server::{
     3 use crate::{
     4     server::HWServer,
     4     server::{
     5     coretypes::ClientId,
     5         server::HWServer,
     6     actions::{Action, Action::*}
     6         coretypes::ClientId,
       
     7         actions::{Action, Action::*}
       
     8     },
       
     9     protocol::messages::{
       
    10         HWProtocolMessage, HWServerMessage::*
       
    11     },
       
    12     utils::is_name_illegal
     7 };
    13 };
     8 use protocol::messages::{
       
     9     HWProtocolMessage, HWServerMessage::*
       
    10 };
       
    11 use utils::is_name_illegal;
       
    12 
    14 
    13 pub fn handle(server: & mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
    15 pub fn handle(server: & mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
    14     match message {
    16     match message {
    15         HWProtocolMessage::Nick(nick) => {
    17         HWProtocolMessage::Nick(nick) => {
    16             let actions;
    18             let client = &mut server.clients[client_id];
    17             {
    19             debug!("{} {}", nick, is_name_illegal(&nick));
    18                 let client = &mut server.clients[client_id];
    20             let actions = if client.room_id != None {
    19                 debug!("{} {}", nick, is_name_illegal(&nick));
    21                 unreachable!()
    20                 actions = if client.room_id != None {
       
    21                     unreachable!()
       
    22                 }
       
    23                 else if !client.nick.is_empty() {
       
    24                     vec![ProtocolError("Nickname already provided.".to_string())]
       
    25                 }
       
    26                 else if     is_name_illegal(&nick) {
       
    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())]
       
    28                 }
       
    29                 else {
       
    30                     client.nick = nick.clone();
       
    31                     vec![Nick(nick).send_self().action(),
       
    32                          CheckRegistered]
       
    33                 };
       
    34             }
    22             }
       
    23             else if !client.nick.is_empty() {
       
    24                 vec![ProtocolError("Nickname already provided.".to_string())]
       
    25             }
       
    26             else if     is_name_illegal(&nick) {
       
    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())]
       
    28             }
       
    29             else {
       
    30                 client.nick = nick.clone();
       
    31                 vec![Nick(nick).send_self().action(),
       
    32                      CheckRegistered]
       
    33             };
       
    34 
    35             server.react(client_id, actions);
    35             server.react(client_id, actions);
    36         },
    36         },
    37         HWProtocolMessage::Proto(proto) => {
    37         HWProtocolMessage::Proto(proto) => {
    38             let actions;
    38             let client = &mut server.clients[client_id];
    39             {
    39             let actions = if client.protocol_number != 0 {
    40                 let client = &mut server.clients[client_id];
    40                 vec![ProtocolError("Protocol already known.".to_string())]
    41                 actions = if client.protocol_number != 0 {
       
    42                     vec![ProtocolError("Protocol already known.".to_string())]
       
    43                 }
       
    44                 else if proto == 0 {
       
    45                     vec![ProtocolError("Bad number.".to_string())]
       
    46                 }
       
    47                 else {
       
    48                     client.protocol_number = proto;
       
    49                     vec![Proto(proto).send_self().action(),
       
    50                          CheckRegistered]
       
    51                 };
       
    52             }
    41             }
       
    42             else if proto == 0 {
       
    43                 vec![ProtocolError("Bad number.".to_string())]
       
    44             }
       
    45             else {
       
    46                 client.protocol_number = proto;
       
    47                 vec![Proto(proto).send_self().action(),
       
    48                      CheckRegistered]
       
    49             };
    53             server.react(client_id, actions);
    50             server.react(client_id, actions);
    54         },
    51         },
    55         _ => warn!("Incorrect command in logging-in state"),
    52         _ => warn!("Incorrect command in logging-in state"),
    56     }
    53     }
    57 }
    54 }