gameServer2/src/server/room.rs
changeset 13419 81e0ed105f5d
parent 13416 cdf69667593b
child 13422 5fb27f94fc3b
equal deleted inserted replaced
13418:bb24c3414b0d 13419:81e0ed105f5d
       
     1 use std::iter;
     1 use server::{
     2 use server::{
     2     coretypes::TeamInfo,
     3     coretypes::{TeamInfo, GameCfg},
     3     client::{ClientId, HWClient}
     4     client::{ClientId, HWClient}
     4 };
     5 };
     5 
     6 
       
     7 const MAX_HEDGEHOGS_IN_ROOM: u8 = 48;
     6 pub type RoomId = usize;
     8 pub type RoomId = usize;
     7 
     9 
     8 pub struct HWRoom {
    10 pub struct HWRoom {
     9     pub id: RoomId,
    11     pub id: RoomId,
    10     pub master_id: Option<ClientId>,
    12     pub master_id: Option<ClientId>,
    11     pub name: String,
    13     pub name: String,
    12     pub password: Option<String>,
    14     pub password: Option<String>,
    13     pub protocol_number: u32,
    15     pub protocol_number: u32,
    14 
    16 
    15     pub players_number: u32,
    17     pub players_number: u32,
       
    18     pub default_hedgehog_number: u8,
       
    19     pub team_limit: u8,
    16     pub ready_players_number: u8,
    20     pub ready_players_number: u8,
    17     pub teams: Vec<TeamInfo>,
    21     pub teams: Vec<(ClientId, TeamInfo)>,
       
    22     pub game_info: Option<()>
    18 }
    23 }
    19 
    24 
    20 impl HWRoom {
    25 impl HWRoom {
    21     pub fn new(id: RoomId) -> HWRoom {
    26     pub fn new(id: RoomId) -> HWRoom {
    22         HWRoom {
    27         HWRoom {
    24             master_id: None,
    29             master_id: None,
    25             name: String::new(),
    30             name: String::new(),
    26             password: None,
    31             password: None,
    27             protocol_number: 0,
    32             protocol_number: 0,
    28             players_number: 0,
    33             players_number: 0,
       
    34             default_hedgehog_number: 4,
       
    35             team_limit: 8,
    29             ready_players_number: 0,
    36             ready_players_number: 0,
    30             teams: Vec::new()
    37             teams: Vec::new(),
       
    38             game_info: None
    31         }
    39         }
       
    40     }
       
    41 
       
    42     pub fn hedgehogs_number(&self) -> u8 {
       
    43         self.teams.iter().map(|(_, t)| t.hedgehogs_number).sum()
       
    44     }
       
    45 
       
    46     pub fn addable_hedgehogs(&self) -> u8 {
       
    47         MAX_HEDGEHOGS_IN_ROOM - self.hedgehogs_number()
       
    48     }
       
    49 
       
    50     pub fn add_team(&mut self, owner_id: ClientId, mut team: TeamInfo) -> &TeamInfo {
       
    51         team.color = iter::repeat(()).enumerate()
       
    52             .map(|(i, _)| i as u8).take(u8::max_value() as usize + 1)
       
    53             .find(|i| self.teams.iter().all(|(_, t)| t.color != *i ))
       
    54             .unwrap_or(0u8);
       
    55         team.hedgehogs_number = if self.teams.is_empty() {
       
    56             self.default_hedgehog_number
       
    57         } else {
       
    58             self.teams[0].1.hedgehogs_number.min(self.addable_hedgehogs())
       
    59         };
       
    60         self.teams.push((owner_id, team));
       
    61         &self.teams.last().unwrap().1
       
    62     }
       
    63 
       
    64     pub fn remove_team(&mut self, name: &str) {
       
    65         if let Some(index) = self.teams.iter().position(|(_, t)| t.name == name) {
       
    66             self.teams.remove(index);
       
    67         }
       
    68     }
       
    69 
       
    70     pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)>
       
    71         where F: Fn(&TeamInfo) -> bool {
       
    72         self.teams.iter_mut().find(|(_, t)| f(t)).map(|(id, t)| (*id, t))
       
    73     }
       
    74 
       
    75     pub fn find_team<F>(&self, f: F) -> Option<&TeamInfo>
       
    76         where F: Fn(&TeamInfo) -> bool {
       
    77         self.teams.iter().map(|(_, t)| t).find(|t| f(*t))
       
    78     }
       
    79 
       
    80     pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> {
       
    81         self.teams.iter().filter(move |(id, _)| *id == client_id).map(|(_, t)| t)
       
    82     }
       
    83 
       
    84     pub fn find_team_owner(&self, team_name: &str) -> Option<(ClientId, &str)> {
       
    85         self.teams.iter().find(|(_, t)| t.name == team_name)
       
    86             .map(|(id, t)| (*id, &t.name[..]))
       
    87     }
       
    88 
       
    89     pub fn find_team_color(&self, owner_id: ClientId) -> Option<u8> {
       
    90         self.client_teams(owner_id).nth(0).map(|t| t.color)
    32     }
    91     }
    33 
    92 
    34     pub fn info(&self, master: Option<&HWClient>) -> Vec<String> {
    93     pub fn info(&self, master: Option<&HWClient>) -> Vec<String> {
    35         let flags = "-".to_string();
    94         let flags = "-".to_string();
    36         vec![
    95         vec![
    37             flags,
    96             flags,
    38             self.name.clone(),
    97             self.name.clone(),
    39             self.players_number.to_string(),
    98             self.players_number.to_string(),
    40             self.teams.len().to_string(),
    99             self.teams.len().to_string(),
    41             master.map_or("?", |c| &c.nick).to_string(),
   100             master.map_or("?", |c| &c.nick).to_string(),
    42             "Default".to_string(),
   101             "Normal".to_string(),
    43             "Default".to_string(),
   102             "Default".to_string(),
    44             "Default".to_string(),
   103             "Default".to_string(),
    45             "Default".to_string(),
   104             "Default".to_string(),
    46         ]
   105         ]
    47     }
   106     }
       
   107 
       
   108     pub fn team_info(owner: &HWClient, team: &TeamInfo) -> Vec<String> {
       
   109         let mut info = vec![
       
   110             team.name.clone(),
       
   111             team.grave.clone(),
       
   112             team.fort.clone(),
       
   113             team.voice_pack.clone(),
       
   114             team.flag.clone(),
       
   115             owner.nick.clone(),
       
   116             team.difficulty.to_string()];
       
   117         let hogs = team.hedgehogs.iter().flat_map(|h|
       
   118             iter::once(h.name.clone()).chain(iter::once(h.hat.clone())));
       
   119         info.extend(hogs);
       
   120         info
       
   121     }
    48 }
   122 }