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