rust/hedgewars-server/src/core/room.rs
changeset 15541 d122b65bdf6f
parent 15540 479911540e17
child 15552 0031683bfa76
equal deleted inserted replaced
15540:479911540e17 15541:d122b65bdf6f
    22         .filter(move |(id, _)| *id == client_id)
    22         .filter(move |(id, _)| *id == client_id)
    23         .map(|(_, t)| t)
    23         .map(|(_, t)| t)
    24 }
    24 }
    25 
    25 
    26 pub struct GameInfo {
    26 pub struct GameInfo {
    27     pub teams_in_game: u8,
    27     pub original_teams: Vec<(ClientId, TeamInfo)>,
    28     pub teams_at_start: Vec<(ClientId, TeamInfo)>,
       
    29     pub left_teams: Vec<String>,
    28     pub left_teams: Vec<String>,
    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     config: RoomConfig,
    32     original_config: RoomConfig,
    34 }
    33 }
    35 
    34 
    36 impl GameInfo {
    35 impl GameInfo {
    37     fn new(teams: Vec<(ClientId, TeamInfo)>, config: RoomConfig) -> GameInfo {
    36     fn new(teams: Vec<(ClientId, TeamInfo)>, config: RoomConfig) -> GameInfo {
    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             teams_in_game: teams.len() as u8,
    42             original_teams: teams,
    44             teams_at_start: teams,
    43             original_config: config,
    45             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.teams_at_start, client_id)
    48         client_teams_impl(&self.original_teams, client_id)
    51     }
    49     }
    52 }
    50 }
    53 
    51 
    54 #[derive(Serialize, Deserialize)]
    52 #[derive(Serialize, Deserialize)]
    55 pub struct RoomSave {
    53 pub struct RoomSave {
   146         if let Some(index) = self.teams.iter().position(|(_, t)| t.name == team_name) {
   144         if let Some(index) = self.teams.iter().position(|(_, t)| t.name == team_name) {
   147             self.teams.remove(index);
   145             self.teams.remove(index);
   148 
   146 
   149             if let Some(info) = &mut self.game_info {
   147             if let Some(info) = &mut self.game_info {
   150                 info.left_teams.push(team_name.to_string());
   148                 info.left_teams.push(team_name.to_string());
   151                 info.teams_in_game -= 1;
       
   152 
   149 
   153                 if let Some(m) = &info.sync_msg {
   150                 if let Some(m) = &info.sync_msg {
   154                     info.msg_log.push(m.clone());
   151                     info.msg_log.push(m.clone());
   155                     info.sync_msg = None
   152                     info.sync_msg = None
   156                 }
   153                 }
   162     }
   159     }
   163 
   160 
   164     pub fn set_hedgehogs_number(&mut self, n: u8) -> Vec<String> {
   161     pub fn set_hedgehogs_number(&mut self, n: u8) -> Vec<String> {
   165         let mut names = Vec::new();
   162         let mut names = Vec::new();
   166         let teams = match self.game_info {
   163         let teams = match self.game_info {
   167             Some(ref mut info) => &mut info.teams_at_start,
   164             Some(ref mut info) => &mut info.original_teams,
   168             None => &mut self.teams,
   165             None => &mut self.teams,
   169         };
   166         };
   170 
   167 
   171         if teams.len() as u8 * n <= MAX_HEDGEHOGS_IN_ROOM {
   168         if teams.len() as u8 * n <= MAX_HEDGEHOGS_IN_ROOM {
   172             for (_, team) in teams.iter_mut() {
   169             for (_, team) in teams.iter_mut() {
   174                 names.push(team.name.clone())
   171                 names.push(team.name.clone())
   175             }
   172             }
   176             self.default_hedgehog_number = n;
   173             self.default_hedgehog_number = n;
   177         }
   174         }
   178         names
   175         names
       
   176     }
       
   177 
       
   178     pub fn teams_in_game(&self) -> Option<u8> {
       
   179         self.game_info
       
   180             .as_ref()
       
   181             .map(|info| (info.original_teams.len() - info.left_teams.len()) as u8)
   179     }
   182     }
   180 
   183 
   181     pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)>
   184     pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)>
   182     where
   185     where
   183         F: Fn(&TeamInfo) -> bool,
   186         F: Fn(&TeamInfo) -> bool,
   301         ]
   304         ]
   302     }
   305     }
   303 
   306 
   304     pub fn active_config(&self) -> &RoomConfig {
   307     pub fn active_config(&self) -> &RoomConfig {
   305         match self.game_info {
   308         match self.game_info {
   306             Some(ref info) => &info.config,
   309             Some(ref info) => &info.original_config,
   307             None => &self.config,
   310             None => &self.config,
   308         }
   311         }
   309     }
   312     }
   310 
   313 
   311     pub fn map_config(&self) -> Vec<String> {
   314     pub fn map_config(&self) -> Vec<String> {
   312         match self.game_info {
   315         match self.game_info {
   313             Some(ref info) => info.config.to_map_config(),
   316             Some(ref info) => info.original_config.to_map_config(),
   314             None => self.config.to_map_config(),
   317             None => self.config.to_map_config(),
   315         }
   318         }
   316     }
   319     }
   317 
   320 
   318     pub fn game_config(&self) -> Vec<GameCfg> {
   321     pub fn game_config(&self) -> Vec<GameCfg> {
   319         match self.game_info {
   322         match self.game_info {
   320             Some(ref info) => info.config.to_game_config(),
   323             Some(ref info) => info.original_config.to_game_config(),
   321             None => self.config.to_game_config(),
   324             None => self.config.to_game_config(),
   322         }
   325         }
   323     }
   326     }
   324 
   327 
   325     pub fn save_config(&mut self, name: String, location: String) {
   328     pub fn save_config(&mut self, name: String, location: String) {