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