rust/hedgewars-server/src/core/room.rs
changeset 15569 4b2f3228f13b
parent 15553 852682c2003f
child 15804 747278149393
equal deleted inserted replaced
15568:de1b31c2d2f2 15569:4b2f3228f13b
    24 }
    24 }
    25 
    25 
    26 pub struct GameInfo {
    26 pub struct GameInfo {
    27     pub original_teams: Vec<(ClientId, TeamInfo)>,
    27     pub original_teams: Vec<(ClientId, TeamInfo)>,
    28     pub left_teams: Vec<String>,
    28     pub left_teams: Vec<String>,
    29     pub ingame_teams_count: u8,
       
    30     pub msg_log: Vec<String>,
    29     pub msg_log: Vec<String>,
    31     pub sync_msg: Option<String>,
    30     pub sync_msg: Option<String>,
    32     pub is_paused: bool,
    31     pub is_paused: bool,
    33     original_config: RoomConfig,
    32     original_config: RoomConfig,
    34 }
    33 }
    38         GameInfo {
    37         GameInfo {
    39             left_teams: Vec::new(),
    38             left_teams: Vec::new(),
    40             msg_log: Vec::new(),
    39             msg_log: Vec::new(),
    41             sync_msg: None,
    40             sync_msg: None,
    42             is_paused: false,
    41             is_paused: false,
    43             ingame_teams_count: teams.len() as u8,
       
    44             original_teams: teams,
    42             original_teams: teams,
    45             original_config: config,
    43             original_config: config,
    46         }
    44         }
    47     }
    45     }
    48 
    46 
    49     pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> + Clone {
    47     pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> + Clone {
    50         client_teams_impl(&self.original_teams, client_id)
    48         client_teams_impl(&self.original_teams, client_id)
       
    49     }
       
    50 
       
    51     pub fn mark_left_teams<'a, I>(&mut self, team_names: I)
       
    52     where
       
    53         I: Iterator<Item = &'a String>,
       
    54     {
       
    55         if let Some(m) = &self.sync_msg {
       
    56             self.msg_log.push(m.clone());
       
    57             self.sync_msg = None
       
    58         }
       
    59 
       
    60         for team_name in team_names {
       
    61             self.left_teams.push(team_name.clone());
       
    62 
       
    63             let remove_msg = crate::utils::to_engine_msg(iter::once(b'F').chain(team_name.bytes()));
       
    64             self.msg_log.push(remove_msg);
       
    65         }
    51     }
    66     }
    52 }
    67 }
    53 
    68 
    54 #[derive(Serialize, Deserialize)]
    69 #[derive(Serialize, Deserialize)]
    55 pub struct RoomSave {
    70 pub struct RoomSave {
   143     }
   158     }
   144 
   159 
   145     pub fn remove_team(&mut self, team_name: &str) {
   160     pub fn remove_team(&mut self, team_name: &str) {
   146         if let Some(index) = self.teams.iter().position(|(_, t)| t.name == team_name) {
   161         if let Some(index) = self.teams.iter().position(|(_, t)| t.name == team_name) {
   147             self.teams.remove(index);
   162             self.teams.remove(index);
   148 
       
   149             if let Some(info) = &mut self.game_info {
       
   150                 info.ingame_teams_count -= 1;
       
   151                 info.left_teams.push(team_name.to_string());
       
   152 
       
   153                 if let Some(m) = &info.sync_msg {
       
   154                     info.msg_log.push(m.clone());
       
   155                     info.sync_msg = None
       
   156                 }
       
   157                 let remove_msg =
       
   158                     crate::utils::to_engine_msg(iter::once(b'F').chain(team_name.bytes()));
       
   159                 info.msg_log.push(remove_msg.clone());
       
   160             }
       
   161         }
   163         }
   162     }
   164     }
   163 
   165 
   164     pub fn set_hedgehogs_number(&mut self, n: u8) -> Vec<String> {
   166     pub fn set_hedgehogs_number(&mut self, n: u8) -> Vec<String> {
   165         let mut names = Vec::new();
   167         let mut names = Vec::new();
   174         }
   176         }
   175         names
   177         names
   176     }
   178     }
   177 
   179 
   178     pub fn teams_in_game(&self) -> Option<u8> {
   180     pub fn teams_in_game(&self) -> Option<u8> {
   179         self.game_info.as_ref().map(|info| info.ingame_teams_count)
   181         self.game_info
       
   182             .as_ref()
       
   183             .map(|info| (info.original_teams.len() - info.left_teams.len()) as u8)
   180     }
   184     }
   181 
   185 
   182     pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)>
   186     pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)>
   183     where
   187     where
   184         F: Fn(&TeamInfo) -> bool,
   188         F: Fn(&TeamInfo) -> bool,
   300             c.scheme.name.to_string(),
   304             c.scheme.name.to_string(),
   301             c.ammo.name.to_string(),
   305             c.ammo.name.to_string(),
   302         ]
   306         ]
   303     }
   307     }
   304 
   308 
       
   309     pub fn config(&self) -> &RoomConfig {
       
   310         &self.config
       
   311     }
       
   312 
   305     pub fn active_config(&self) -> &RoomConfig {
   313     pub fn active_config(&self) -> &RoomConfig {
   306         match self.game_info {
   314         match self.game_info {
   307             Some(ref info) => &info.original_config,
   315             Some(ref info) => &info.original_config,
   308             None => &self.config,
   316             None => &self.config,
   309         }
   317         }