rust/hedgewars-server/src/core/server.rs
author alfadur
Fri, 10 Apr 2020 21:58:09 +0300
changeset 15573 7478568cffbe
parent 15563 d122b65bdf6f
child 15574 0031683bfa76
permissions -rw-r--r--
fix phantom rooms
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,
15547
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
     6
    types::{ClientId, GameCfg, RoomId, ServerVar, TeamInfo, Vote, VoteType, Voting},
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     7
};
15555
0606f89698e7 check the room passwords
alfadur <mail@none>
parents: 15554
diff changeset
     8
use crate::utils;
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14707
diff changeset
     9
14807
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
    10
use bitflags::*;
13810
0463a4221327 cleanup crate imports
alfadur
parents: 13771
diff changeset
    11
use log::*;
15539
abd5eb807166 add ip ban check
alfadur <mail@none>
parents: 15538
diff changeset
    12
use slab::Slab;
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    13
use std::{borrow::BorrowMut, cmp::min, collections::HashSet, iter, mem::replace};
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    14
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
    15
#[derive(Debug)]
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    16
pub enum CreateRoomError {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    17
    InvalidName,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    18
    AlreadyExists,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    19
}
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    20
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
    21
#[derive(Debug)]
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    22
pub enum JoinRoomError {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    23
    DoesntExist,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    24
    WrongProtocol,
15555
0606f89698e7 check the room passwords
alfadur <mail@none>
parents: 15554
diff changeset
    25
    WrongPassword,
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    26
    Full,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    27
    Restricted,
15556
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15555
diff changeset
    28
    RegistrationRequired,
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    29
}
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
    30
15540
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    31
#[derive(Debug)]
15504
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    32
pub enum LeaveRoomResult {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    33
    RoomRemoved,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    34
    RoomRemains {
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    35
        is_empty: bool,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    36
        was_master: bool,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    37
        was_in_game: bool,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    38
        new_master: Option<ClientId>,
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    39
        removed_teams: Vec<String>,
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
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
    43
#[derive(Debug)]
15509
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    44
pub struct ChangeMasterResult {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    45
    pub old_master_id: Option<ClientId>,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    46
    pub new_master_id: ClientId,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    47
}
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    48
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    49
#[derive(Debug)]
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    50
pub enum ChangeMasterError {
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    51
    NoAccess,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    52
    AlreadyMaster,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    53
    NoClient,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    54
    ClientNotInRoom,
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    55
}
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    56
91f0c5ec37b5 move change_master into the server
alfadur
parents: 15504
diff changeset
    57
#[derive(Debug)]
15541
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    58
pub enum AddTeamError {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    59
    TooManyTeams,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    60
    TooManyHedgehogs,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    61
    TeamAlreadyExists,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    62
    GameInProgress,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    63
    Restricted,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    64
}
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    65
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    66
#[derive(Debug)]
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    67
pub enum RemoveTeamError {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    68
    NoTeam,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    69
    TeamNotOwned,
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    70
}
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    71
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
    72
#[derive(Debug)]
15540
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    73
pub enum ModifyTeamError {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    74
    NoTeam,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    75
    NotMaster,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    76
}
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    77
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    78
#[derive(Debug)]
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    79
pub enum SetTeamCountError {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    80
    InvalidNumber,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    81
    NotMaster,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    82
}
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    83
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    84
#[derive(Debug)]
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    85
pub enum SetHedgehogsError {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    86
    NoTeam,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    87
    InvalidNumber(u8),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    88
    NotMaster,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    89
}
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    90
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    91
#[derive(Debug)]
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    92
pub enum SetConfigError {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    93
    NotMaster,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    94
    RoomFixed,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    95
}
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    96
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
    97
#[derive(Debug)]
15540
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    98
pub enum ModifyRoomNameError {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
    99
    AccessDenied,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   100
    InvalidName,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   101
    DuplicateName,
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   102
}
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   103
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   104
#[derive(Debug)]
15547
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   105
pub enum StartVoteError {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   106
    VotingInProgress,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   107
}
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   108
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   109
#[derive(Debug)]
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   110
pub enum VoteResult {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   111
    Submitted,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   112
    Succeeded(VoteType),
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   113
    Failed,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   114
}
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   115
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   116
#[derive(Debug)]
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   117
pub enum VoteError {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   118
    NoVoting,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   119
    AlreadyVoted,
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   120
}
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   121
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   122
#[derive(Debug)]
15514
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   123
pub enum StartGameError {
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   124
    NotEnoughClans,
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   125
    NotReady,
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   126
    AlreadyInGame,
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   127
}
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   128
395be40faa51 move start_game into the server
alfadur
parents: 15509
diff changeset
   129
#[derive(Debug)]
15538
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   130
pub struct EndGameResult {
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   131
    pub joined_mid_game_clients: Vec<ClientId>,
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   132
    pub left_teams: Vec<String>,
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   133
    pub unreadied_nicks: Vec<String>,
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   134
}
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   135
b907b9071ec5 move end_game into server
alfadur
parents: 15514
diff changeset
   136
#[derive(Debug)]
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   137
pub struct UninitializedError();
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   138
#[derive(Debug)]
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   139
pub struct AccessError();
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   140
14804
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   141
pub struct ServerGreetings {
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   142
    pub for_latest_protocol: String,
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   143
    pub for_old_protocols: String,
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   144
}
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   145
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   146
impl ServerGreetings {
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   147
    fn new() -> Self {
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   148
        Self {
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   149
            for_latest_protocol: "\u{1f994} is watching".to_string(),
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   150
            for_old_protocols: "\u{1f994} is watching".to_string(),
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   151
        }
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   152
    }
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   153
}
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   154
14807
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   155
bitflags! {
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   156
    pub struct ServerFlags: u8 {
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   157
        const REGISTERED_ONLY = 0b0000_1000;
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   158
    }
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   159
}
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   160
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   161
struct HwChecker {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   162
    pub id: ClientId,
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   163
    pub is_ready: bool,
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   164
}
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   165
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   166
impl HwChecker {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   167
    pub fn new(id: ClientId) -> Self {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   168
        Self {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   169
            id,
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   170
            is_ready: false,
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   171
        }
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   172
    }
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   173
}
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   174
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   175
pub struct HwServer {
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15541
diff changeset
   176
    clients: IndexSlab<HwClient>,
15544
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   177
    rooms: Slab<HwRoom>,
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   178
    checkers: IndexSlab<HwChecker>,
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   179
    latest_protocol: u16,
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   180
    flags: ServerFlags,
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   181
    greetings: ServerGreetings,
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   182
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   183
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   184
impl HwServer {
14801
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14800
diff changeset
   185
    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
   186
        let rooms = Slab::with_capacity(rooms_limit);
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14707
diff changeset
   187
        let clients = IndexSlab::with_capacity(clients_limit);
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   188
        let checkers = IndexSlab::new();
14715
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14714
diff changeset
   189
        Self {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   190
            clients,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   191
            rooms,
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   192
            checkers,
14804
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   193
            greetings: ServerGreetings::new(),
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   194
            latest_protocol: 58,
14807
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   195
            flags: ServerFlags::empty(),
14715
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14714
diff changeset
   196
        }
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   197
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   198
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   199
    #[inline]
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   200
    pub fn client(&self, client_id: ClientId) -> &HwClient {
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   201
        &self.clients[client_id]
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   202
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   203
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   204
    #[inline]
15541
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   205
    pub fn has_client(&self, client_id: ClientId) -> bool {
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   206
        self.clients.contains(client_id)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   207
    }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   208
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   209
    #[inline]
15556
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15555
diff changeset
   210
    pub fn iter_clients(&self) -> impl Iterator<Item = &HwClient> + Clone {
15541
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   211
        self.clients.iter().map(|(_, c)| c)
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   212
    }
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   213
b3157d218ae2 disallow mutable clients to leave the server
alfadur <mail@none>
parents: 15540
diff changeset
   214
    #[inline]
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   215
    pub fn room(&self, room_id: RoomId) -> &HwRoom {
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   216
        &self.rooms[room_id]
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   217
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   218
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   219
    #[inline]
15544
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   220
    pub fn get_room(&self, room_id: RoomId) -> Option<&HwRoom> {
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   221
        self.rooms.get(room_id)
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   222
    }
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   223
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   224
    #[inline]
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   225
    fn get_room_mut(&mut self, room_id: RoomId) -> Option<&mut HwRoom> {
15544
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   226
        self.rooms.get_mut(room_id)
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   227
    }
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   228
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   229
    #[inline]
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   230
    pub fn iter_rooms(&self) -> impl Iterator<Item = &HwRoom> {
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   231
        self.rooms.iter().map(|(_, r)| r)
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   232
    }
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   233
4a0b06b03199 make rooms field private
alfadur <mail@none>
parents: 15542
diff changeset
   234
    #[inline]
15504
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   235
    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
   236
        (&self.clients[client_id], &self.rooms[room_id])
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   237
    }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   238
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   239
    #[inline]
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   240
    fn client_and_room_mut(&mut self, client_id: ClientId) -> Option<(&mut HwClient, &mut HwRoom)> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   241
        let client = &mut self.clients[client_id];
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   242
        if let Some(room_id) = client.room_id {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   243
            Some((client, &mut self.rooms[room_id]))
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   244
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   245
            None
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   246
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   247
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   248
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   249
    #[inline]
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   250
    pub fn get_room_control(&mut self, client_id: ClientId) -> Option<HwRoomControl> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   251
        HwRoomControl::new(self, client_id)
15504
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   252
    }
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   253
4cc9ec732392 refactor some of the room handler
alfadur
parents: 15465
diff changeset
   254
    #[inline]
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   255
    pub fn is_admin(&self, client_id: ClientId) -> bool {
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   256
        self.clients
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   257
            .get(client_id)
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   258
            .map(|c| c.is_admin())
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   259
            .unwrap_or(false)
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   260
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   261
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15541
diff changeset
   262
    pub fn add_client(&mut self, client_id: ClientId, data: HwAnteroomClient) {
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   263
        if data.is_checker {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   264
            self.checkers.insert(client_id, HwChecker::new(client_id));
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15553
diff changeset
   265
        } else if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) {
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   266
            let mut client = HwClient::new(client_id, protocol.get(), nick);
15197
f6115638aa92 enable local admins for non-official servers
alfadur
parents: 15096
diff changeset
   267
            #[cfg(not(feature = "official-server"))]
f6115638aa92 enable local admins for non-official servers
alfadur
parents: 15096
diff changeset
   268
            client.set_is_admin(data.is_local_admin);
f6115638aa92 enable local admins for non-official servers
alfadur
parents: 15096
diff changeset
   269
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   270
            #[cfg(feature = "official-server")]
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   271
            {
15553
ede5f4ec48f3 fix official server
alfadur <mail@none>
parents: 15551
diff changeset
   272
                client.set_is_registered(data.is_registered);
ede5f4ec48f3 fix official server
alfadur <mail@none>
parents: 15551
diff changeset
   273
                client.set_is_admin(data.is_admin);
ede5f4ec48f3 fix official server
alfadur <mail@none>
parents: 15551
diff changeset
   274
                client.set_is_contributor(data.is_contributor);
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   275
            }
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   276
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14707
diff changeset
   277
            self.clients.insert(client_id, client);
13119
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12852
diff changeset
   278
        }
12126
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   279
    }
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
   280
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
   281
    pub fn remove_client(&mut self, client_id: ClientId) {
14717
8a45c90f4580 fix client removal
alfadur
parents: 14716
diff changeset
   282
        self.clients.remove(client_id);
12127
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
   283
    }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12126
diff changeset
   284
15465
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   285
    pub fn get_greetings(&self, client: &HwClient) -> &str {
61a0bd0bb021 refactor root handler
alfadur
parents: 15463
diff changeset
   286
        if client.protocol_number < self.latest_protocol {
14804
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   287
            &self.greetings.for_old_protocols
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   288
        } else {
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   289
            &self.greetings.for_latest_protocol
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   290
        }
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   291
    }
b3adc030104b implement server vars
alfadur
parents: 14802
diff changeset
   292
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   293
    #[inline]
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   294
    pub fn create_room(
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   295
        &mut self,
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   296
        creator_id: ClientId,
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   297
        name: String,
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   298
        password: Option<String>,
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   299
    ) -> Result<(&HwClient, &HwRoom), CreateRoomError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   300
        use CreateRoomError::*;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   301
        if utils::is_name_illegal(&name) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   302
            Err(InvalidName)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   303
        } else if self.has_room(&name) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   304
            Err(AlreadyExists)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   305
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   306
            Ok(create_room(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   307
                &mut self.clients[creator_id],
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   308
                &mut self.rooms,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   309
                name,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   310
                password,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   311
            ))
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   312
        }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   313
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   314
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   315
    pub fn join_room(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   316
        &mut self,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   317
        client_id: ClientId,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   318
        room_id: RoomId,
15555
0606f89698e7 check the room passwords
alfadur <mail@none>
parents: 15554
diff changeset
   319
        room_password: Option<&str>,
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   320
    ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   321
        use JoinRoomError::*;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   322
        let room = &mut self.rooms[room_id];
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   323
        let client = &mut self.clients[client_id];
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   324
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   325
        if client.protocol_number != room.protocol_number {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   326
            Err(WrongProtocol)
15556
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15555
diff changeset
   327
        } else if room.password.is_some()
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15555
diff changeset
   328
            && room_password != room.password.as_deref()
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15555
diff changeset
   329
            && !client.has_super_power()
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15555
diff changeset
   330
        {
15555
0606f89698e7 check the room passwords
alfadur <mail@none>
parents: 15554
diff changeset
   331
            Err(WrongPassword)
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   332
        } else if room.is_join_restricted() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   333
            Err(Restricted)
15556
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15555
diff changeset
   334
        } else if room.is_registration_required() {
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15555
diff changeset
   335
            Err(RegistrationRequired)
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   336
        } else if room.players_number == u8::max_value() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   337
            Err(Full)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   338
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   339
            move_to_room(client, room);
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   340
            let room_id = room.id;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   341
            Ok((
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   342
                &self.clients[client_id],
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   343
                &self.rooms[room_id],
15556
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15555
diff changeset
   344
                self.iter_clients()
bb93e9642b5b send cleared flags on room join
alfadur <mail@none>
parents: 15555
diff changeset
   345
                    .filter(move |c| c.room_id == Some(room_id)),
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   346
            ))
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   347
        }
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   348
    }
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   349
14692
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14525
diff changeset
   350
    #[inline]
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   351
    pub fn join_room_by_name(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   352
        &mut self,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   353
        client_id: ClientId,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   354
        room_name: &str,
15555
0606f89698e7 check the room passwords
alfadur <mail@none>
parents: 15554
diff changeset
   355
        room_password: Option<&str>,
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   356
    ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   357
        use JoinRoomError::*;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   358
        let room = self.rooms.iter().find(|(_, r)| r.name == room_name);
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   359
        if let Some((_, room)) = room {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   360
            let room_id = room.id;
15555
0606f89698e7 check the room passwords
alfadur <mail@none>
parents: 15554
diff changeset
   361
            self.join_room(client_id, room_id, room_password)
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   362
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   363
            Err(DoesntExist)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   364
        }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   365
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   366
15540
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   367
    pub fn enable_super_power(&mut self, client_id: ClientId) -> bool {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   368
        let client = &mut self.clients[client_id];
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   369
        if client.is_admin() {
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   370
            client.set_has_super_power(true);
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   371
        }
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   372
        client.is_admin()
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   373
    }
e705d30e0f10 some more room handler cleanup
alfadur <mail@none>
parents: 15539
diff changeset
   374
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   375
    #[inline]
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   376
    pub fn set_var(&mut self, client_id: ClientId, var: ServerVar) -> Result<(), AccessError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   377
        if self.clients[client_id].is_admin() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   378
            match var {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   379
                ServerVar::MOTDNew(msg) => self.greetings.for_latest_protocol = msg,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   380
                ServerVar::MOTDOld(msg) => self.greetings.for_old_protocols = msg,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   381
                ServerVar::LatestProto(n) => self.latest_protocol = n,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   382
            }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   383
            Ok(())
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   384
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   385
            Err(AccessError())
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   386
        }
14692
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14525
diff changeset
   387
    }
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14525
diff changeset
   388
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   389
    #[inline]
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   390
    pub fn get_vars(&self, client_id: ClientId) -> Result<[ServerVar; 3], AccessError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   391
        if self.clients[client_id].is_admin() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   392
            Ok([
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   393
                ServerVar::MOTDNew(self.greetings.for_latest_protocol.clone()),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   394
                ServerVar::MOTDOld(self.greetings.for_old_protocols.clone()),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   395
                ServerVar::LatestProto(self.latest_protocol),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   396
            ])
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   397
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   398
            Err(AccessError())
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   399
        }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   400
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   401
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   402
    pub fn get_used_protocols(&self, client_id: ClientId) -> Result<Vec<u16>, AccessError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   403
        if self.clients[client_id].is_admin() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   404
            let mut protocols: HashSet<_> = self
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   405
                .clients
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   406
                .iter()
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   407
                .map(|(_, c)| c.protocol_number)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   408
                .chain(self.rooms.iter().map(|(_, r)| r.protocol_number))
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   409
                .collect();
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   410
            let mut protocols: Vec<_> = protocols.drain().collect();
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   411
            protocols.sort();
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   412
            Ok(protocols)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   413
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   414
            Err(AccessError())
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   415
        }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   416
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   417
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   418
    #[inline]
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   419
    pub fn has_room(&self, name: &str) -> bool {
14718
f64e21f164a5 fix some warnings
alfadur
parents: 14717
diff changeset
   420
        self.find_room(name).is_some()
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   421
    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   422
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
   423
    #[inline]
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   424
    pub fn find_room(&self, name: &str) -> Option<&HwRoom> {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   425
        self.rooms
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   426
            .iter()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   427
            .find_map(|(_, r)| Some(r).filter(|r| r.name == name))
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   428
    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   429
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   430
    fn find_room_mut(&mut self, name: &str) -> Option<&mut HwRoom> {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   431
        self.rooms
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   432
            .iter_mut()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   433
            .find_map(|(_, r)| Some(r).filter(|r| r.name == name))
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   434
    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   435
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   436
    pub fn find_client(&self, nick: &str) -> Option<&HwClient> {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   437
        self.clients
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   438
            .iter()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   439
            .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick))
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13445
diff changeset
   440
    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13445
diff changeset
   441
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   442
    fn find_client_mut(&mut self, nick: &str) -> Option<&mut HwClient> {
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   443
        self.clients
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   444
            .iter_mut()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   445
            .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick))
13450
d79795acaa73 Mostly implement voting
alfadur
parents: 13445
diff changeset
   446
    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13445
diff changeset
   447
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   448
    pub fn iter_client_ids(&self) -> impl Iterator<Item = ClientId> + '_ {
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   449
        self.clients.iter().map(|(id, _)| id)
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   450
    }
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   451
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   452
    pub fn filter_clients<'a, F>(&'a self, f: F) -> impl Iterator<Item = ClientId> + 'a
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   453
    where
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   454
        F: Fn(&(usize, &HwClient)) -> bool + 'a,
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   455
    {
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   456
        self.clients.iter().filter(f).map(|(_, c)| c.id)
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   457
    }
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   458
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   459
    pub fn filter_rooms<'a, F>(&'a self, f: F) -> impl Iterator<Item = RoomId> + 'a
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   460
    where
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   461
        F: Fn(&(usize, &HwRoom)) -> bool + 'a,
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   462
    {
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   463
        self.rooms.iter().filter(f).map(|(_, c)| c.id)
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   464
    }
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   465
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   466
    pub fn collect_client_ids<F>(&self, f: F) -> Vec<ClientId>
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   467
    where
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   468
        F: Fn(&(usize, &HwClient)) -> bool,
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
   469
    {
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   470
        self.filter_clients(f).collect()
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   471
    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   472
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   473
    pub fn collect_nicks<F>(&self, f: F) -> Vec<String>
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   474
    where
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   475
        F: Fn(&(usize, &HwClient)) -> bool,
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   476
    {
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   477
        self.clients
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   478
            .iter()
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   479
            .filter(f)
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   480
            .map(|(_, c)| c.nick.clone())
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   481
            .collect()
14715
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14714
diff changeset
   482
    }
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14714
diff changeset
   483
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   484
    pub fn lobby_client_ids(&self) -> impl Iterator<Item = ClientId> + '_ {
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   485
        self.filter_clients(|(_, c)| c.room_id == None)
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   486
    }
01f8ab45f806 fix lobby joining
alfadur
parents: 14801
diff changeset
   487
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   488
    pub fn room_client_ids(&self, room_id: RoomId) -> impl Iterator<Item = ClientId> + '_ {
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   489
        self.filter_clients(move |(_, c)| c.room_id == Some(room_id))
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   490
    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   491
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   492
    pub fn protocol_client_ids(&self, protocol: u16) -> impl Iterator<Item = ClientId> + '_ {
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   493
        self.filter_clients(move |(_, c)| c.protocol_number == protocol)
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   494
    }
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   495
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   496
    pub fn protocol_room_ids(&self, protocol: u16) -> impl Iterator<Item = RoomId> + '_ {
14810
18240b308505 implement stats message
alfadur
parents: 14807
diff changeset
   497
        self.filter_rooms(move |(_, r)| r.protocol_number == protocol)
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   498
    }
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   499
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   500
    pub fn other_client_ids_in_room(&self, self_id: ClientId) -> Vec<ClientId> {
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   501
        let room_id = self.clients[self_id].room_id;
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   502
        self.collect_client_ids(|(id, c)| *id != self_id && c.room_id == room_id)
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
   503
    }
14807
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   504
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   505
    pub fn is_registered_only(&self) -> bool {
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   506
        self.flags.contains(ServerFlags::REGISTERED_ONLY)
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   507
    }
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   508
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   509
    pub fn set_is_registered_only(&mut self, value: bool) {
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   510
        self.flags.set(ServerFlags::REGISTERED_ONLY, value)
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14804
diff changeset
   511
    }
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   512
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   513
    pub fn set_room_saves(&mut self, room_id: RoomId, text: &str) -> Result<(), serde_yaml::Error> {
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   514
        if let Some(room) = self.rooms.get_mut(room_id) {
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   515
            room.set_saves(text)
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   516
        } else {
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   517
            Ok(())
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   518
        }
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   519
    }
13445
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13442
diff changeset
   520
}
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
   521
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   522
pub struct HwRoomControl<'a> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   523
    server: &'a mut HwServer,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   524
    client_id: ClientId,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   525
    room_id: RoomId,
15573
7478568cffbe fix phantom rooms
alfadur
parents: 15563
diff changeset
   526
    is_room_removed: bool
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   527
}
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   528
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   529
impl<'a> HwRoomControl<'a> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   530
    #[inline]
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   531
    pub fn new(server: &'a mut HwServer, client_id: ClientId) -> Option<Self> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   532
        if let Some(room_id) = server.clients[client_id].room_id {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   533
            Some(Self {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   534
                server,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   535
                client_id,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   536
                room_id,
15573
7478568cffbe fix phantom rooms
alfadur
parents: 15563
diff changeset
   537
                is_room_removed: false
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   538
            })
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   539
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   540
            None
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   541
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   542
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   543
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   544
    #[inline]
15573
7478568cffbe fix phantom rooms
alfadur
parents: 15563
diff changeset
   545
    pub fn cleanup_room(self) {
7478568cffbe fix phantom rooms
alfadur
parents: 15563
diff changeset
   546
        if self.is_room_removed {
7478568cffbe fix phantom rooms
alfadur
parents: 15563
diff changeset
   547
            self.server.rooms.remove(self.room_id);
7478568cffbe fix phantom rooms
alfadur
parents: 15563
diff changeset
   548
        }
7478568cffbe fix phantom rooms
alfadur
parents: 15563
diff changeset
   549
    }
7478568cffbe fix phantom rooms
alfadur
parents: 15563
diff changeset
   550
7478568cffbe fix phantom rooms
alfadur
parents: 15563
diff changeset
   551
    #[inline]
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   552
    pub fn server(&self) -> &HwServer {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   553
        self.server
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   554
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   555
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   556
    #[inline]
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   557
    pub fn client(&self) -> &HwClient {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   558
        &self.server.clients[self.client_id]
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   559
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   560
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   561
    #[inline]
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   562
    fn client_mut(&mut self) -> &mut HwClient {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   563
        &mut self.server.clients[self.client_id]
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   564
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   565
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   566
    #[inline]
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   567
    pub fn room(&self) -> &HwRoom {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   568
        &self.server.rooms[self.room_id]
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   569
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   570
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   571
    #[inline]
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   572
    fn room_mut(&mut self) -> &mut HwRoom {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   573
        &mut self.server.rooms[self.room_id]
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   574
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   575
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   576
    #[inline]
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   577
    pub fn get(&self) -> (&HwClient, &HwRoom) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   578
        (self.client(), self.room())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   579
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   580
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   581
    #[inline]
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   582
    fn get_mut(&mut self) -> (&mut HwClient, &mut HwRoom) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   583
        (
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   584
            &mut self.server.clients[self.client_id],
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   585
            &mut self.server.rooms[self.room_id],
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   586
        )
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   587
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   588
15547
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   589
    pub fn change_client<'b: 'a>(self, client_id: ClientId) -> Option<HwRoomControl<'a>> {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   590
        let room_id = self.room_id;
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   591
        HwRoomControl::new(self.server, client_id).filter(|c| c.room_id == room_id)
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   592
    }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   593
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   594
    pub fn leave_room(&mut self) -> LeaveRoomResult {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   595
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   596
        room.players_number -= 1;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   597
        client.room_id = None;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   598
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   599
        let is_empty = room.players_number == 0;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   600
        let is_fixed = room.is_fixed();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   601
        let was_master = room.master_id == Some(client.id);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   602
        let was_in_game = client.is_in_game();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   603
        let mut removed_teams = vec![];
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   604
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   605
        if is_empty && !is_fixed {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   606
            if client.is_ready() && room.ready_players_number > 0 {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   607
                room.ready_players_number -= 1;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   608
            }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   609
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   610
            removed_teams = room
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   611
                .client_teams(client.id)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   612
                .map(|t| t.name.clone())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   613
                .collect();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   614
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   615
            for team_name in &removed_teams {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   616
                room.remove_team(team_name);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   617
            }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   618
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   619
            if client.is_master() && !is_fixed {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   620
                client.set_is_master(false);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   621
                room.master_id = None;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   622
            }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   623
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   624
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   625
        client.set_is_ready(false);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   626
        client.set_is_in_game(false);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   627
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   628
        if !is_fixed {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   629
            if room.players_number == 0 {
15573
7478568cffbe fix phantom rooms
alfadur
parents: 15563
diff changeset
   630
                self.is_room_removed = true
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   631
            } else if room.master_id == None {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   632
                let protocol_number = room.protocol_number;
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   633
                let new_master_id = self.server.room_client_ids(self.room_id).next();
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   634
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   635
                if let Some(new_master_id) = new_master_id {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   636
                    let room = self.room_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   637
                    room.master_id = Some(new_master_id);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   638
                    let new_master = &mut self.server.clients[new_master_id];
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   639
                    new_master.set_is_master(true);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   640
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   641
                    if protocol_number < 42 {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   642
                        let nick = new_master.nick.clone();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   643
                        self.room_mut().name = nick;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   644
                    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   645
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   646
                    let room = self.room_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   647
                    room.set_join_restriction(false);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   648
                    room.set_team_add_restriction(false);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   649
                    room.set_unregistered_players_restriction(true);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   650
                }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   651
            }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   652
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   653
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   654
        if is_empty && !is_fixed {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   655
            LeaveRoomResult::RoomRemoved
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   656
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   657
            LeaveRoomResult::RoomRemains {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   658
                is_empty,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   659
                was_master,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   660
                was_in_game,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   661
                new_master: self.room().master_id,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   662
                removed_teams,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   663
            }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   664
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   665
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   666
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   667
    pub fn change_master(
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   668
        &mut self,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   669
        new_master_nick: String,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   670
    ) -> Result<ChangeMasterResult, ChangeMasterError> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   671
        use ChangeMasterError::*;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   672
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   673
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   674
        if client.is_admin() || room.master_id == Some(client.id) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   675
            let new_master_id = self
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   676
                .server
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   677
                .clients
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   678
                .iter()
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   679
                .find(|(_, c)| c.nick == new_master_nick)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   680
                .map(|(id, _)| id);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   681
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   682
            match new_master_id {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   683
                Some(new_master_id) if new_master_id == self.client_id => Err(AlreadyMaster),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   684
                Some(new_master_id) => {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   685
                    let new_master = &mut self.server.clients[new_master_id];
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   686
                    if new_master.room_id == Some(self.room_id) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   687
                        self.server.clients[new_master_id].set_is_master(true);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   688
                        let old_master_id = self.room().master_id;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   689
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   690
                        if let Some(master_id) = old_master_id {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   691
                            self.server.clients[master_id].set_is_master(false);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   692
                        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   693
                        self.room_mut().master_id = Some(new_master_id);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   694
                        Ok(ChangeMasterResult {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   695
                            old_master_id,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   696
                            new_master_id,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   697
                        })
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   698
                    } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   699
                        Err(ClientNotInRoom)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   700
                    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   701
                }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   702
                None => Err(NoClient),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   703
            }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   704
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   705
            Err(NoAccess)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   706
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   707
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   708
15547
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   709
    pub fn start_vote(&mut self, kind: VoteType) -> Result<(), StartVoteError> {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   710
        use StartVoteError::*;
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   711
        match self.room().voting {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   712
            Some(_) => Err(VotingInProgress),
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   713
            None => {
15548
24f692e791d3 disallow mutable rooms outside the server
alfadur <mail@none>
parents: 15547
diff changeset
   714
                let voting = Voting::new(kind, self.server.room_client_ids(self.room_id).collect());
15547
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   715
                self.room_mut().voting = Some(voting);
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   716
                Ok(())
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   717
            }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   718
        }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   719
    }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   720
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   721
    pub fn vote(&mut self, vote: Vote) -> Result<VoteResult, VoteError> {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   722
        use self::{VoteError::*, VoteResult::*};
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   723
        let client_id = self.client_id;
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   724
        if let Some(ref mut voting) = self.room_mut().voting {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   725
            if vote.is_forced || voting.votes.iter().all(|(id, _)| client_id != *id) {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   726
                voting.votes.push((client_id, vote.is_pro));
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   727
                let i = voting.votes.iter();
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   728
                let pro = i.clone().filter(|(_, v)| *v).count();
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   729
                let contra = i.filter(|(_, v)| !*v).count();
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   730
                let success_quota = voting.voters.len() / 2 + 1;
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   731
                if vote.is_forced && vote.is_pro || pro >= success_quota {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   732
                    let voting = self.room_mut().voting.take().unwrap();
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   733
                    Ok(Succeeded(voting.kind))
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   734
                } else if vote.is_forced && !vote.is_pro
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   735
                    || contra > voting.voters.len() - success_quota
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   736
                {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   737
                    Ok(Failed)
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   738
                } else {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   739
                    Ok(Submitted)
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   740
                }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   741
            } else {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   742
                Err(AlreadyVoted)
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   743
            }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   744
        } else {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   745
            Err(NoVoting)
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   746
        }
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   747
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   748
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   749
    pub fn toggle_flag(&mut self, flags: super::room::RoomFlags) -> bool {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   750
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   751
        if client.is_master() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   752
            room.flags.toggle(flags);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   753
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   754
        client.is_master()
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   755
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   756
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   757
    pub fn fix_room(&mut self) -> Result<(), AccessError> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   758
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   759
        if client.is_admin() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   760
            room.set_is_fixed(true);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   761
            room.set_join_restriction(false);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   762
            room.set_team_add_restriction(false);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   763
            room.set_unregistered_players_restriction(true);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   764
            Ok(())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   765
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   766
            Err(AccessError())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   767
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   768
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   769
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   770
    pub fn unfix_room(&mut self) -> Result<(), AccessError> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   771
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   772
        if client.is_admin() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   773
            room.set_is_fixed(false);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   774
            Ok(())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   775
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   776
            Err(AccessError())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   777
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   778
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   779
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   780
    pub fn set_room_name(&mut self, mut name: String) -> Result<String, ModifyRoomNameError> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   781
        use ModifyRoomNameError::*;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   782
        let room_exists = self.server.has_room(&name);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   783
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   784
        if room.is_fixed() || room.master_id != Some(client.id) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   785
            Err(AccessDenied)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   786
        } else if utils::is_name_illegal(&name) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   787
            Err(InvalidName)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   788
        } else if room_exists {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   789
            Err(DuplicateName)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   790
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   791
            std::mem::swap(&mut room.name, &mut name);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   792
            Ok(name)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   793
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   794
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   795
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   796
    pub fn set_room_greeting(&mut self, greeting: Option<String>) -> Result<(), AccessError> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   797
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   798
        if client.is_admin() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   799
            room.greeting = greeting.unwrap_or(String::new());
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   800
            Ok(())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   801
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   802
            Err(AccessError())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   803
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   804
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   805
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   806
    pub fn set_room_max_teams(&mut self, count: u8) -> Result<(), SetTeamCountError> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   807
        use SetTeamCountError::*;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   808
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   809
        if !client.is_master() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   810
            Err(NotMaster)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   811
        } else if !(2..=super::room::MAX_TEAMS_IN_ROOM).contains(&count) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   812
            Err(InvalidNumber)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   813
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   814
            room.max_teams = count;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   815
            Ok(())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   816
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   817
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   818
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   819
    pub fn set_team_hedgehogs_number(
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   820
        &mut self,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   821
        team_name: &str,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   822
        number: u8,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   823
    ) -> Result<(), SetHedgehogsError> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   824
        use SetHedgehogsError::*;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   825
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   826
        let addable_hedgehogs = room.addable_hedgehogs();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   827
        if let Some((_, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   828
            let max_hedgehogs = min(
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   829
                super::room::MAX_HEDGEHOGS_IN_ROOM,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   830
                addable_hedgehogs + team.hedgehogs_number,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   831
            );
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   832
            if !client.is_master() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   833
                Err(NotMaster)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   834
            } else if !(1..=max_hedgehogs).contains(&number) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   835
                Err(InvalidNumber(team.hedgehogs_number))
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   836
            } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   837
                team.hedgehogs_number = number;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   838
                Ok(())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   839
            }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   840
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   841
            Err(NoTeam)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   842
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   843
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   844
15547
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   845
    pub fn set_hedgehogs_number(&mut self, number: u8) -> Vec<String> {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   846
        self.room_mut().set_hedgehogs_number(number)
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   847
    }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   848
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   849
    pub fn add_team(&mut self, mut info: Box<TeamInfo>) -> Result<&TeamInfo, AddTeamError> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   850
        use AddTeamError::*;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   851
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   852
        if room.teams.len() >= room.max_teams as usize {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   853
            Err(TooManyTeams)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   854
        } else if room.addable_hedgehogs() == 0 {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   855
            Err(TooManyHedgehogs)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   856
        } else if room.find_team(|t| t.name == info.name) != None {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   857
            Err(TeamAlreadyExists)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   858
        } else if room.game_info.is_some() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   859
            Err(GameInProgress)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   860
        } else if room.is_team_add_restricted() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   861
            Err(Restricted)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   862
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   863
            info.owner = client.nick.clone();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   864
            let team = room.add_team(client.id, *info, client.protocol_number < 42);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   865
            client.teams_in_game += 1;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   866
            client.clan = Some(team.color);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   867
            Ok(team)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   868
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   869
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   870
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   871
    pub fn remove_team(&mut self, team_name: &str) -> Result<(), RemoveTeamError> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   872
        use RemoveTeamError::*;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   873
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   874
        match room.find_team_owner(team_name) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   875
            None => Err(NoTeam),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   876
            Some((id, _)) if id != client.id => Err(RemoveTeamError::TeamNotOwned),
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   877
            Some(_) => {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   878
                client.teams_in_game -= 1;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   879
                client.clan = room.find_team_color(client.id);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   880
                room.remove_team(team_name);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   881
                Ok(())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   882
            }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   883
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   884
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   885
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   886
    pub fn set_team_color(&mut self, team_name: &str, color: u8) -> Result<(), ModifyTeamError> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   887
        use ModifyTeamError::*;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   888
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   889
        if let Some((owner, team)) = room.find_team_and_owner_mut(|t| t.name == team_name) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   890
            if !client.is_master() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   891
                Err(NotMaster)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   892
            } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   893
                team.color = color;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   894
                self.server.clients[owner].clan = Some(color);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   895
                Ok(())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   896
            }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   897
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   898
            Err(NoTeam)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   899
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   900
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   901
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   902
    pub fn set_config(&mut self, cfg: GameCfg) -> Result<(), SetConfigError> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   903
        use SetConfigError::*;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   904
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   905
        if room.is_fixed() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   906
            Err(RoomFixed)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   907
        } else if !client.is_master() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   908
            Err(NotMaster)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   909
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   910
            let cfg = match cfg {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   911
                GameCfg::Scheme(name, mut values) => {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   912
                    if client.protocol_number == 49 && values.len() >= 2 {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   913
                        let mut s = "X".repeat(50);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   914
                        s.push_str(&values.pop().unwrap());
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   915
                        values.push(s);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   916
                    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   917
                    GameCfg::Scheme(name, values)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   918
                }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   919
                cfg => cfg,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   920
            };
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   921
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   922
            room.set_config(cfg);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   923
            Ok(())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   924
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   925
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   926
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   927
    pub fn save_config(&mut self, name: String, location: String) {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   928
        self.room_mut().save_config(name, location);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   929
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   930
15547
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   931
    pub fn load_config(&mut self, name: &str) -> Option<&str> {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   932
        self.room_mut().load_config(name)
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   933
    }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   934
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   935
    pub fn delete_config(&mut self, name: &str) -> bool {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   936
        self.room_mut().delete_config(name)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   937
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   938
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   939
    pub fn toggle_ready(&mut self) -> bool {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   940
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   941
        client.set_is_ready(!client.is_ready());
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   942
        if client.is_ready() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   943
            room.ready_players_number += 1;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   944
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   945
            room.ready_players_number -= 1;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   946
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   947
        client.is_ready()
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   948
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   949
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   950
    pub fn start_game(&mut self) -> Result<Vec<String>, StartGameError> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   951
        use StartGameError::*;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   952
        let (room_clients, room_nicks): (Vec<_>, Vec<_>) = self
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   953
            .server
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   954
            .clients
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   955
            .iter()
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   956
            .map(|(id, c)| (id, c.nick.clone()))
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   957
            .unzip();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   958
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   959
        let room = self.room_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   960
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   961
        if !room.has_multiple_clans() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   962
            Err(NotEnoughClans)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   963
        } else if room.protocol_number <= 43 && room.players_number != room.ready_players_number {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   964
            Err(NotReady)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   965
        } else if room.game_info.is_some() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   966
            Err(AlreadyInGame)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   967
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   968
            room.start_round();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   969
            for id in room_clients {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   970
                let team_indices = self.room().client_team_indices(id);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   971
                let c = &mut self.server.clients[id];
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   972
                c.set_is_in_game(true);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   973
                c.team_indices = team_indices;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   974
            }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   975
            Ok(room_nicks)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   976
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   977
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   978
15547
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   979
    pub fn toggle_pause(&mut self) -> bool {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   980
        if let Some(ref mut info) = self.room_mut().game_info {
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   981
            info.is_paused = !info.is_paused;
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   982
        }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   983
        self.room_mut().game_info.is_some()
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   984
    }
16d3c9acd715 restore voting
alfadur <mail@none>
parents: 15545
diff changeset
   985
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   986
    pub fn leave_game(&mut self) -> Option<Vec<String>> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   987
        let (client, room) = self.get_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   988
        let client_left = client.is_in_game();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   989
        if client_left {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   990
            client.set_is_in_game(false);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   991
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   992
            let team_names: Vec<_> = room
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   993
                .client_teams(client.id)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   994
                .map(|t| t.name.clone())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   995
                .collect();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   996
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   997
            if let Some(ref mut info) = room.game_info {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   998
                for team_name in &team_names {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
   999
                    let remove_msg =
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1000
                        utils::to_engine_msg(std::iter::once(b'F').chain(team_name.bytes()));
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1001
                    if let Some(m) = &info.sync_msg {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1002
                        info.msg_log.push(m.clone());
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1003
                    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1004
                    if info.sync_msg.is_some() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1005
                        info.sync_msg = None
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1006
                    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1007
                    info.msg_log.push(remove_msg);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1008
                }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1009
                Some(team_names)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1010
            } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1011
                unreachable!();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1012
            }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1013
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1014
            None
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1015
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1016
    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1017
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1018
    pub fn end_game(&mut self) -> Option<EndGameResult> {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1019
        let room = self.room_mut();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1020
        room.ready_players_number = room.master_id.is_some() as u8;
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1021
15563
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15562
diff changeset
  1022
        if let Some(mut info) = replace(&mut room.game_info, None) {
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1023
            let room_id = room.id;
15563
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15562
diff changeset
  1024
            for team_name in &info.left_teams {
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15562
diff changeset
  1025
                room.remove_team(team_name);
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15562
diff changeset
  1026
            }
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15562
diff changeset
  1027
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1028
            let joined_mid_game_clients = self
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1029
                .server
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1030
                .clients
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1031
                .iter()
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1032
                .filter(|(_, c)| c.room_id == Some(self.room_id) && c.is_joined_mid_game())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1033
                .map(|(_, c)| c.id)
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1034
                .collect();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1035
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1036
            let unreadied_nicks: Vec<_> = self
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1037
                .server
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1038
                .clients
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1039
                .iter_mut()
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1040
                .filter(|(_, c)| c.room_id == Some(room_id))
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1041
                .map(|(_, c)| {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1042
                    c.set_is_ready(c.is_master());
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1043
                    c.set_is_joined_mid_game(false);
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1044
                    c
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1045
                })
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1046
                .filter_map(|c| {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1047
                    if !c.is_master() {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1048
                        Some(c.nick.clone())
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1049
                    } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1050
                        None
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1051
                    }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1052
                })
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1053
                .collect();
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1054
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1055
            Some(EndGameResult {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1056
                joined_mid_game_clients,
15563
d122b65bdf6f reapply some simplifications
alfadur <mail@none>
parents: 15562
diff changeset
  1057
                left_teams: replace(&mut info.left_teams, vec![]),
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1058
                unreadied_nicks,
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1059
            })
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1060
        } else {
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1061
            None
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1062
        }
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1063
    }
15549
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15548
diff changeset
  1064
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15548
diff changeset
  1065
    pub fn log_engine_msg(&mut self, log_msg: String, sync_msg: Option<Option<String>>) {
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15548
diff changeset
  1066
        if let Some(ref mut info) = self.room_mut().game_info {
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15548
diff changeset
  1067
            if !log_msg.is_empty() {
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15548
diff changeset
  1068
                info.msg_log.push(log_msg);
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15548
diff changeset
  1069
            }
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15548
diff changeset
  1070
            if let Some(msg) = sync_msg {
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15548
diff changeset
  1071
                info.sync_msg = msg;
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15548
diff changeset
  1072
            }
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15548
diff changeset
  1073
        }
428a0e7da27b restore engine message handler
alfadur <mail@none>
parents: 15548
diff changeset
  1074
    }
15545
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1075
}
f4f6060b536c add a separate interface for modifying room state
alfadur <mail@none>
parents: 15544
diff changeset
  1076
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
  1077
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
  1078
    let entry = rooms.vacant_entry();
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
  1079
    let room = HwRoom::new(entry.key());
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1080
    entry.insert(room)
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1081
}
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1082
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
  1083
fn create_room<'a, 'b>(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
  1084
    client: &'a mut HwClient,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
  1085
    rooms: &'b mut Slab<HwRoom>,
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1086
    name: String,
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1087
    password: Option<String>,
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
  1088
) -> (&'a HwClient, &'b HwRoom) {
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1089
    let room = allocate_room(rooms);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1090
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1091
    room.master_id = Some(client.id);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1092
    room.name = name;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1093
    room.password = password;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1094
    room.protocol_number = client.protocol_number;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1095
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1096
    room.players_number = 1;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1097
    room.ready_players_number = 1;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1098
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1099
    client.room_id = Some(room.id);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1100
    client.set_is_master(true);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1101
    client.set_is_ready(true);
15555
0606f89698e7 check the room passwords
alfadur <mail@none>
parents: 15554
diff changeset
  1102
    client.set_is_in_game(false);
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1103
    client.set_is_joined_mid_game(false);
15555
0606f89698e7 check the room passwords
alfadur <mail@none>
parents: 15554
diff changeset
  1104
    client.clan = None;
0606f89698e7 check the room passwords
alfadur <mail@none>
parents: 15554
diff changeset
  1105
    client.teams_in_game = 0;
0606f89698e7 check the room passwords
alfadur <mail@none>
parents: 15554
diff changeset
  1106
    client.team_indices = vec![];
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1107
15463
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15197
diff changeset
  1108
    (client, room)
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1109
}
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1110
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
  1111
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
  1112
    debug_assert!(client.room_id != Some(room.id));
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1113
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1114
    room.players_number += 1;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1115
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1116
    client.room_id = Some(room.id);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1117
    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
  1118
    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
  1119
15562
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15558
diff changeset
  1120
    if let Some(ref mut info) = room.game_info {
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15558
diff changeset
  1121
        let teams = info.client_teams(client.id);
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15558
diff changeset
  1122
        client.teams_in_game = teams.clone().count() as u8;
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15558
diff changeset
  1123
        client.clan = teams.clone().next().map(|t| t.color);
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15558
diff changeset
  1124
        let team_names: Vec<_> = teams.map(|t| t.name.clone()).collect();
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1125
15562
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15558
diff changeset
  1126
        if !team_names.is_empty() {
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15558
diff changeset
  1127
            info.left_teams.retain(|name| !team_names.contains(&name));
14525
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1128
        }
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1129
    }
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14478
diff changeset
  1130
}