gameServer2/src/server/room.rs
author alfadur
Wed, 04 Jul 2018 18:49:51 +0300
changeset 13448 2501428303a2
parent 13433 c8425fbcf1d9
child 13482 f748a72432f2
permissions -rw-r--r--
Fix team remove synchronization
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
     1
use std::{iter};
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     2
use server::{
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
     3
    coretypes::{TeamInfo, GameCfg, GameCfg::*},
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     4
    client::{ClientId, HWClient}
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     5
};
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     6
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
     7
const MAX_HEDGEHOGS_IN_ROOM: u8 = 48;
13124
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
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    10
#[derive(Clone)]
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    11
struct Ammo {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    12
    name: String,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    13
    settings: Option<String>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    14
}
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    15
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    16
#[derive(Clone)]
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    17
struct Scheme {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    18
    name: String,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    19
    settings: Option<Vec<String>>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    20
}
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    21
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    22
#[derive(Clone)]
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    23
struct RoomConfig {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    24
    feature_size: u32,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    25
    map_type: String,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    26
    map_generator: u32,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    27
    maze_size: u32,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    28
    seed: String,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    29
    template: u32,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    30
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    31
    ammo: Ammo,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    32
    scheme: Scheme,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    33
    script: String,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    34
    theme: String,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    35
    drawn_map: Option<String>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    36
}
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    37
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    38
impl RoomConfig {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    39
    fn new() -> RoomConfig {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    40
        RoomConfig {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    41
            feature_size: 12,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    42
            map_type: "+rnd+".to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    43
            map_generator: 0,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    44
            maze_size: 0,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    45
            seed: "seed".to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    46
            template: 0,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    47
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    48
            ammo: Ammo {name: "Default".to_string(), settings: None },
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    49
            scheme: Scheme {name: "Default".to_string(), settings: None },
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    50
            script: "Normal".to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    51
            theme: "\u{1f994}".to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    52
            drawn_map: None
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    53
        }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    54
    }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    55
}
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
    56
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    57
fn client_teams_impl(teams: &Vec<(ClientId, TeamInfo)>, client_id: ClientId)
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    58
    -> impl Iterator<Item = &TeamInfo> + Clone
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    59
{
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    60
    teams.iter().filter(move |(id, _)| *id == client_id).map(|(_, t)| t)
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    61
}
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    62
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    63
fn map_config_from(c: &RoomConfig) -> Vec<String> {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    64
    vec![c.feature_size.to_string(), c.map_type.to_string(),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    65
         c.map_generator.to_string(), c.maze_size.to_string(),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    66
         c.seed.to_string(), c.template.to_string()]
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    67
}
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    68
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    69
fn game_config_from(c: &RoomConfig) -> Vec<GameCfg> {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    70
    use server::coretypes::GameCfg::*;
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    71
    let mut v = vec![
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    72
        Ammo(c.ammo.name.to_string(), c.ammo.settings.clone()),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    73
        Scheme(c.scheme.name.to_string(), c.scheme.settings.clone()),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    74
        Script(c.script.to_string()),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    75
        Theme(c.theme.to_string())];
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    76
    if let Some(ref m) = c.drawn_map {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    77
        v.push(DrawnMap(m.to_string()))
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    78
    }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    79
    v
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    80
}
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    81
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
    82
pub struct GameInfo {
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13428
diff changeset
    83
    pub teams_in_game: u8,
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    84
    pub teams_at_start: Vec<(ClientId, TeamInfo)>,
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    85
    pub left_teams: Vec<String>,
13433
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13432
diff changeset
    86
    pub msg_log: Vec<String>,
13448
2501428303a2 Fix team remove synchronization
alfadur
parents: 13433
diff changeset
    87
    pub sync_msg: Option<String>,
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    88
    pub is_paused: bool,
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    89
    config: RoomConfig
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13428
diff changeset
    90
}
f091f69d59e4 Additional round cleanup
alfadur
parents: 13428
diff changeset
    91
f091f69d59e4 Additional round cleanup
alfadur
parents: 13428
diff changeset
    92
impl GameInfo {
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    93
    fn new(teams: Vec<(ClientId, TeamInfo)>, config: RoomConfig) -> GameInfo {
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13428
diff changeset
    94
        GameInfo {
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    95
            left_teams: Vec::new(),
13433
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13432
diff changeset
    96
            msg_log: Vec::new(),
13448
2501428303a2 Fix team remove synchronization
alfadur
parents: 13433
diff changeset
    97
            sync_msg: None,
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    98
            is_paused: false,
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
    99
            teams_in_game: teams.len() as u8,
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   100
            teams_at_start: teams,
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   101
            config
13431
f091f69d59e4 Additional round cleanup
alfadur
parents: 13428
diff changeset
   102
        }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13428
diff changeset
   103
    }
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   104
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   105
    pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> + Clone {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   106
        client_teams_impl(&self.teams_at_start, client_id)
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   107
    }
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   108
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   109
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   110
pub struct HWRoom {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   111
    pub id: RoomId,
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   112
    pub master_id: Option<ClientId>,
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   113
    pub name: String,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   114
    pub password: Option<String>,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   115
    pub protocol_number: u32,
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   116
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   117
    pub players_number: u32,
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   118
    pub default_hedgehog_number: u8,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   119
    pub team_limit: u8,
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   120
    pub ready_players_number: u8,
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   121
    pub teams: Vec<(ClientId, TeamInfo)>,
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   122
    config: RoomConfig,
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   123
    pub game_info: Option<GameInfo>
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   124
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   125
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   126
impl HWRoom {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   127
    pub fn new(id: RoomId) -> HWRoom {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   128
        HWRoom {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   129
            id,
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   130
            master_id: None,
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   131
            name: String::new(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   132
            password: None,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   133
            protocol_number: 0,
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   134
            players_number: 0,
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   135
            default_hedgehog_number: 4,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   136
            team_limit: 8,
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   137
            ready_players_number: 0,
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   138
            teams: Vec::new(),
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   139
            config: RoomConfig::new(),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   140
            game_info: None
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   141
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   142
    }
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   143
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   144
    pub fn hedgehogs_number(&self) -> u8 {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   145
        self.teams.iter().map(|(_, t)| t.hedgehogs_number).sum()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   146
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   147
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   148
    pub fn addable_hedgehogs(&self) -> u8 {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   149
        MAX_HEDGEHOGS_IN_ROOM - self.hedgehogs_number()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   150
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   151
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   152
    pub fn add_team(&mut self, owner_id: ClientId, mut team: TeamInfo) -> &TeamInfo {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   153
        team.color = iter::repeat(()).enumerate()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   154
            .map(|(i, _)| i as u8).take(u8::max_value() as usize + 1)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   155
            .find(|i| self.teams.iter().all(|(_, t)| t.color != *i ))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   156
            .unwrap_or(0u8);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   157
        team.hedgehogs_number = if self.teams.is_empty() {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   158
            self.default_hedgehog_number
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   159
        } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   160
            self.teams[0].1.hedgehogs_number.min(self.addable_hedgehogs())
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   161
        };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   162
        self.teams.push((owner_id, team));
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   163
        &self.teams.last().unwrap().1
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   164
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   165
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   166
    pub fn remove_team(&mut self, name: &str) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   167
        if let Some(index) = self.teams.iter().position(|(_, t)| t.name == name) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   168
            self.teams.remove(index);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   169
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   170
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   171
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   172
    pub fn find_team_and_owner_mut<F>(&mut self, f: F) -> Option<(ClientId, &mut TeamInfo)>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   173
        where F: Fn(&TeamInfo) -> bool {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   174
        self.teams.iter_mut().find(|(_, t)| f(t)).map(|(id, t)| (*id, t))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   175
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   176
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   177
    pub fn find_team<F>(&self, f: F) -> Option<&TeamInfo>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   178
        where F: Fn(&TeamInfo) -> bool {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   179
        self.teams.iter().map(|(_, t)| t).find(|t| f(*t))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   180
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   181
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   182
    pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> {
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   183
        client_teams_impl(&self.teams, client_id)
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   184
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   185
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   186
    pub fn client_team_indices(&self, client_id: ClientId) -> Vec<u8> {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   187
        self.teams.iter().enumerate()
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   188
            .filter(move |(_, (id, _))| *id == client_id)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   189
            .map(|(i, _)| i as u8).collect()
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   190
    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   191
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   192
    pub fn find_team_owner(&self, team_name: &str) -> Option<(ClientId, &str)> {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   193
        self.teams.iter().find(|(_, t)| t.name == team_name)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   194
            .map(|(id, t)| (*id, &t.name[..]))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   195
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   196
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   197
    pub fn find_team_color(&self, owner_id: ClientId) -> Option<u8> {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   198
        self.client_teams(owner_id).nth(0).map(|t| t.color)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   199
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   200
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   201
    pub fn has_multiple_clans(&self) -> bool {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   202
        self.teams.iter().min_by_key(|(_, t)| t.color) !=
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   203
            self.teams.iter().max_by_key(|(_, t)| t.color)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   204
    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13427
diff changeset
   205
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   206
    pub fn set_config(&mut self, cfg: GameCfg) {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   207
        let c = &mut self.config;
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   208
        match cfg {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   209
            FeatureSize(s) => c.feature_size = s,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   210
            MapType(t) => c.map_type = t,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   211
            MapGenerator(g) => c.map_generator = g,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   212
            MazeSize(s) => c.maze_size = s,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   213
            Seed(s) => c.seed = s,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   214
            Template(t) => c.template = t,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   215
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   216
            Ammo(n, s) => c.ammo = Ammo {name: n, settings: s},
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   217
            Scheme(n, s) => c.scheme = Scheme {name: n, settings: s},
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   218
            Script(s) => c.script = s,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   219
            Theme(t) => c.theme = t,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   220
            DrawnMap(m) => c.drawn_map = Some(m)
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   221
        };
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   222
    }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   223
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   224
    pub fn start_round(&mut self) {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   225
        if self.game_info.is_none() {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   226
            self.game_info = Some(GameInfo::new(
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   227
                self.teams.clone(), self.config.clone()));
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   228
        }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   229
    }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   230
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   231
    pub fn info(&self, master: Option<&HWClient>) -> Vec<String> {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   232
        let flags = "-".to_string();
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   233
        let c = &self.config;
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   234
        vec![
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   235
            flags,
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   236
            self.name.clone(),
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   237
            self.players_number.to_string(),
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   238
            self.teams.len().to_string(),
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   239
            master.map_or("[]", |c| &c.nick).to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   240
            c.map_type.to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   241
            c.script.to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   242
            c.scheme.name.to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   243
            c.ammo.name.to_string()
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   244
        ]
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   245
    }
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   246
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   247
    pub fn map_config(&self) -> Vec<String> {
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   248
        match self.game_info {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   249
            Some(ref info) => map_config_from(&info.config),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   250
            None => map_config_from(&self.config)
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   251
        }
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   252
    }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   253
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   254
    pub fn game_config(&self) -> Vec<GameCfg> {
13432
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   255
        match self.game_info {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   256
            Some(ref info) => game_config_from(&info.config),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13431
diff changeset
   257
            None => game_config_from(&self.config)
13427
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   258
        }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   259
    }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13424
diff changeset
   260
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   261
    pub fn team_info(owner: &HWClient, team: &TeamInfo) -> Vec<String> {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   262
        let mut info = vec![
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   263
            team.name.clone(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   264
            team.grave.clone(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   265
            team.fort.clone(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   266
            team.voice_pack.clone(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   267
            team.flag.clone(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   268
            owner.nick.clone(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   269
            team.difficulty.to_string()];
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   270
        let hogs = team.hedgehogs.iter().flat_map(|h|
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   271
            iter::once(h.name.clone()).chain(iter::once(h.hat.clone())));
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   272
        info.extend(hogs);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   273
        info
81e0ed105f5d implementation of team related messages
alfadur
parents: 13421
diff changeset
   274
    }
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   275
}