gameServer2/src/server/handlers/lobby.rs
changeset 13416 cdf69667593b
parent 13119 1e39b8749072
child 13419 81e0ed105f5d
equal deleted inserted replaced
13415:0eedc17055a0 13416:cdf69667593b
     1 use mio;
     1 use mio;
     2 
     2 
     3 use server::server::HWServer;
     3 use server::{
     4 use server::actions::Action;
     4     server::HWServer,
     5 use server::actions::Action::*;
     5     actions::{Action, Action::*}
     6 use protocol::messages::HWProtocolMessage;
     6 };
     7 use protocol::messages::HWServerMessage::*;
     7 use protocol::messages::{
       
     8     HWProtocolMessage,
       
     9     HWServerMessage::*
       
    10 };
       
    11 use utils::is_name_illegal;
     8 
    12 
     9 pub fn handle(server: &mut HWServer, token: usize, message: HWProtocolMessage) {
    13 pub fn handle(server: &mut HWServer, token: usize, message: HWProtocolMessage) {
    10     use protocol::messages::HWProtocolMessage::*;
    14     use protocol::messages::HWProtocolMessage::*;
    11     match message {
    15     match message {
       
    16         CreateRoom(name, password) => {
       
    17             let actions =
       
    18                 if is_name_illegal(&name) {
       
    19                     vec![Warn("Illegal room name! A room name 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())]
       
    20                 } else if server.has_room(&name) {
       
    21                     vec![Warn("A room with the same name already exists.".to_string())]
       
    22                 } else {
       
    23                     let flags_msg = ClientFlags(
       
    24                         "+hr".to_string(),
       
    25                         vec![server.clients[token].nick.clone()]);
       
    26                     vec![AddRoom(name, password),
       
    27                          SendMe(flags_msg)]
       
    28                 };
       
    29             server.react(token, actions);
       
    30         },
    12         Chat(msg) => {
    31         Chat(msg) => {
    13             let chat_msg = ChatMsg(server.clients[token].nick.clone(), msg);
    32             let chat_msg = ChatMsg(server.clients[token].nick.clone(), msg);
    14             server.react(token, vec![SendAllButMe(chat_msg)]);
    33             server.react(token, vec![SendAllButMe(chat_msg)]);
    15         },
    34         },
    16         CreateRoom(name, password) => {
    35         JoinRoom(name, password) => {
    17             let room_exists = server.rooms.iter().find(|&(_, r)| r.name == name).is_some();
    36             let actions;
    18             if room_exists {
    37             {
    19                 server.react(token, vec![Warn("Room exists".to_string())]);
    38                 let room = server.rooms.iter().find(|(_, r)| r.name == name);
    20             } else {
    39                 let room_id = room.map(|(_, r)| r.id);
    21                 let flags_msg = ClientFlags("+hr".to_string(), vec![server.clients[token].nick.clone()]);
    40                 let nicks = server.clients.iter()
    22                 {
    41                     .filter(|(_, c)| c.room_id == room_id)
    23                     let c = &mut server.clients[token];
    42                     .map(|(_, c)| c.nick.clone())
    24                     c.is_master = true;
    43                     .collect();
    25                     c.is_ready = true;
    44                 let c = &mut server.clients[token];
    26                     c.is_joined_mid_game = false;
    45                 actions = match room {
    27                 }
    46                     None => vec![Warn("No such room.".to_string())],
    28                 server.react(token, vec![
    47                     Some((_, r)) => {
    29                     AddRoom(name, password)
    48                         if c.protocol_number != r.protocol_number {
    30                     , SendMe(flags_msg)
    49                             vec![Warn("Room version incompatible to your Hedgewars version!".to_string())]
    31                     ]);
    50                         } else {
       
    51                             vec![MoveToRoom(r.id),
       
    52                                  SendMe(RoomJoined(nicks))]
       
    53                         }
       
    54                     }
       
    55                 };
    32             }
    56             }
    33         },
    57             server.react(token, actions);
    34         Join(name, password) => {
       
    35 
       
    36         },
    58         },
    37         List => warn!("Deprecated LIST message received"),
    59         List => warn!("Deprecated LIST message received"),
    38         _ => warn!("Incorrect command in lobby state"),
    60         _ => warn!("Incorrect command in lobby state"),
    39     }
    61     }
    40 }
    62 }