gameServer2/src/server/room.rs
author alfadur
Fri, 20 Jul 2018 22:14:20 +0300
changeset 13528 c8b626b0a3ad
parent 13527 e3ae9eea0689
child 13529 662f7df89d06
permissions -rw-r--r--
Implement save/delete commands
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
     1
use std::{
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
     2
    iter, collections::HashMap
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
     3
};
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     4
use server::{
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
     5
    coretypes::{ClientId, RoomId, TeamInfo, GameCfg, GameCfg::*, Voting},
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
     6
    client::{HWClient}
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     7
};
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     8
13525
d126d9a646ac Keep hedgehog limit in sync
alfadur
parents: 13524
diff changeset
     9
const MAX_HEDGEHOGS_IN_ROOM: u8 = 64;
d126d9a646ac Keep hedgehog limit in sync
alfadur
parents: 13524
diff changeset
    10
const MAX_TEAMS_IN_ROOM: u8 = 8;
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
    11
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    12
#[derive(Clone)]
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    13
struct Ammo {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    14
    name: String,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    15
    settings: Option<String>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    16
}
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    17
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    18
#[derive(Clone)]
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    19
struct Scheme {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    20
    name: String,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    21
    settings: Option<Vec<String>>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    22
}
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    23
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    24
#[derive(Clone)]
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    25
struct RoomConfig {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    26
    feature_size: u32,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    27
    map_type: String,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    28
    map_generator: u32,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    29
    maze_size: u32,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    30
    seed: String,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    31
    template: u32,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    32
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    33
    ammo: Ammo,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    34
    scheme: Scheme,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    35
    script: String,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    36
    theme: String,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    37
    drawn_map: Option<String>
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    38
}
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    39
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    40
impl RoomConfig {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    41
    fn new() -> RoomConfig {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    42
        RoomConfig {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    43
            feature_size: 12,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    44
            map_type: "+rnd+".to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    45
            map_generator: 0,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    46
            maze_size: 0,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    47
            seed: "seed".to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    48
            template: 0,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    49
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    50
            ammo: Ammo {name: "Default".to_string(), settings: None },
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    51
            scheme: Scheme {name: "Default".to_string(), settings: None },
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    52
            script: "Normal".to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    53
            theme: "\u{1f994}".to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    54
            drawn_map: None
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    55
        }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    56
    }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    57
}
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
    58
13524
5359ff75da3a indulge clippy
alfadur
parents: 13523
diff changeset
    59
fn client_teams_impl(teams: &[(ClientId, TeamInfo)], client_id: ClientId)
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    60
    -> impl Iterator<Item = &TeamInfo> + Clone
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    61
{
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    62
    teams.iter().filter(move |(id, _)| *id == client_id).map(|(_, t)| t)
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    63
}
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    64
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    65
fn map_config_from(c: &RoomConfig) -> Vec<String> {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    66
    vec![c.feature_size.to_string(), c.map_type.to_string(),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    67
         c.map_generator.to_string(), c.maze_size.to_string(),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    68
         c.seed.to_string(), c.template.to_string()]
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    69
}
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    70
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    71
fn game_config_from(c: &RoomConfig) -> Vec<GameCfg> {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    72
    use server::coretypes::GameCfg::*;
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    73
    let mut v = vec![
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    74
        Ammo(c.ammo.name.to_string(), c.ammo.settings.clone()),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    75
        Scheme(c.scheme.name.to_string(), c.scheme.settings.clone()),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    76
        Script(c.script.to_string()),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    77
        Theme(c.theme.to_string())];
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    78
    if let Some(ref m) = c.drawn_map {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    79
        v.push(DrawnMap(m.to_string()))
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    80
    }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    81
    v
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    82
}
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    83
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    84
pub struct GameInfo {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13423
diff changeset
    85
    pub teams_in_game: u8,
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    86
    pub teams_at_start: Vec<(ClientId, TeamInfo)>,
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    87
    pub left_teams: Vec<String>,
13428
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13427
diff changeset
    88
    pub msg_log: Vec<String>,
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13428
diff changeset
    89
    pub sync_msg: Option<String>,
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    90
    pub is_paused: bool,
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    91
    config: RoomConfig
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13423
diff changeset
    92
}
f091f69d59e4 Additional round cleanup
alfadur
parents: 13423
diff changeset
    93
f091f69d59e4 Additional round cleanup
alfadur
parents: 13423
diff changeset
    94
impl GameInfo {
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    95
    fn new(teams: Vec<(ClientId, TeamInfo)>, config: RoomConfig) -> GameInfo {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13423
diff changeset
    96
        GameInfo {
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
    97
            left_teams: Vec::new(),
13428
c8425fbcf1d9 Fix spectators joining midgame
alfadur
parents: 13427
diff changeset
    98
            msg_log: Vec::new(),
13443
2501428303a2 Fix team remove synchronization
alfadur
parents: 13428
diff changeset
    99
            sync_msg: None,
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   100
            is_paused: false,
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   101
            teams_in_game: teams.len() as u8,
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   102
            teams_at_start: teams,
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   103
            config
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13423
diff changeset
   104
        }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13423
diff changeset
   105
    }
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   106
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   107
    pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> + Clone {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   108
        client_teams_impl(&self.teams_at_start, client_id)
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   109
    }
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   110
}
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   111
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   112
pub struct RoomSave {
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   113
    pub location: String,
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   114
    config: RoomConfig
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   115
}
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   116
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   117
bitflags!{
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   118
    pub struct RoomFlags: u8 {
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   119
        const FIXED = 0b0000_0001;
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   120
        const RESTRICTED_JOIN = 0b0000_0010;
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   121
        const RESTRICTED_TEAM_ADD = 0b0000_0100;
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   122
        const RESTRICTED_UNREGISTERED_PLAYERS = 0b0000_1000;
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   123
    }
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   124
}
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   125
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   126
pub struct HWRoom {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   127
    pub id: RoomId,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   128
    pub master_id: Option<ClientId>,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   129
    pub name: String,
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   130
    pub password: Option<String>,
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   131
    pub greeting: String,
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13478
diff changeset
   132
    pub protocol_number: u16,
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   133
    pub flags: RoomFlags,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   134
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   135
    pub players_number: u8,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   136
    pub default_hedgehog_number: u8,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   137
    pub team_limit: u8,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   138
    pub ready_players_number: u8,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   139
    pub teams: Vec<(ClientId, TeamInfo)>,
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   140
    config: RoomConfig,
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   141
    pub voting: Option<Voting>,
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   142
    pub saves: HashMap<String, RoomSave>,
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   143
    pub game_info: Option<GameInfo>
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   144
}
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   145
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   146
impl HWRoom {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   147
    pub fn new(id: RoomId) -> HWRoom {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   148
        HWRoom {
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   149
            id,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   150
            master_id: None,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   151
            name: String::new(),
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   152
            password: None,
13477
f748a72432f2 Implement greetings & fixed rooms
alfadur
parents: 13443
diff changeset
   153
            greeting: "".to_string(),
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   154
            flags: RoomFlags::empty(),
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   155
            protocol_number: 0,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   156
            players_number: 0,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   157
            default_hedgehog_number: 4,
13525
d126d9a646ac Keep hedgehog limit in sync
alfadur
parents: 13524
diff changeset
   158
            team_limit: MAX_TEAMS_IN_ROOM,
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   159
            ready_players_number: 0,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   160
            teams: Vec::new(),
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   161
            config: RoomConfig::new(),
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   162
            voting: None,
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   163
            saves: HashMap::new(),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   164
            game_info: None
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   165
        }
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   166
    }
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   167
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   168
    pub fn hedgehogs_number(&self) -> u8 {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   169
        self.teams.iter().map(|(_, t)| t.hedgehogs_number).sum()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   170
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   171
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   172
    pub fn addable_hedgehogs(&self) -> u8 {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   173
        MAX_HEDGEHOGS_IN_ROOM - self.hedgehogs_number()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   174
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   175
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   176
    pub fn add_team(&mut self, owner_id: ClientId, mut team: TeamInfo) -> &TeamInfo {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   177
        team.color = iter::repeat(()).enumerate()
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   178
            .map(|(i, _)| i as u8).take(u8::max_value() as usize + 1)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   179
            .find(|i| self.teams.iter().all(|(_, t)| t.color != *i ))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   180
            .unwrap_or(0u8);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   181
        team.hedgehogs_number = if self.teams.is_empty() {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   182
            self.default_hedgehog_number
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   183
        } else {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   184
            self.teams[0].1.hedgehogs_number.min(self.addable_hedgehogs())
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   185
        };
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   186
        self.teams.push((owner_id, team));
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   187
        &self.teams.last().unwrap().1
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   188
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   189
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   190
    pub fn remove_team(&mut self, name: &str) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   191
        if let Some(index) = self.teams.iter().position(|(_, t)| t.name == name) {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   192
            self.teams.remove(index);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   193
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   194
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   195
13478
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   196
    pub fn set_hedgehogs_number(&mut self, n: u8) -> Vec<String> {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   197
        let mut names = Vec::new();
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   198
        let teams = match self.game_info {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   199
            Some(ref mut info) => &mut info.teams_at_start,
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   200
            None => &mut self.teams
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   201
        };
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   202
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   203
        if teams.len() as u8 * n <= MAX_HEDGEHOGS_IN_ROOM {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   204
            for (_, team) in teams.iter_mut() {
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   205
                team.hedgehogs_number = n;
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   206
                names.push(team.name.clone())
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   207
            };
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   208
            self.default_hedgehog_number = n;
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   209
        }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   210
        names
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   211
    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13477
diff changeset
   212
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   213
    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
   214
        where F: Fn(&TeamInfo) -> bool {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   215
        self.teams.iter_mut().find(|(_, t)| f(t)).map(|(id, t)| (*id, t))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   216
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   217
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   218
    pub fn find_team<F>(&self, f: F) -> Option<&TeamInfo>
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   219
        where F: Fn(&TeamInfo) -> bool {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   220
        self.teams.iter().map(|(_, t)| t).find(|t| f(*t))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   221
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   222
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   223
    pub fn client_teams(&self, client_id: ClientId) -> impl Iterator<Item = &TeamInfo> {
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   224
        client_teams_impl(&self.teams, client_id)
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   225
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   226
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   227
    pub fn client_team_indices(&self, client_id: ClientId) -> Vec<u8> {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   228
        self.teams.iter().enumerate()
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   229
            .filter(move |(_, (id, _))| *id == client_id)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   230
            .map(|(i, _)| i as u8).collect()
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   231
    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   232
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   233
    pub fn find_team_owner(&self, team_name: &str) -> Option<(ClientId, &str)> {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   234
        self.teams.iter().find(|(_, t)| t.name == team_name)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   235
            .map(|(id, t)| (*id, &t.name[..]))
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   236
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   237
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   238
    pub fn find_team_color(&self, owner_id: ClientId) -> Option<u8> {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   239
        self.client_teams(owner_id).nth(0).map(|t| t.color)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   240
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   241
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   242
    pub fn has_multiple_clans(&self) -> bool {
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   243
        self.teams.iter().min_by_key(|(_, t)| t.color) !=
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   244
            self.teams.iter().max_by_key(|(_, t)| t.color)
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   245
    }
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
   246
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   247
    pub fn set_config(&mut self, cfg: GameCfg) {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   248
        let c = &mut self.config;
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   249
        match cfg {
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   250
            FeatureSize(s) => c.feature_size = s,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   251
            MapType(t) => c.map_type = t,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   252
            MapGenerator(g) => c.map_generator = g,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   253
            MazeSize(s) => c.maze_size = s,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   254
            Seed(s) => c.seed = s,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   255
            Template(t) => c.template = t,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   256
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   257
            Ammo(n, s) => c.ammo = Ammo {name: n, settings: s},
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   258
            Scheme(n, s) => c.scheme = Scheme {name: n, settings: s},
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   259
            Script(s) => c.script = s,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   260
            Theme(t) => c.theme = t,
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   261
            DrawnMap(m) => c.drawn_map = Some(m)
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   262
        };
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   263
    }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   264
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   265
    pub fn start_round(&mut self) {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   266
        if self.game_info.is_none() {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   267
            self.game_info = Some(GameInfo::new(
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   268
                self.teams.clone(), self.config.clone()));
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   269
        }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   270
    }
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   271
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   272
    pub fn is_fixed(&self) -> bool {
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   273
        self.flags.contains(RoomFlags::FIXED)
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   274
    }
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   275
    pub fn is_join_restricted(&self) -> bool {
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   276
        self.flags.contains(RoomFlags::RESTRICTED_JOIN)
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   277
    }
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   278
    pub fn is_team_add_restricted(&self) -> bool {
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   279
        self.flags.contains(RoomFlags::RESTRICTED_TEAM_ADD)
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   280
    }
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   281
    pub fn are_unregistered_players_restricted(&self) -> bool {
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   282
        self.flags.contains(RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS)
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   283
    }
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   284
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   285
    pub fn set_is_fixed(&mut self, value: bool) {
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   286
        self.flags.set(RoomFlags::FIXED, value)
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   287
    }
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   288
    pub fn set_join_restriction(&mut self, value: bool) {
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   289
        self.flags.set(RoomFlags::RESTRICTED_JOIN, value)
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   290
    }
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   291
    pub fn set_team_add_restriction(&mut self, value: bool) {
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   292
        self.flags.set(RoomFlags::RESTRICTED_TEAM_ADD, value)
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   293
    }
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   294
    pub fn set_unregistered_players_restriction(&mut self, value: bool) {
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   295
        self.flags.set(RoomFlags::RESTRICTED_UNREGISTERED_PLAYERS, value)
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   296
    }
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   297
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   298
    fn flags_string(&self) -> String {
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   299
        let mut result = "-".to_string();
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   300
        if self.game_info.is_some()  { result += "g" }
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   301
        if self.password.is_some()   { result += "p" }
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   302
        if self.is_join_restricted() { result += "j" }
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   303
        if self.are_unregistered_players_restricted() {
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   304
            result += "r"
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   305
        }
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   306
        result
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   307
    }
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   308
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   309
    pub fn info(&self, master: Option<&HWClient>) -> Vec<String> {
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   310
        let c = &self.config;
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   311
        vec![
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
   312
            self.flags_string(),
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   313
            self.name.clone(),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   314
            self.players_number.to_string(),
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   315
            self.teams.len().to_string(),
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   316
            master.map_or("[]", |c| &c.nick).to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   317
            c.map_type.to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   318
            c.script.to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   319
            c.scheme.name.to_string(),
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   320
            c.ammo.name.to_string()
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   321
        ]
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   322
    }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   323
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   324
    pub fn map_config(&self) -> Vec<String> {
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   325
        match self.game_info {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   326
            Some(ref info) => map_config_from(&info.config),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   327
            None => map_config_from(&self.config)
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   328
        }
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   329
    }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   330
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   331
    pub fn game_config(&self) -> Vec<GameCfg> {
13427
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   332
        match self.game_info {
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   333
            Some(ref info) => game_config_from(&info.config),
6f6a866c86a2 Send more data on room joining
alfadur
parents: 13426
diff changeset
   334
            None => game_config_from(&self.config)
13422
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   335
        }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   336
    }
5fb27f94fc3b Implement game config messages
alfadur
parents: 13419
diff changeset
   337
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   338
    pub fn save_config(&mut self, name: String, location: String) {
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   339
        self.saves.insert(name, RoomSave { location, config: self.config.clone() });
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   340
    }
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   341
13527
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   342
    pub fn load_config(&mut self, name: &str) -> Option<&str> {
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   343
        if let Some(save) = self.saves.get(name) {
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   344
            self.config = save.config.clone();
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   345
            Some(&save.location[..])
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   346
        } else {
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   347
            None
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   348
        }
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   349
    }
e3ae9eea0689 Implement map voting
alfadur
parents: 13525
diff changeset
   350
13528
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   351
    pub fn delete_config(&mut self, name: &str) -> bool {
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   352
        self.saves.remove(name).is_some()
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   353
    }
c8b626b0a3ad Implement save/delete commands
alfadur
parents: 13527
diff changeset
   354
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   355
    pub fn team_info(owner: &HWClient, team: &TeamInfo) -> Vec<String> {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   356
        let mut info = vec![
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   357
            team.name.clone(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   358
            team.grave.clone(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   359
            team.fort.clone(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   360
            team.voice_pack.clone(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   361
            team.flag.clone(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   362
            owner.nick.clone(),
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   363
            team.difficulty.to_string()];
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   364
        let hogs = team.hedgehogs.iter().flat_map(|h|
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   365
            iter::once(h.name.clone()).chain(iter::once(h.hat.clone())));
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   366
        info.extend(hogs);
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   367
        info
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   368
    }
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents:
diff changeset
   369
}