rust/hedgewars-server/src/server/handlers/common.rs
changeset 14690 f61ce544d436
parent 14689 aae29ba56aec
child 14691 2071da901c63
equal deleted inserted replaced
14689:aae29ba56aec 14690:f61ce544d436
     3     protocol::messages::{
     3     protocol::messages::{
     4         HWProtocolMessage::{self, Rnd},
     4         HWProtocolMessage::{self, Rnd},
     5         HWServerMessage::{self, *},
     5         HWServerMessage::{self, *},
     6     },
     6     },
     7     server::{
     7     server::{
     8         actions::Action,
       
     9         client::HWClient,
     8         client::HWClient,
    10         core::HWServer,
     9         core::HWServer,
    11         coretypes::{ClientId, GameCfg, RoomId, Vote, VoteType},
    10         coretypes::{ClientId, GameCfg, RoomId, Vote, VoteType},
    12         room::HWRoom,
    11         room::HWRoom,
    13     },
    12     },
   135         room.remove_team(&team_name);
   134         room.remove_team(&team_name);
   136         response.add(TeamRemove(team_name).send_all().in_room(room.id));
   135         response.add(TeamRemove(team_name).send_all().in_room(room.id));
   137     }
   136     }
   138 }
   137 }
   139 
   138 
   140 pub fn exit_room(client: &HWClient, room: &mut HWRoom, response: &mut Response, msg: &str) {
   139 fn remove_client_from_room(
       
   140     client: &mut HWClient,
       
   141     room: &mut HWRoom,
       
   142     response: &mut Response,
       
   143     msg: &str,
       
   144 ) {
   141     if room.players_number > 1 || room.is_fixed() {
   145     if room.players_number > 1 || room.is_fixed() {
   142         room.players_number -= 1;
   146         room.players_number -= 1;
   143         if client.is_ready() && room.ready_players_number > 0 {
   147         if client.is_ready() && room.ready_players_number > 0 {
   144             room.ready_players_number -= 1;
   148             room.ready_players_number -= 1;
   145         }
   149         }
   157                     .in_room(room.id)
   161                     .in_room(room.id)
   158                     .but_self(),
   162                     .but_self(),
   159             );
   163             );
   160         }
   164         }
   161 
   165 
   162         //ChangeMaster(room.id, None));
   166         if client.is_master() && !room.is_fixed() {
       
   167             client.set_is_master(false);
       
   168             response.add(
       
   169                 ClientFlags("-h".to_string(), vec![client.nick.clone()])
       
   170                     .send_all()
       
   171                     .in_room(room.id),
       
   172             );
       
   173             room.master_id = None;
       
   174         }
   163     }
   175     }
   164 
   176 
   165     let update_msg = if room.players_number == 0 && !room.is_fixed() {
   177     let update_msg = if room.players_number == 0 && !room.is_fixed() {
   166         RoomRemove(room.name.clone())
   178         RoomRemove(room.name.clone())
   167     } else {
   179     } else {
   170     response.add(update_msg.send_all().with_protocol(room.protocol_number));
   182     response.add(update_msg.send_all().with_protocol(room.protocol_number));
   171 
   183 
   172     response.add(ClientFlags("-i".to_string(), vec![client.nick.clone()]).send_all());
   184     response.add(ClientFlags("-i".to_string(), vec![client.nick.clone()]).send_all());
   173 }
   185 }
   174 
   186 
       
   187 pub fn exit_room(server: &mut HWServer, client_id: ClientId, response: &mut Response, msg: &str) {
       
   188     let client = &mut server.clients[client_id];
       
   189 
       
   190     if let Some(room_id) = client.room_id {
       
   191         if room_id != server.lobby_id {
       
   192             let room = &mut server.rooms[room_id];
       
   193 
       
   194             remove_client_from_room(client, room, response, msg);
       
   195             client.room_id = Some(server.lobby_id);
       
   196 
       
   197             if !room.is_fixed() && room.master_id == None {
       
   198                 if let Some(new_master_id) = server.room_clients(room_id).first().cloned() {
       
   199                     let new_master_nick = server.clients[new_master_id].nick.clone();
       
   200                     let room = &mut server.rooms[room_id];
       
   201                     room.master_id = Some(new_master_id);
       
   202                     server.clients[new_master_id].set_is_master(true);
       
   203 
       
   204                     if room.protocol_number < 42 {
       
   205                         room.name = new_master_nick.clone();
       
   206                     }
       
   207 
       
   208                     room.set_join_restriction(false);
       
   209                     room.set_team_add_restriction(false);
       
   210                     room.set_unregistered_players_restriction(true);
       
   211 
       
   212                     response.add(
       
   213                         ClientFlags("+h".to_string(), vec![new_master_nick])
       
   214                             .send_all()
       
   215                             .in_room(room.id),
       
   216                     );
       
   217                 }
       
   218             }
       
   219         }
       
   220     }
       
   221 }
       
   222 
   175 pub fn remove_client(server: &mut HWServer, response: &mut Response, msg: String) {
   223 pub fn remove_client(server: &mut HWServer, response: &mut Response, msg: String) {
   176     let client_id = response.client_id();
   224     let client_id = response.client_id();
   177     let lobby_id = server.lobby_id;
   225     let lobby_id = server.lobby_id;
   178     let client = &mut server.clients[client_id];
   226     let client = &mut server.clients[client_id];
   179     let (nick, room_id) = (client.nick.clone(), client.room_id);
   227     let (nick, room_id) = (client.nick.clone(), client.room_id);
   180 
   228 
   181     if let Some(room_id) = room_id {
   229     exit_room(server, client_id, response, &msg);
   182         let room = &mut server.rooms[room_id];
       
   183         exit_room(client, room, response, &msg);
       
   184         client.room_id = Some(lobby_id);
       
   185     }
       
   186 
   230 
   187     server.remove_client(client_id);
   231     server.remove_client(client_id);
   188 
   232 
   189     response.add(LobbyLeft(nick, msg.to_string()).send_all());
   233     response.add(LobbyLeft(nick, msg.to_string()).send_all());
   190     response.add(Bye("User quit: ".to_string() + &msg).send_self());
   234     response.add(Bye("User quit: ".to_string() + &msg).send_self());
   260     match kind {
   304     match kind {
   261         VoteType::Kick(nick) => {
   305         VoteType::Kick(nick) => {
   262             if let Some(client) = server.find_client(&nick) {
   306             if let Some(client) = server.find_client(&nick) {
   263                 if client.room_id == Some(room_id) {
   307                 if client.room_id == Some(room_id) {
   264                     let id = client.id;
   308                     let id = client.id;
   265                     response.add(Kicked.send_self());
   309                     response.add(Kicked.send(id));
   266                     exit_room(
   310                     exit_room(server, id, response, "kicked");
   267                         &mut server.clients[client_id],
       
   268                         &mut server.rooms[room_id],
       
   269                         response,
       
   270                         "kicked",
       
   271                     );
       
   272                 }
   311                 }
   273             }
   312             }
   274         }
   313         }
   275         VoteType::Map(None) => (),
   314         VoteType::Map(None) => (),
   276         VoteType::Map(Some(name)) => {
   315         VoteType::Map(Some(name)) => {