rust/hedgewars-server/src/core/server.rs
author alfadur <mail@none>
Mon, 23 Dec 2019 18:08:55 +0300
changeset 15542 fd3a20e9d095
parent 15541 b3157d218ae2
child 15544 4a0b06b03199
permissions -rw-r--r--
move the anteroom out of the server
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     1
use super::{
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15541
diff changeset
     2
    anteroom::HwAnteroomClient,
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
     3
    client::HwClient,
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14707
diff changeset
     4
    indexslab::IndexSlab,
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
     5
    room::HwRoom,
15541
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
     6
    types::{ClientId, RoomId, ServerVar, TeamInfo},
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     7
};
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
     8
use crate::{protocol::messages::HwProtocolMessage::Greeting, utils};
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14707
diff changeset
     9
15541
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    10
use bitflags::_core::hint::unreachable_unchecked;
14807
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
    11
use bitflags::*;
13810
0463a4221327 cleanup crate imports
alfadur
parents: 13771
diff changeset
    12
use log::*;
15539
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15538
diff changeset
    13
use slab::Slab;
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15541
diff changeset
    14
use std::{borrow::BorrowMut, collections::HashSet, iter, mem::replace};
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    15
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
    16
#[derive(Debug)]
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    17
pub enum CreateRoomError {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    18
    InvalidName,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    19
    AlreadyExists,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    20
}
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    21
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
    22
#[derive(Debug)]
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    23
pub enum JoinRoomError {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    24
    DoesntExist,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    25
    WrongProtocol,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    26
    Full,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    27
    Restricted,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    28
}
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    29
15540
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    30
#[derive(Debug)]
15504
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    31
pub enum LeaveRoomResult {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    32
    RoomRemoved,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    33
    RoomRemains {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    34
        is_empty: bool,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    35
        was_master: bool,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    36
        was_in_game: bool,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    37
        new_master: Option<ClientId>,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    38
        removed_teams: Vec<String>,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    39
    },
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    40
}
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    41
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    42
#[derive(Debug)]
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    43
pub enum LeaveRoomError {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    44
    NoRoom,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    45
}
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    46
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
    47
#[derive(Debug)]
15509
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    48
pub struct ChangeMasterResult {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    49
    pub old_master_id: Option<ClientId>,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    50
    pub new_master_id: ClientId,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    51
}
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    52
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    53
#[derive(Debug)]
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    54
pub enum ChangeMasterError {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    55
    NoAccess,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    56
    AlreadyMaster,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    57
    NoClient,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    58
    ClientNotInRoom,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    59
}
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    60
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    61
#[derive(Debug)]
15541
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    62
pub enum AddTeamError {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    63
    TooManyTeams,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    64
    TooManyHedgehogs,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    65
    TeamAlreadyExists,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    66
    GameInProgress,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    67
    Restricted,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    68
}
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    69
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    70
#[derive(Debug)]
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    71
pub enum RemoveTeamError {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    72
    NoTeam,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    73
    TeamNotOwned,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    74
}
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    75
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    76
#[derive(Debug)]
15540
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    77
pub enum ModifyTeamError {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    78
    NoTeam,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    79
    NotMaster,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    80
}
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    81
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    82
#[derive(Debug)]
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    83
pub enum ModifyRoomNameError {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    84
    AccessDenied,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    85
    InvalidName,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    86
    DuplicateName,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    87
}
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    88
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    89
#[derive(Debug)]
15514
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
    90
pub enum StartGameError {
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
    91
    NotEnoughClans,
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
    92
    NotEnoughTeams,
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
    93
    NotReady,
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
    94
    AlreadyInGame,
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
    95
}
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
    96
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
    97
#[derive(Debug)]
15538
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
    98
pub struct EndGameResult {
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
    99
    pub joined_mid_game_clients: Vec<ClientId>,
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   100
    pub left_teams: Vec<String>,
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   101
    pub unreadied_nicks: Vec<String>,
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   102
}
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   103
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   104
#[derive(Debug)]
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   105
pub struct UninitializedError();
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   106
#[derive(Debug)]
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   107
pub struct AccessError();
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   108
14804
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   109
pub struct ServerGreetings {
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   110
    pub for_latest_protocol: String,
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   111
    pub for_old_protocols: String,
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   112
}
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   113
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   114
impl ServerGreetings {
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   115
    fn new() -> Self {
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   116
        Self {
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   117
            for_latest_protocol: "\u{1f994} is watching".to_string(),
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   118
            for_old_protocols: "\u{1f994} is watching".to_string(),
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   119
        }
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   120
    }
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   121
}
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   122
14807
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   123
bitflags! {
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   124
    pub struct ServerFlags: u8 {
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   125
        const REGISTERED_ONLY = 0b0000_1000;
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   126
    }
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   127
}
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   128
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   129
pub struct HwServer {
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15541
diff changeset
   130
    clients: IndexSlab<HwClient>,
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   131
    pub rooms: Slab<HwRoom>,
14804
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   132
    pub latest_protocol: u16,
14807
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   133
    pub flags: ServerFlags,
14804
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   134
    pub greetings: ServerGreetings,
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   135
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   136
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   137
impl HwServer {
14801
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14800
diff changeset
   138
    pub fn new(clients_limit: usize, rooms_limit: usize) -> Self {
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
   139
        let rooms = Slab::with_capacity(rooms_limit);
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14707
diff changeset
   140
        let clients = IndexSlab::with_capacity(clients_limit);
14715
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14714
diff changeset
   141
        Self {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   142
            clients,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   143
            rooms,
14804
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   144
            greetings: ServerGreetings::new(),
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   145
            latest_protocol: 58,
14807
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   146
            flags: ServerFlags::empty(),
14715
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14714
diff changeset
   147
        }
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   148
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   149
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   150
    #[inline]
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   151
    pub fn client(&self, client_id: ClientId) -> &HwClient {
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   152
        &self.clients[client_id]
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   153
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   154
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   155
    #[inline]
15540
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   156
    fn client_mut(&mut self, client_id: ClientId) -> &mut HwClient {
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   157
        &mut self.clients[client_id]
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   158
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   159
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   160
    #[inline]
15541
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   161
    pub fn has_client(&self, client_id: ClientId) -> bool {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   162
        self.clients.contains(client_id)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   163
    }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   164
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   165
    #[inline]
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   166
    pub fn iter_clients(&self) -> impl Iterator<Item = &HwClient> {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   167
        self.clients.iter().map(|(_, c)| c)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   168
    }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   169
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   170
    #[inline]
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   171
    pub fn room(&self, room_id: RoomId) -> &HwRoom {
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   172
        &self.rooms[room_id]
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   173
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   174
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   175
    #[inline]
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   176
    pub fn room_mut(&mut self, room_id: RoomId) -> &mut HwRoom {
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   177
        &mut self.rooms[room_id]
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   178
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   179
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   180
    #[inline]
15504
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   181
    pub fn client_and_room(&self, client_id: ClientId, room_id: RoomId) -> (&HwClient, &HwRoom) {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   182
        (&self.clients[client_id], &self.rooms[room_id])
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   183
    }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   184
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   185
    #[inline]
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   186
    pub fn client_and_room_mut(
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   187
        &mut self,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   188
        client_id: ClientId,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   189
        room_id: RoomId,
15541
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   190
    ) -> (&HwClient, &mut HwRoom) {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   191
        (&self.clients[client_id], &mut self.rooms[room_id])
15504
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   192
    }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   193
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   194
    #[inline]
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   195
    pub fn is_admin(&self, client_id: ClientId) -> bool {
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   196
        self.clients
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   197
            .get(client_id)
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   198
            .map(|c| c.is_admin())
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   199
            .unwrap_or(false)
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   200
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   201
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15541
diff changeset
   202
    pub fn add_client(&mut self, client_id: ClientId, data: HwAnteroomClient) {
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14707
diff changeset
   203
        if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) {
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   204
            let mut client = HwClient::new(client_id, protocol.get(), nick);
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   205
            client.set_is_checker(data.is_checker);
15197
f6115638aa92 enable local admins for non-official servers
alfadur
parents: 15096
diff changeset
   206
            #[cfg(not(feature = "official-server"))]
f6115638aa92 enable local admins for non-official servers
alfadur
parents: 15096
diff changeset
   207
            client.set_is_admin(data.is_local_admin);
f6115638aa92 enable local admins for non-official servers
alfadur
parents: 15096
diff changeset
   208
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   209
            #[cfg(feature = "official-server")]
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   210
            {
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   211
                client.set_is_registered(info.is_registered);
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   212
                client.set_is_admin(info.is_admin);
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   213
                client.set_is_contributor(info.is_contributor);
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   214
            }
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   215
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14707
diff changeset
   216
            self.clients.insert(client_id, client);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
   217
        }
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   218
    }
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
   219
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
   220
    pub fn remove_client(&mut self, client_id: ClientId) {
14717
8a45c90f4580 fix client removal
alfadur
parents: 14716
diff changeset
   221
        self.clients.remove(client_id);
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
   222
    }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
   223
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   224
    pub fn get_greetings(&self, client: &HwClient) -> &str {
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   225
        if client.protocol_number < self.latest_protocol {
14804
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   226
            &self.greetings.for_old_protocols
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   227
        } else {
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   228
            &self.greetings.for_latest_protocol
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   229
        }
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   230
    }
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   231
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   232
    #[inline]
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   233
    pub fn get_client_nick(&self, client_id: ClientId) -> &str {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   234
        &self.clients[client_id].nick
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   235
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   236
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   237
    #[inline]
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   238
    pub fn create_room(
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   239
        &mut self,
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   240
        creator_id: ClientId,
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   241
        name: String,
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   242
        password: Option<String>,
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   243
    ) -> Result<(&HwClient, &HwRoom), CreateRoomError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   244
        use CreateRoomError::*;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   245
        if utils::is_name_illegal(&name) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   246
            Err(InvalidName)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   247
        } else if self.has_room(&name) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   248
            Err(AlreadyExists)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   249
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   250
            Ok(create_room(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   251
                &mut self.clients[creator_id],
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   252
                &mut self.rooms,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   253
                name,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   254
                password,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   255
            ))
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   256
        }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   257
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   258
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   259
    pub fn join_room(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   260
        &mut self,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   261
        client_id: ClientId,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   262
        room_id: RoomId,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   263
    ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   264
        use JoinRoomError::*;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   265
        let room = &mut self.rooms[room_id];
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   266
        let client = &mut self.clients[client_id];
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   267
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   268
        if client.protocol_number != room.protocol_number {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   269
            Err(WrongProtocol)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   270
        } else if room.is_join_restricted() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   271
            Err(Restricted)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   272
        } else if room.players_number == u8::max_value() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   273
            Err(Full)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   274
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   275
            move_to_room(client, room);
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   276
            let room_id = room.id;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   277
            Ok((
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   278
                &self.clients[client_id],
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   279
                &self.rooms[room_id],
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   280
                self.clients.iter().map(|(_, c)| c),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   281
            ))
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   282
        }
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   283
    }
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   284
14692
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14525
diff changeset
   285
    #[inline]
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   286
    pub fn join_room_by_name(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   287
        &mut self,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   288
        client_id: ClientId,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   289
        room_name: &str,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   290
    ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   291
        use JoinRoomError::*;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   292
        let room = self.rooms.iter().find(|(_, r)| r.name == room_name);
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   293
        if let Some((_, room)) = room {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   294
            let room_id = room.id;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   295
            self.join_room(client_id, room_id)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   296
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   297
            Err(DoesntExist)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   298
        }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   299
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   300
15504
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   301
    pub fn leave_room(&mut self, client_id: ClientId) -> Result<LeaveRoomResult, LeaveRoomError> {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   302
        let client = &mut self.clients[client_id];
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   303
        if let Some(room_id) = client.room_id {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   304
            let room = &mut self.rooms[room_id];
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   305
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   306
            room.players_number -= 1;
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   307
            client.room_id = None;
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   308
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   309
            let is_empty = room.players_number == 0;
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   310
            let is_fixed = room.is_fixed();
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   311
            let was_master = room.master_id == Some(client_id);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   312
            let was_in_game = client.is_in_game();
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   313
            let mut removed_teams = vec![];
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   314
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   315
            if is_empty && !is_fixed {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   316
                if client.is_ready() && room.ready_players_number > 0 {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   317
                    room.ready_players_number -= 1;
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   318
                }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   319
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   320
                removed_teams = room
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   321
                    .client_teams(client.id)
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   322
                    .map(|t| t.name.clone())
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   323
                    .collect();
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   324
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   325
                for team_name in &removed_teams {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   326
                    room.remove_team(team_name);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   327
                }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   328
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   329
                if client.is_master() && !is_fixed {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   330
                    client.set_is_master(false);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   331
                    room.master_id = None;
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   332
                }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   333
            }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   334
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   335
            client.set_is_ready(false);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   336
            client.set_is_in_game(false);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   337
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   338
            if !is_fixed {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   339
                if room.players_number == 0 {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   340
                    self.rooms.remove(room_id);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   341
                } else if room.master_id == None {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   342
                    let new_master_id = self.room_clients(room_id).next();
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   343
                    if let Some(new_master_id) = new_master_id {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   344
                        let room = &mut self.rooms[room_id];
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   345
                        room.master_id = Some(new_master_id);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   346
                        let new_master = &mut self.clients[new_master_id];
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   347
                        new_master.set_is_master(true);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   348
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   349
                        if room.protocol_number < 42 {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   350
                            room.name = new_master.nick.clone();
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   351
                        }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   352
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   353
                        room.set_join_restriction(false);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   354
                        room.set_team_add_restriction(false);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   355
                        room.set_unregistered_players_restriction(true);
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   356
                    }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   357
                }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   358
            }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   359
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   360
            if is_empty && !is_fixed {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   361
                Ok(LeaveRoomResult::RoomRemoved)
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   362
            } else {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   363
                Ok(LeaveRoomResult::RoomRemains {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   364
                    is_empty,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   365
                    was_master,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   366
                    was_in_game,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   367
                    new_master: self.rooms[room_id].master_id,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   368
                    removed_teams,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   369
                })
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   370
            }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   371
        } else {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   372
            Err(LeaveRoomError::NoRoom)
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   373
        }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   374
    }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   375
15509
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   376
    pub fn change_master(
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   377
        &mut self,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   378
        client_id: ClientId,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   379
        room_id: RoomId,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   380
        new_master_nick: String,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   381
    ) -> Result<ChangeMasterResult, ChangeMasterError> {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   382
        let client = &mut self.clients[client_id];
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   383
        let room = &mut self.rooms[room_id];
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   384
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   385
        if client.is_admin() || room.master_id == Some(client_id) {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   386
            let new_master_id = self
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   387
                .clients
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   388
                .iter()
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   389
                .find(|(_, c)| c.nick == new_master_nick)
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   390
                .map(|(id, _)| id);
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   391
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   392
            match new_master_id {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   393
                Some(new_master_id) if new_master_id == client_id => {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   394
                    Err(ChangeMasterError::AlreadyMaster)
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   395
                }
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   396
                Some(new_master_id) => {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   397
                    let new_master = &mut self.clients[new_master_id];
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   398
                    if new_master.room_id == Some(room_id) {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   399
                        self.clients[new_master_id].set_is_master(true);
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   400
                        let old_master_id = room.master_id;
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   401
                        if let Some(master_id) = old_master_id {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   402
                            self.clients[master_id].set_is_master(false);
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   403
                        }
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   404
                        room.master_id = Some(new_master_id);
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   405
                        Ok(ChangeMasterResult {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   406
                            old_master_id,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   407
                            new_master_id,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   408
                        })
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   409
                    } else {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   410
                        Err(ChangeMasterError::ClientNotInRoom)
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   411
                    }
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   412
                }
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   413
                None => Err(ChangeMasterError::NoClient),
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   414
            }
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   415
        } else {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   416
            Err(ChangeMasterError::NoAccess)
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   417
        }
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   418
    }
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
   419
15514
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   420
    pub fn start_game(&mut self, room_id: RoomId) -> Result<Vec<String>, StartGameError> {
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   421
        let (room_clients, room_nicks): (Vec<_>, Vec<_>) = self
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   422
            .clients
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   423
            .iter()
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   424
            .map(|(id, c)| (id, c.nick.clone()))
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   425
            .unzip();
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   426
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   427
        let room = &mut self.rooms[room_id];
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   428
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   429
        if !room.has_multiple_clans() {
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   430
            Err(StartGameError::NotEnoughClans)
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   431
        } else if room.protocol_number <= 43 && room.players_number != room.ready_players_number {
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   432
            Err(StartGameError::NotReady)
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   433
        } else if room.game_info.is_some() {
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   434
            Err(StartGameError::AlreadyInGame)
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   435
        } else {
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   436
            room.start_round();
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   437
            for id in room_clients {
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   438
                let c = &mut self.clients[id];
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   439
                c.set_is_in_game(true);
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   440
                c.team_indices = room.client_team_indices(c.id);
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   441
            }
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   442
            Ok(room_nicks)
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   443
        }
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   444
    }
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   445
15541
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   446
    pub fn leave_game(&mut self, client_id: ClientId) -> Option<Vec<String>> {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   447
        let client = &mut self.clients[client_id];
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   448
        let client_left = client.is_in_game();
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   449
        if client_left {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   450
            client.set_is_in_game(false);
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   451
            let room = &mut self.rooms[client.room_id.expect("Client should've been in the game")];
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   452
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   453
            let team_names: Vec<_> = room
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   454
                .client_teams(client_id)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   455
                .map(|t| t.name.clone())
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   456
                .collect();
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   457
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   458
            if let Some(ref mut info) = room.game_info {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   459
                info.teams_in_game -= team_names.len() as u8;
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   460
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   461
                for team_name in &team_names {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   462
                    let remove_msg =
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   463
                        utils::to_engine_msg(std::iter::once(b'F').chain(team_name.bytes()));
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   464
                    if let Some(m) = &info.sync_msg {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   465
                        info.msg_log.push(m.clone());
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   466
                    }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   467
                    if info.sync_msg.is_some() {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   468
                        info.sync_msg = None
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   469
                    }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   470
                    info.msg_log.push(remove_msg);
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   471
                }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   472
                Some(team_names)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   473
            } else {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   474
                unreachable!();
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   475
            }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   476
        } else {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   477
            None
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   478
        }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   479
    }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   480
15538
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   481
    pub fn end_game(&mut self, room_id: RoomId) -> EndGameResult {
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   482
        let room = &mut self.rooms[room_id];
15541
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   483
        room.ready_players_number = room.master_id.is_some() as u8;
15538
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   484
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   485
        if let Some(info) = replace(&mut room.game_info, None) {
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   486
            let joined_mid_game_clients = self
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   487
                .clients
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   488
                .iter()
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   489
                .filter(|(_, c)| c.room_id == Some(room_id) && c.is_joined_mid_game())
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   490
                .map(|(_, c)| c.id)
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   491
                .collect();
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   492
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   493
            let unreadied_nicks: Vec<_> = self
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   494
                .clients
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   495
                .iter_mut()
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   496
                .filter(|(_, c)| c.room_id == Some(room_id))
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   497
                .map(|(_, c)| {
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   498
                    c.set_is_ready(c.is_master());
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   499
                    c.set_is_joined_mid_game(false);
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   500
                    c
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   501
                })
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   502
                .filter_map(|c| {
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   503
                    if !c.is_master() {
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   504
                        Some(c.nick.clone())
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   505
                    } else {
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   506
                        None
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   507
                    }
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   508
                })
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   509
                .collect();
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   510
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   511
            EndGameResult {
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   512
                joined_mid_game_clients,
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   513
                left_teams: info.left_teams.clone(),
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   514
                unreadied_nicks,
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   515
            }
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   516
        } else {
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   517
            unreachable!()
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   518
        }
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   519
    }
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   520
15540
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   521
    pub fn enable_super_power(&mut self, client_id: ClientId) -> bool {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   522
        let client = &mut self.clients[client_id];
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   523
        if client.is_admin() {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   524
            client.set_has_super_power(true);
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   525
        }
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   526
        client.is_admin()
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   527
    }
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   528
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   529
    pub fn set_room_name(
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   530
        &mut self,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   531
        client_id: ClientId,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   532
        room_id: RoomId,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   533
        mut name: String,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   534
    ) -> Result<String, ModifyRoomNameError> {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   535
        let room_exists = self.has_room(&name);
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   536
        let room = &mut self.rooms[room_id];
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   537
        if room.is_fixed() || room.master_id != Some(client_id) {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   538
            Err(ModifyRoomNameError::AccessDenied)
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   539
        } else if utils::is_name_illegal(&name) {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   540
            Err(ModifyRoomNameError::InvalidName)
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   541
        } else if room_exists {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   542
            Err(ModifyRoomNameError::DuplicateName)
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   543
        } else {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   544
            std::mem::swap(&mut room.name, &mut name);
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   545
            Ok(name)
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   546
        }
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   547
    }
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   548
15541
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   549
    pub fn add_team(
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   550
        &mut self,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   551
        client_id: ClientId,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   552
        mut info: Box<TeamInfo>,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   553
    ) -> Result<&TeamInfo, AddTeamError> {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   554
        let client = &mut self.clients[client_id];
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   555
        if let Some(room_id) = client.room_id {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   556
            let room = &mut self.rooms[room_id];
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   557
            if room.teams.len() >= room.max_teams as usize {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   558
                Err(AddTeamError::TooManyTeams)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   559
            } else if room.addable_hedgehogs() == 0 {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   560
                Err(AddTeamError::TooManyHedgehogs)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   561
            } else if room.find_team(|t| t.name == info.name) != None {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   562
                Err(AddTeamError::TeamAlreadyExists)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   563
            } else if room.game_info.is_some() {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   564
                Err(AddTeamError::GameInProgress)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   565
            } else if room.is_team_add_restricted() {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   566
                Err(AddTeamError::Restricted)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   567
            } else {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   568
                info.owner = client.nick.clone();
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   569
                let team = room.add_team(client.id, *info, client.protocol_number < 42);
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   570
                client.teams_in_game += 1;
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   571
                client.clan = Some(team.color);
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   572
                Ok(team)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   573
            }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   574
        } else {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   575
            unreachable!()
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   576
        }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   577
    }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   578
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   579
    pub fn remove_team(
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   580
        &mut self,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   581
        client_id: ClientId,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   582
        team_name: &str,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   583
    ) -> Result<(), RemoveTeamError> {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   584
        let client = &mut self.clients[client_id];
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   585
        if let Some(room_id) = client.room_id {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   586
            let room = &mut self.rooms[room_id];
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   587
            match room.find_team_owner(team_name) {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   588
                None => Err(RemoveTeamError::NoTeam),
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   589
                Some((id, _)) if id != client_id => Err(RemoveTeamError::TeamNotOwned),
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   590
                Some(_) => {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   591
                    client.teams_in_game -= 1;
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   592
                    client.clan = room.find_team_color(client.id);
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   593
                    room.remove_team(team_name);
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   594
                    Ok(())
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   595
                }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   596
            }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   597
        } else {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   598
            unreachable!();
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   599
        }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   600
    }
15540
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   601
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   602
    pub fn set_team_color(
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   603
        &mut self,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   604
        client_id: ClientId,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   605
        room_id: RoomId,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   606
        team_name: &str,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   607
        color: u8,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   608
    ) -> Result<(), ModifyTeamError> {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   609
        let client = &self.clients[client_id];
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   610
        let room = &mut self.rooms[room_id];
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   611
        if let Some((owner, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   612
            if !client.is_master() {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   613
                Err(ModifyTeamError::NotMaster)
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   614
            } else {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   615
                team.color = color;
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   616
                self.clients[owner].clan = Some(color);
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   617
                Ok(())
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   618
            }
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   619
        } else {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   620
            Err(ModifyTeamError::NoTeam)
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   621
        }
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   622
    }
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   623
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   624
    pub fn toggle_ready(&mut self, client_id: ClientId) -> bool {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   625
        let client = &mut self.clients[client_id];
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   626
        if let Some(room_id) = client.room_id {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   627
            let room = &mut self.rooms[room_id];
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   628
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   629
            client.set_is_ready(!client.is_ready());
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   630
            if client.is_ready() {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   631
                room.ready_players_number += 1;
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   632
            } else {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   633
                room.ready_players_number -= 1;
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   634
            }
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   635
        }
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   636
        client.is_ready()
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   637
    }
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   638
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   639
    #[inline]
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   640
    pub fn set_var(&mut self, client_id: ClientId, var: ServerVar) -> Result<(), AccessError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   641
        if self.clients[client_id].is_admin() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   642
            match var {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   643
                ServerVar::MOTDNew(msg) => self.greetings.for_latest_protocol = msg,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   644
                ServerVar::MOTDOld(msg) => self.greetings.for_old_protocols = msg,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   645
                ServerVar::LatestProto(n) => self.latest_protocol = n,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   646
            }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   647
            Ok(())
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   648
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   649
            Err(AccessError())
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   650
        }
14692
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14525
diff changeset
   651
    }
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14525
diff changeset
   652
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   653
    #[inline]
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   654
    pub fn get_vars(&self, client_id: ClientId) -> Result<[ServerVar; 3], AccessError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   655
        if self.clients[client_id].is_admin() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   656
            Ok([
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   657
                ServerVar::MOTDNew(self.greetings.for_latest_protocol.clone()),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   658
                ServerVar::MOTDOld(self.greetings.for_old_protocols.clone()),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   659
                ServerVar::LatestProto(self.latest_protocol),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   660
            ])
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   661
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   662
            Err(AccessError())
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   663
        }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   664
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   665
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   666
    pub fn get_used_protocols(&self, client_id: ClientId) -> Result<Vec<u16>, AccessError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   667
        if self.clients[client_id].is_admin() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   668
            let mut protocols: HashSet<_> = self
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   669
                .clients
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   670
                .iter()
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   671
                .map(|(_, c)| c.protocol_number)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   672
                .chain(self.rooms.iter().map(|(_, r)| r.protocol_number))
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   673
                .collect();
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   674
            let mut protocols: Vec<_> = protocols.drain().collect();
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   675
            protocols.sort();
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   676
            Ok(protocols)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   677
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   678
            Err(AccessError())
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   679
        }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   680
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   681
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   682
    #[inline]
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   683
    pub fn has_room(&self, name: &str) -> bool {
14718
f64e21f164a5 fix some warnings
alfadur
parents: 14717
diff changeset
   684
        self.find_room(name).is_some()
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   685
    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   686
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   687
    #[inline]
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   688
    pub fn find_room(&self, name: &str) -> Option<&HwRoom> {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   689
        self.rooms
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   690
            .iter()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   691
            .find_map(|(_, r)| Some(r).filter(|r| r.name == name))
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   692
    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   693
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   694
    pub fn find_room_mut(&mut self, name: &str) -> Option<&mut HwRoom> {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   695
        self.rooms
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   696
            .iter_mut()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   697
            .find_map(|(_, r)| Some(r).filter(|r| r.name == name))
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   698
    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   699
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   700
    pub fn find_client(&self, nick: &str) -> Option<&HwClient> {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   701
        self.clients
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   702
            .iter()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   703
            .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick))
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13445
diff changeset
   704
    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13445
diff changeset
   705
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   706
    pub fn find_client_mut(&mut self, nick: &str) -> Option<&mut HwClient> {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   707
        self.clients
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   708
            .iter_mut()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   709
            .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick))
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13445
diff changeset
   710
    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13445
diff changeset
   711
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   712
    pub fn all_clients(&self) -> impl Iterator<Item = ClientId> + '_ {
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   713
        self.clients.iter().map(|(id, _)| id)
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   714
    }
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   715
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   716
    pub fn filter_clients<'a, F>(&'a self, f: F) -> impl Iterator<Item = ClientId> + 'a
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   717
    where
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   718
        F: Fn(&(usize, &HwClient)) -> bool + 'a,
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   719
    {
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   720
        self.clients.iter().filter(f).map(|(_, c)| c.id)
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   721
    }
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   722
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   723
    pub fn filter_rooms<'a, F>(&'a self, f: F) -> impl Iterator<Item = RoomId> + 'a
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   724
    where
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   725
        F: Fn(&(usize, &HwRoom)) -> bool + 'a,
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   726
    {
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   727
        self.rooms.iter().filter(f).map(|(_, c)| c.id)
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   728
    }
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   729
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   730
    pub fn collect_clients<F>(&self, f: F) -> Vec<ClientId>
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   731
    where
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   732
        F: Fn(&(usize, &HwClient)) -> bool,
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   733
    {
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   734
        self.filter_clients(f).collect()
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   735
    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   736
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   737
    pub fn collect_nicks<F>(&self, f: F) -> Vec<String>
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   738
    where
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   739
        F: Fn(&(usize, &HwClient)) -> bool,
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   740
    {
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   741
        self.clients
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   742
            .iter()
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   743
            .filter(f)
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   744
            .map(|(_, c)| c.nick.clone())
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   745
            .collect()
14715
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14714
diff changeset
   746
    }
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14714
diff changeset
   747
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   748
    pub fn lobby_clients(&self) -> impl Iterator<Item = ClientId> + '_ {
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   749
        self.filter_clients(|(_, c)| c.room_id == None)
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   750
    }
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   751
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   752
    pub fn room_clients(&self, room_id: RoomId) -> impl Iterator<Item = ClientId> + '_ {
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   753
        self.filter_clients(move |(_, c)| c.room_id == Some(room_id))
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   754
    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   755
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   756
    pub fn protocol_clients(&self, protocol: u16) -> impl Iterator<Item = ClientId> + '_ {
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   757
        self.filter_clients(move |(_, c)| c.protocol_number == protocol)
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   758
    }
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   759
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   760
    pub fn protocol_rooms(&self, protocol: u16) -> impl Iterator<Item = RoomId> + '_ {
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   761
        self.filter_rooms(move |(_, r)| r.protocol_number == protocol)
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   762
    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   763
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   764
    pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> {
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   765
        let room_id = self.clients[self_id].room_id;
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   766
        self.collect_clients(|(id, c)| *id != self_id && c.room_id == room_id)
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   767
    }
14807
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   768
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   769
    pub fn is_registered_only(&self) -> bool {
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   770
        self.flags.contains(ServerFlags::REGISTERED_ONLY)
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   771
    }
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   772
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   773
    pub fn set_is_registered_only(&mut self, value: bool) {
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   774
        self.flags.set(ServerFlags::REGISTERED_ONLY, value)
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   775
    }
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13442
diff changeset
   776
}
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   777
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   778
fn allocate_room(rooms: &mut Slab<HwRoom>) -> &mut HwRoom {
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   779
    let entry = rooms.vacant_entry();
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   780
    let room = HwRoom::new(entry.key());
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   781
    entry.insert(room)
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   782
}
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   783
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   784
fn create_room<'a, 'b>(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   785
    client: &'a mut HwClient,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   786
    rooms: &'b mut Slab<HwRoom>,
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   787
    name: String,
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   788
    password: Option<String>,
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   789
) -> (&'a HwClient, &'b HwRoom) {
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   790
    let room = allocate_room(rooms);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   791
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   792
    room.master_id = Some(client.id);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   793
    room.name = name;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   794
    room.password = password;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   795
    room.protocol_number = client.protocol_number;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   796
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   797
    room.players_number = 1;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   798
    room.ready_players_number = 1;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   799
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   800
    client.room_id = Some(room.id);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   801
    client.set_is_master(true);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   802
    client.set_is_ready(true);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   803
    client.set_is_joined_mid_game(false);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   804
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   805
    (client, room)
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   806
}
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   807
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   808
fn move_to_room(client: &mut HwClient, room: &mut HwRoom) {
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   809
    debug_assert!(client.room_id != Some(room.id));
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   810
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   811
    room.players_number += 1;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   812
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   813
    client.room_id = Some(room.id);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   814
    client.set_is_joined_mid_game(room.game_info.is_some());
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   815
    client.set_is_in_game(room.game_info.is_some());
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   816
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   817
    if let Some(ref mut info) = room.game_info {
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   818
        let teams = info.client_teams(client.id);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   819
        client.teams_in_game = teams.clone().count() as u8;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   820
        client.clan = teams.clone().next().map(|t| t.color);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   821
        let team_names: Vec<_> = teams.map(|t| t.name.clone()).collect();
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   822
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   823
        if !team_names.is_empty() {
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   824
            info.left_teams.retain(|name| !team_names.contains(&name));
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   825
            info.teams_in_game += team_names.len() as u8;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   826
            room.teams = info
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   827
                .teams_at_start
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   828
                .iter()
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   829
                .filter(|(_, t)| !team_names.contains(&t.name))
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   830
                .cloned()
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   831
                .collect();
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   832
        }
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   833
    }
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   834
}