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