rust/hedgewars-server/src/handlers/common.rs
changeset 15545 f4f6060b536c
parent 15544 4a0b06b03199
child 15547 16d3c9acd715
equal deleted inserted replaced
15544:4a0b06b03199 15545:f4f6060b536c
     1 use crate::{
     1 use crate::{
     2     core::{
     2     core::{
     3         client::HwClient,
     3         client::HwClient,
     4         room::HwRoom,
     4         room::HwRoom,
     5         server::{
     5         server::{EndGameResult, HwServer, JoinRoomError, LeaveRoomResult, StartGameError},
     6             EndGameResult, HwServer, JoinRoomError, LeaveRoomError, LeaveRoomResult, StartGameError,
       
     7         },
       
     8         types::{ClientId, GameCfg, RoomId, TeamInfo, Vote, VoteType},
     6         types::{ClientId, GameCfg, RoomId, TeamInfo, Vote, VoteType},
     9     },
     7     },
    10     protocol::messages::{
     8     protocol::messages::{
    11         add_flags, remove_flags, server_chat,
     9         add_flags, remove_flags, server_chat,
    12         HwProtocolMessage::{self, Rnd},
    10         HwProtocolMessage::{self, Rnd},
    72     ];
    70     ];
    73 
    71 
    74     let server_msg = ServerMessage(server.get_greetings(client).to_string());
    72     let server_msg = ServerMessage(server.get_greetings(client).to_string());
    75 
    73 
    76     let rooms_msg = Rooms(
    74     let rooms_msg = Rooms(
    77         server.iter_rooms()
    75         server
       
    76             .iter_rooms()
    78             .filter(|r| r.protocol_number == client.protocol_number)
    77             .filter(|r| r.protocol_number == client.protocol_number)
    79             .flat_map(|r| r.info(r.master_id.map(|id| server.client(id))))
    78             .flat_map(|r| r.info(r.master_id.map(|id| server.client(id))))
    80             .collect(),
    79             .collect(),
    81     );
    80     );
    82 
    81 
   258             );
   257             );
   259         }
   258         }
   260     }
   259     }
   261 }
   260 }
   262 
   261 
   263 pub fn get_room_leave_data(
       
   264     server: &HwServer,
       
   265     room_id: RoomId,
       
   266     leave_message: &str,
       
   267     result: Result<LeaveRoomResult, LeaveRoomError>,
       
   268     response: &mut Response,
       
   269 ) {
       
   270     match result {
       
   271         Ok(result) => {
       
   272             let room = server.room(room_id);
       
   273             get_room_leave_result(server, room, leave_message, result, response)
       
   274         }
       
   275         Err(_) => (),
       
   276     }
       
   277 }
       
   278 
       
   279 pub fn remove_client(server: &mut HwServer, response: &mut Response, msg: String) {
   262 pub fn remove_client(server: &mut HwServer, response: &mut Response, msg: String) {
   280     let client_id = response.client_id();
   263     let client_id = response.client_id();
   281     let client = server.client(client_id);
   264     let client = server.client(client_id);
   282     let nick = client.nick.clone();
   265     let nick = client.nick.clone();
   283 
   266 
   284     if let Some(room_id) = client.room_id {
   267     if let Some(mut room_control) = server.get_room_control(client_id) {
   285         let result = server.leave_room(client_id);
   268         let room_id = room_control.room().id;
   286         get_room_leave_data(server, room_id, &msg, result, response);
   269         let result = room_control.leave_room();
       
   270         get_room_leave_result(server, server.room(room_id), &msg, result, response);
   287     }
   271     }
   288 
   272 
   289     server.remove_client(client_id);
   273     server.remove_client(client_id);
   290 
   274 
   291     response.add(LobbyLeft(nick, msg.clone()).send_all());
   275     response.add(LobbyLeft(nick, msg.clone()).send_all());
   363     response: &mut Response,
   347     response: &mut Response,
   364     kind: VoteType,
   348     kind: VoteType,
   365 ) {
   349 ) {
   366     match kind {
   350     match kind {
   367         VoteType::Kick(nick) => {
   351         VoteType::Kick(nick) => {
   368             if let Some(client) = server.find_client(&nick) {
   352             if let Some(kicked_client) = server.find_client(&nick) {
   369                 if client.room_id == Some(room_id) {
   353                 let kicked_id = kicked_client.id;
   370                     let id = client.id;
   354                 if kicked_client.room_id == Some(room_id) {
   371                     response.add(Kicked.send(id));
   355                     if let Some(mut room_control) = server.get_room_control(kicked_client.id) {
   372                     let result = server.leave_room(id);
   356                         response.add(Kicked.send(kicked_id));
   373                     get_room_leave_data(server, room_id, "kicked", result, response);
   357                         let result = room_control.leave_room();
       
   358                         get_room_leave_result(
       
   359                             room_control.server(),
       
   360                             room_control.room(),
       
   361                             "kicked",
       
   362                             result,
       
   363                             response,
       
   364                         );
       
   365                     }
   374                 }
   366                 }
   375             }
   367             }
   376         }
   368         }
   377         VoteType::Map(None) => (),
   369         VoteType::Map(None) => (),
   378         VoteType::Map(Some(name)) => {
   370         VoteType::Map(Some(name)) => {