rust/hedgewars-server/src/handlers/common.rs
changeset 15543 02f648f7cbe1
parent 15541 d122b65bdf6f
child 15544 77551d5e0a3b
equal deleted inserted replaced
15542:3b0b6ce76303 15543:02f648f7cbe1
    15         ProtocolFlags as Flags,
    15         ProtocolFlags as Flags,
    16     },
    16     },
    17     utils::to_engine_msg,
    17     utils::to_engine_msg,
    18 };
    18 };
    19 
    19 
    20 use super::Response;
    20 use super::{
       
    21     actions::{Destination, DestinationGroup},
       
    22     Response,
       
    23 };
    21 
    24 
    22 use crate::core::types::RoomConfig;
    25 use crate::core::types::RoomConfig;
    23 use rand::{self, seq::SliceRandom, thread_rng, Rng};
    26 use rand::{self, seq::SliceRandom, thread_rng, Rng};
    24 use std::{iter::once, mem::replace};
    27 use std::{iter::once, mem::replace};
    25 
    28 
   163                 ClientFlags(remove_flags(&[*flag]), replace(cleared_nicks, vec![])).send_self(),
   166                 ClientFlags(remove_flags(&[*flag]), replace(cleared_nicks, vec![])).send_self(),
   164             );
   167             );
   165         }
   168         }
   166     }
   169     }
   167 
   170 
   168     get_room_teams(room, client.id, response);
   171     get_room_teams(room, Destination::ToSelf, response);
   169     get_room_config(room, client.id, response);
   172     get_room_config(room, Destination::ToSelf, response);
   170 
   173 
   171     if !room.greeting.is_empty() {
   174     if !room.greeting.is_empty() {
   172         response.add(
   175         response.add(
   173             ChatMsg {
   176             ChatMsg {
   174                 nick: "[greeting]".to_string(),
   177                 nick: "[greeting]".to_string(),
   339 ) {
   342 ) {
   340     let update_msg = RoomUpdated(room_name.unwrap_or(room.name.clone()), room.info(master));
   343     let update_msg = RoomUpdated(room_name.unwrap_or(room.name.clone()), room.info(master));
   341     response.add(update_msg.send_all().with_protocol(room.protocol_number));
   344     response.add(update_msg.send_all().with_protocol(room.protocol_number));
   342 }
   345 }
   343 
   346 
   344 pub fn get_room_config_impl(config: &RoomConfig, to_client: ClientId, response: &mut Response) {
   347 pub fn get_room_config_impl(
   345     response.add(ConfigEntry("FULLMAPCONFIG".to_string(), config.to_map_config()).send(to_client));
   348     config: &RoomConfig,
       
   349     destination: Destination,
       
   350     response: &mut Response,
       
   351 ) {
       
   352     response.add(
       
   353         ConfigEntry("FULLMAPCONFIG".to_string(), config.to_map_config())
       
   354             .send_to_destination(destination.clone()),
       
   355     );
   346     for cfg in config.to_game_config() {
   356     for cfg in config.to_game_config() {
   347         response.add(cfg.to_server_msg().send(to_client));
   357         response.add(cfg.to_server_msg().send_to_destination(destination.clone()));
   348     }
   358     }
   349 }
   359 }
   350 
   360 
   351 pub fn get_room_config(room: &HwRoom, to_client: ClientId, response: &mut Response) {
   361 pub fn get_room_config(room: &HwRoom, destination: Destination, response: &mut Response) {
   352     get_room_config_impl(room.active_config(), to_client, response);
   362     get_room_config_impl(room.active_config(), destination, response);
   353 }
   363 }
   354 
   364 
   355 pub fn get_teams<'a, I>(teams: I, to_client: ClientId, response: &mut Response)
   365 pub fn get_teams<'a, I>(teams: I, destination: Destination, response: &mut Response)
   356 where
   366 where
   357     I: Iterator<Item = &'a TeamInfo>,
   367     I: Iterator<Item = &'a TeamInfo>,
   358 {
   368 {
   359     for team in teams {
   369     for team in teams {
   360         response.add(TeamAdd(team.to_protocol()).send(to_client));
   370         response.add(TeamAdd(team.to_protocol()).send_to_destination(destination.clone()));
   361         response.add(TeamColor(team.name.clone(), team.color).send(to_client));
   371         response
   362         response.add(HedgehogsNumber(team.name.clone(), team.hedgehogs_number).send(to_client));
   372             .add(TeamColor(team.name.clone(), team.color).send_to_destination(destination.clone()));
   363     }
   373         response.add(
   364 }
   374             HedgehogsNumber(team.name.clone(), team.hedgehogs_number)
   365 
   375                 .send_to_destination(destination.clone()),
   366 pub fn get_room_teams(room: &HwRoom, to_client: ClientId, response: &mut Response) {
   376         );
       
   377     }
       
   378 }
       
   379 
       
   380 pub fn get_room_teams(room: &HwRoom, destination: Destination, response: &mut Response) {
   367     let current_teams = match room.game_info {
   381     let current_teams = match room.game_info {
   368         Some(ref info) => &info.original_teams,
   382         Some(ref info) => &info.original_teams,
   369         None => &room.teams,
   383         None => &room.teams,
   370     };
   384     };
   371 
   385 
   372     get_teams(current_teams.iter().map(|(_, t)| t), to_client, response);
   386     get_teams(current_teams.iter().map(|(_, t)| t), destination, response);
   373 }
   387 }
   374 
   388 
   375 pub fn get_room_flags(
   389 pub fn get_room_flags(
   376     server: &HwServer,
   390     server: &HwServer,
   377     room_id: RoomId,
   391     room_id: RoomId,
   378     to_client: ClientId,
   392     destination: Destination,
   379     response: &mut Response,
   393     response: &mut Response,
   380 ) {
   394 ) {
   381     let room = server.room(room_id);
   395     let room = server.room(room_id);
   382     if let Some(id) = room.master_id {
   396     if let Some(id) = room.master_id {
   383         response.add(
   397         response.add(
   384             ClientFlags(
   398             ClientFlags(
   385                 add_flags(&[Flags::RoomMaster]),
   399                 add_flags(&[Flags::RoomMaster]),
   386                 vec![server.client(id).nick.clone()],
   400                 vec![server.client(id).nick.clone()],
   387             )
   401             )
   388             .send(to_client),
   402             .send_to_destination(destination.clone()),
   389         );
   403         );
   390     }
   404     }
   391     let nicks = server.collect_nicks(|(_, c)| c.room_id == Some(room_id) && c.is_ready());
   405     let nicks = server.collect_nicks(|(_, c)| c.room_id == Some(room_id) && c.is_ready());
   392 
   406 
   393     if !nicks.is_empty() {
   407     if !nicks.is_empty() {
   394         response.add(ClientFlags(add_flags(&[Flags::Ready]), nicks).send(to_client));
   408         response
       
   409             .add(ClientFlags(add_flags(&[Flags::Ready]), nicks).send_to_destination(destination));
   395     }
   410     }
   396 }
   411 }
   397 
   412 
   398 pub fn check_vote(
   413 pub fn check_vote(
   399     server: &HwServer,
   414     server: &HwServer,
   509 
   524 
   510                     let room_master = room.master_id.map(|id| room_control.server().client(id));
   525                     let room_master = room.master_id.map(|id| room_control.server().client(id));
   511 
   526 
   512                     super::common::get_room_update(None, room, room_master, response);
   527                     super::common::get_room_update(None, room, room_master, response);
   513 
   528 
   514                     for client_id in room_control.server().room_client_ids(room.id) {
   529                     let room_destination = Destination::ToAll {
   515                         super::common::get_room_config(room, client_id, response);
   530                         group: DestinationGroup::Room(room.id),
   516                     }
   531                         skip_self: false,
       
   532                     };
       
   533                     super::common::get_room_config(room, room_destination, response);
   517                 }
   534                 }
   518             }
   535             }
   519             VoteType::Pause => {
   536             VoteType::Pause => {
   520                 if room_control.toggle_pause() {
   537                 if room_control.toggle_pause() {
   521                     response.add(
   538                     response.add(
   593             .iter()
   610             .iter()
   594             .map(|name| TeamRemove(name.clone()).send_all().in_room(room.id)),
   611             .map(|name| TeamRemove(name.clone()).send_all().in_room(room.id)),
   595     );
   612     );
   596 
   613 
   597     for client_id in result.joined_mid_game_clients {
   614     for client_id in result.joined_mid_game_clients {
   598         super::common::get_room_config(room, client_id, response);
   615         super::common::get_room_config(room, Destination::ToId(client_id), response);
   599     }
   616     }
   600 
   617 
   601     if !result.unreadied_nicks.is_empty() {
   618     if !result.unreadied_nicks.is_empty() {
   602         response.add(
   619         response.add(
   603             ClientFlags(remove_flags(&[Flags::Ready]), result.unreadied_nicks)
   620             ClientFlags(remove_flags(&[Flags::Ready]), result.unreadied_nicks)