gameServer2/src/server/room.rs
changeset 13427 6f6a866c86a2
parent 13426 f091f69d59e4
child 13428 c8425fbcf1d9
equal deleted inserted replaced
13426:f091f69d59e4 13427:6f6a866c86a2
     5 };
     5 };
     6 
     6 
     7 const MAX_HEDGEHOGS_IN_ROOM: u8 = 48;
     7 const MAX_HEDGEHOGS_IN_ROOM: u8 = 48;
     8 pub type RoomId = usize;
     8 pub type RoomId = usize;
     9 
     9 
       
    10 #[derive(Clone)]
    10 struct Ammo {
    11 struct Ammo {
    11     name: String,
    12     name: String,
    12     settings: Option<String>
    13     settings: Option<String>
    13 }
    14 }
    14 
    15 
       
    16 #[derive(Clone)]
    15 struct Scheme {
    17 struct Scheme {
    16     name: String,
    18     name: String,
    17     settings: Option<Vec<String>>
    19     settings: Option<Vec<String>>
    18 }
    20 }
    19 
    21 
       
    22 #[derive(Clone)]
    20 struct RoomConfig {
    23 struct RoomConfig {
    21     feature_size: u32,
    24     feature_size: u32,
    22     map_type: String,
    25     map_type: String,
    23     map_generator: u32,
    26     map_generator: u32,
    24     maze_size: u32,
    27     maze_size: u32,
    49             drawn_map: None
    52             drawn_map: None
    50         }
    53         }
    51     }
    54     }
    52 }
    55 }
    53 
    56 
       
    57 fn client_teams_impl(teams: &Vec<(ClientId, TeamInfo)>, client_id: ClientId)
       
    58     -> impl Iterator<Item = &TeamInfo> + Clone
       
    59 {
       
    60     teams.iter().filter(move |(id, _)| *id == client_id).map(|(_, t)| t)
       
    61 }
       
    62 
       
    63 fn map_config_from(c: &RoomConfig) -> Vec<String> {
       
    64     vec![c.feature_size.to_string(), c.map_type.to_string(),
       
    65          c.map_generator.to_string(), c.maze_size.to_string(),
       
    66          c.seed.to_string(), c.template.to_string()]
       
    67 }
       
    68 
       
    69 fn game_config_from(c: &RoomConfig) -> Vec<GameCfg> {
       
    70     use server::coretypes::GameCfg::*;
       
    71     let mut v = vec![
       
    72         Ammo(c.ammo.name.to_string(), c.ammo.settings.clone()),
       
    73         Scheme(c.scheme.name.to_string(), c.scheme.settings.clone()),
       
    74         Script(c.script.to_string()),
       
    75         Theme(c.theme.to_string())];
       
    76     if let Some(ref m) = c.drawn_map {
       
    77         v.push(DrawnMap(m.to_string()))
       
    78     }
       
    79     v
       
    80 }
       
    81 
    54 pub struct GameInfo {
    82 pub struct GameInfo {
    55     pub teams_in_game: u8,
    83     pub teams_in_game: u8,
    56     pub left_teams: Vec<String>
    84     pub teams_at_start: Vec<(ClientId, TeamInfo)>,
       
    85     pub left_teams: Vec<String>,
       
    86     pub msg_log: String,
       
    87     pub last_msg: Option<String>,
       
    88     pub is_paused: bool,
       
    89     config: RoomConfig
    57 }
    90 }
    58 
    91 
    59 impl GameInfo {
    92 impl GameInfo {
    60     pub fn new(teams_number: u8) -> GameInfo {
    93     fn new(teams: Vec<(ClientId, TeamInfo)>, config: RoomConfig) -> GameInfo {
    61         GameInfo {
    94         GameInfo {
    62             teams_in_game: teams_number,
    95             left_teams: Vec::new(),
    63             left_teams: Vec::new()
    96             msg_log: String::new(),
    64         }
    97             last_msg: None,
       
    98             is_paused: false,
       
    99             teams_in_game: teams.len() as u8,
       
   100             teams_at_start: teams,
       
   101             config
       
   102         }
       
   103     }
       
   104 
       
   105     pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> + Clone {
       
   106         client_teams_impl(&self.teams_at_start, client_id)
    65     }
   107     }
    66 }
   108 }
    67 
   109 
    68 pub struct HWRoom {
   110 pub struct HWRoom {
    69     pub id: RoomId,
   111     pub id: RoomId,
   136         where F: Fn(&TeamInfo) -> bool {
   178         where F: Fn(&TeamInfo) -> bool {
   137         self.teams.iter().map(|(_, t)| t).find(|t| f(*t))
   179         self.teams.iter().map(|(_, t)| t).find(|t| f(*t))
   138     }
   180     }
   139 
   181 
   140     pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> {
   182     pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> {
   141         self.teams.iter().filter(move |(id, _)| *id == client_id).map(|(_, t)| t)
   183         client_teams_impl(&self.teams, client_id)
   142     }
   184     }
   143 
   185 
   144     pub fn client_team_indices(&self, client_id: ClientId) -> Vec<u8> {
   186     pub fn client_team_indices(&self, client_id: ClientId) -> Vec<u8> {
   145         self.teams.iter().enumerate()
   187         self.teams.iter().enumerate()
   146             .filter(move |(_, (id, _))| *id == client_id)
   188             .filter(move |(_, (id, _))| *id == client_id)
   175             Scheme(n, s) => c.scheme = Scheme {name: n, settings: s},
   217             Scheme(n, s) => c.scheme = Scheme {name: n, settings: s},
   176             Script(s) => c.script = s,
   218             Script(s) => c.script = s,
   177             Theme(t) => c.theme = t,
   219             Theme(t) => c.theme = t,
   178             DrawnMap(m) => c.drawn_map = Some(m)
   220             DrawnMap(m) => c.drawn_map = Some(m)
   179         };
   221         };
       
   222     }
       
   223 
       
   224     pub fn start_round(&mut self) {
       
   225         if self.game_info.is_none() {
       
   226             self.game_info = Some(GameInfo::new(
       
   227                 self.teams.clone(), self.config.clone()));
       
   228         }
   180     }
   229     }
   181 
   230 
   182     pub fn info(&self, master: Option<&HWClient>) -> Vec<String> {
   231     pub fn info(&self, master: Option<&HWClient>) -> Vec<String> {
   183         let flags = "-".to_string();
   232         let flags = "-".to_string();
   184         let c = &self.config;
   233         let c = &self.config;
   194             c.ammo.name.to_string()
   243             c.ammo.name.to_string()
   195         ]
   244         ]
   196     }
   245     }
   197 
   246 
   198     pub fn map_config(&self) -> Vec<String> {
   247     pub fn map_config(&self) -> Vec<String> {
   199         let c = &self.config;
   248         match self.game_info {
   200         vec![c.feature_size.to_string(), c.map_type.to_string(),
   249             Some(ref info) => map_config_from(&info.config),
   201              c.map_generator.to_string(), c.maze_size.to_string(),
   250             None => map_config_from(&self.config)
   202              c.seed.to_string(), c.template.to_string()]
   251         }
   203     }
   252     }
   204 
   253 
   205     pub fn game_config(&self) -> Vec<GameCfg> {
   254     pub fn game_config(&self) -> Vec<GameCfg> {
   206         use server::coretypes::GameCfg::*;
   255         match self.game_info {
   207         let c = &self.config;
   256             Some(ref info) => game_config_from(&info.config),
   208         let mut v = vec![
   257             None => game_config_from(&self.config)
   209             Ammo(c.ammo.name.to_string(), c.ammo.settings.clone()),
   258         }
   210             Scheme(c.scheme.name.to_string(), c.scheme.settings.clone()),
       
   211             Script(c.script.to_string()),
       
   212             Theme(c.theme.to_string())];
       
   213         if let Some(ref m) = c.drawn_map {
       
   214             v.push(DrawnMap(m.to_string()))
       
   215         }
       
   216         v
       
   217     }
   259     }
   218 
   260 
   219     pub fn team_info(owner: &HWClient, team: &TeamInfo) -> Vec<String> {
   261     pub fn team_info(owner: &HWClient, team: &TeamInfo) -> Vec<String> {
   220         let mut info = vec![
   262         let mut info = vec![
   221             team.name.clone(),
   263             team.name.clone(),