rust/hedgewars-server/src/server/handlers/lobby.rs
changeset 14672 6e6632068a33
parent 14671 455865ccd36c
child 14686 9f98086de1b6
equal deleted inserted replaced
14671:455865ccd36c 14672:6e6632068a33
    19     message: HWProtocolMessage,
    19     message: HWProtocolMessage,
    20 ) {
    20 ) {
    21     use crate::protocol::messages::HWProtocolMessage::*;
    21     use crate::protocol::messages::HWProtocolMessage::*;
    22     match message {
    22     match message {
    23         CreateRoom(name, password) => {
    23         CreateRoom(name, password) => {
    24             let actions = if is_name_illegal(&name) {
    24             if is_name_illegal(&name) {
    25                 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())]
    25                 response.add(Warning("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()).send_self());
    26             } else if server.has_room(&name) {
    26             } else if server.has_room(&name) {
    27                 vec![Warn(
    27                 response.add(
    28                     "A room with the same name already exists.".to_string(),
    28                     Warning("A room with the same name already exists.".to_string()).send_self(),
    29                 )]
    29                 );
    30             } else {
    30             } else {
    31                 let flags_msg = ClientFlags(
    31                 let flags_msg = ClientFlags(
    32                     "+hr".to_string(),
    32                     "+hr".to_string(),
    33                     vec![server.clients[client_id].nick.clone()],
    33                     vec![server.clients[client_id].nick.clone()],
    34                 );
    34                 );
    43                         .with_protocol(room.protocol_number),
    43                         .with_protocol(room.protocol_number),
    44                 );
    44                 );
    45                 response.add(flags_msg.send_self());
    45                 response.add(flags_msg.send_self());
    46 
    46 
    47                 response.add(ClientFlags("+i".to_string(), vec![client.nick.clone()]).send_self());
    47                 response.add(ClientFlags("+i".to_string(), vec![client.nick.clone()]).send_self());
    48                 vec![]
       
    49             };
    48             };
    50             server.react(client_id, actions);
       
    51         }
    49         }
    52         Chat(msg) => {
    50         Chat(msg) => {
    53             let actions = vec![ChatMsg {
    51             response.add(
    54                 nick: server.clients[client_id].nick.clone(),
    52                 ChatMsg {
    55                 msg,
    53                     nick: server.clients[client_id].nick.clone(),
    56             }
    54                     msg,
    57             .send_all()
    55                 }
    58             .in_room(server.lobby_id)
    56                 .send_all()
    59             .but_self()
    57                 .in_room(server.lobby_id)
    60             .action()];
    58                 .but_self(),
    61             server.react(client_id, actions);
    59             );
    62         }
    60         }
    63         JoinRoom(name, _password) => {
    61         JoinRoom(name, _password) => {
    64             let room = server.rooms.iter().find(|(_, r)| r.name == name);
    62             let room = server.rooms.iter().find(|(_, r)| r.name == name);
    65             let room_id = room.map(|(_, r)| r.id);
    63             let room_id = room.map(|(_, r)| r.id);
    66             let nicks = server
    64             let nicks = server
    69                 .filter(|(_, c)| c.room_id == room_id)
    67                 .filter(|(_, c)| c.room_id == room_id)
    70                 .map(|(_, c)| c.nick.clone())
    68                 .map(|(_, c)| c.nick.clone())
    71                 .collect();
    69                 .collect();
    72             let c = &mut server.clients[client_id];
    70             let c = &mut server.clients[client_id];
    73 
    71 
    74             let actions = if let Some((_, r)) = room {
    72             if let Some((_, r)) = room {
    75                 if c.protocol_number != r.protocol_number {
    73                 if c.protocol_number != r.protocol_number {
    76                     vec![Warn(
    74                     response.add(
    77                         "Room version incompatible to your Hedgewars version!".to_string(),
    75                         Warning("Room version incompatible to your Hedgewars version!".to_string())
    78                     )]
    76                             .send_self(),
       
    77                     );
    79                 } else if r.is_join_restricted() {
    78                 } else if r.is_join_restricted() {
    80                     vec![Warn(
    79                     response.add(
    81                         "Access denied. This room currently doesn't allow joining.".to_string(),
    80                         Warning(
    82                     )]
    81                             "Access denied. This room currently doesn't allow joining.".to_string(),
       
    82                         )
       
    83                         .send_self(),
       
    84                     );
    83                 } else if r.players_number == u8::max_value() {
    85                 } else if r.players_number == u8::max_value() {
    84                     vec![Warn("This room is already full".to_string())]
    86                     response.add(Warning("This room is already full".to_string()).send_self());
    85                 } else if let Some(room_id) = room_id {
    87                 } else if let Some(room_id) = room_id {
    86                     let nick = c.nick.clone();
    88                     let nick = c.nick.clone();
    87                     server.move_to_room(client_id, room_id);
    89                     server.move_to_room(client_id, room_id);
    88 
    90 
    89                     response.add(RoomJoined(vec![nick.clone()]).send_all().in_room(room_id));
    91                     response.add(RoomJoined(vec![nick.clone()]).send_all().in_room(room_id));
    99                                 msg: room.greeting.clone(),
   101                                 msg: room.greeting.clone(),
   100                             }
   102                             }
   101                             .send_self(),
   103                             .send_self(),
   102                         );
   104                         );
   103                     }
   105                     }
   104 
       
   105                     vec![]
       
   106                 } else {
       
   107                     vec![]
       
   108                 }
   106                 }
   109             } else {
   107             } else {
   110                 vec![Warn("No such room.".to_string())]
   108                 response.add(Warning("No such room.".to_string()).send_self());
   111             };
   109             }
   112             server.react(client_id, actions);
       
   113         }
   110         }
   114         Rnd(v) => {
   111         Rnd(v) => {
   115             server.react(client_id, vec![rnd_reply(&v).send_self().action()]);
   112             response.add(rnd_reply(&v).send_self());
   116         }
   113         }
   117         List => warn!("Deprecated LIST message received"),
   114         List => warn!("Deprecated LIST message received"),
   118         _ => warn!("Incorrect command in lobby state"),
   115         _ => warn!("Incorrect command in lobby state"),
   119     }
   116     }
   120 }
   117 }