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