rust/hedgewars-server/src/core/server.rs
author sheepluva
Sun, 08 Dec 2019 02:09:19 +0100
branchhedgeroid
changeset 15520 de08a3ae4f8d
parent 15446 61a0bd0bb021
child 15487 4cc9ec732392
permissions -rw-r--r--
hedgeroid/frontlib: update proto and weapons number
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     1
use super::{
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
     2
    client::HwClient,
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
     3
    indexslab::IndexSlab,
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
     4
    room::HwRoom,
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
     5
    types::{ClientId, RoomId, ServerVar},
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
     6
};
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
     7
use crate::{protocol::messages::HwProtocolMessage::Greeting, utils};
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
     8
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
     9
use crate::core::server::JoinRoomError::WrongProtocol;
14791
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
    10
use bitflags::*;
13810
0463a4221327 cleanup crate imports
alfadur
parents: 13803
diff changeset
    11
use log::*;
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    12
use slab;
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    13
use std::{borrow::BorrowMut, collections::HashSet, iter, num::NonZeroU16};
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
    14
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12153
diff changeset
    15
type Slab<T> = slab::Slab<T>;
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
    16
15446
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
    17
#[derive(Debug)]
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    18
pub enum CreateRoomError {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    19
    InvalidName,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    20
    AlreadyExists,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    21
}
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    22
15446
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
    23
#[derive(Debug)]
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    24
pub enum JoinRoomError {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    25
    DoesntExist,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    26
    WrongProtocol,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    27
    Full,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    28
    Restricted,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    29
}
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    30
15446
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
    31
#[derive(Debug)]
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
    32
pub struct UninitializedError();
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
    33
#[derive(Debug)]
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    34
pub struct AccessError();
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    35
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    36
pub struct HwAnteClient {
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    37
    pub nick: Option<String>,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    38
    pub protocol_number: Option<NonZeroU16>,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    39
    pub server_salt: String,
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
    40
    pub is_checker: bool,
15181
f6115638aa92 enable local admins for non-official servers
alfadur
parents: 15080
diff changeset
    41
    pub is_local_admin: bool,
15446
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
    42
    pub is_registered: bool,
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
    43
    pub is_admin: bool,
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
    44
    pub is_contributor: bool,
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    45
}
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    46
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    47
pub struct HwAnteroom {
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    48
    pub clients: IndexSlab<HwAnteClient>,
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    49
}
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    50
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    51
impl HwAnteroom {
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    52
    pub fn new(clients_limit: usize) -> Self {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    53
        let clients = IndexSlab::with_capacity(clients_limit);
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    54
        HwAnteroom { clients }
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    55
    }
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    56
15181
f6115638aa92 enable local admins for non-official servers
alfadur
parents: 15080
diff changeset
    57
    pub fn add_client(&mut self, client_id: ClientId, salt: String, is_local_admin: bool) {
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    58
        let client = HwAnteClient {
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    59
            nick: None,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    60
            protocol_number: None,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    61
            server_salt: salt,
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
    62
            is_checker: false,
15181
f6115638aa92 enable local admins for non-official servers
alfadur
parents: 15080
diff changeset
    63
            is_local_admin,
15446
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
    64
            is_registered: false,
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
    65
            is_admin: false,
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
    66
            is_contributor: false,
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    67
        };
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    68
        self.clients.insert(client_id, client);
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    69
    }
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    70
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    71
    pub fn remove_client(&mut self, client_id: ClientId) -> Option<HwAnteClient> {
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
    72
        let client = self.clients.remove(client_id);
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    73
        client
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    74
    }
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    75
}
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
    76
14788
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
    77
pub struct ServerGreetings {
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
    78
    pub for_latest_protocol: String,
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
    79
    pub for_old_protocols: String,
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
    80
}
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
    81
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
    82
impl ServerGreetings {
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
    83
    fn new() -> Self {
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
    84
        Self {
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
    85
            for_latest_protocol: "\u{1f994} is watching".to_string(),
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
    86
            for_old_protocols: "\u{1f994} is watching".to_string(),
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
    87
        }
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
    88
    }
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
    89
}
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
    90
14791
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
    91
bitflags! {
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
    92
    pub struct ServerFlags: u8 {
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
    93
        const REGISTERED_ONLY = 0b0000_1000;
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
    94
    }
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
    95
}
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
    96
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    97
pub struct HwServer {
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    98
    pub clients: IndexSlab<HwClient>,
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
    99
    pub rooms: Slab<HwRoom>,
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   100
    pub anteroom: HwAnteroom,
14788
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
   101
    pub latest_protocol: u16,
14791
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
   102
    pub flags: ServerFlags,
14788
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
   103
    pub greetings: ServerGreetings,
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   104
}
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   105
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   106
impl HwServer {
14785
65861ba8b4e8 move room saves to IO thread
alfadur
parents: 14784
diff changeset
   107
    pub fn new(clients_limit: usize, rooms_limit: usize) -> Self {
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
   108
        let rooms = Slab::with_capacity(rooms_limit);
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
   109
        let clients = IndexSlab::with_capacity(clients_limit);
14699
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14698
diff changeset
   110
        Self {
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   111
            clients,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   112
            rooms,
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   113
            anteroom: HwAnteroom::new(clients_limit),
14788
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
   114
            greetings: ServerGreetings::new(),
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
   115
            latest_protocol: 58,
14791
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
   116
            flags: ServerFlags::empty(),
14699
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14698
diff changeset
   117
        }
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   118
    }
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   119
15446
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   120
    #[inline]
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   121
    pub fn client(&self, client_id: ClientId) -> &HwClient {
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   122
        &self.clients[client_id]
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   123
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   124
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   125
    #[inline]
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   126
    pub fn client_mut(&mut self, client_id: ClientId) -> &mut HwClient {
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   127
        &mut self.clients[client_id]
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   128
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   129
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   130
    #[inline]
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   131
    pub fn room(&self, room_id: RoomId) -> &HwRoom {
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   132
        &self.rooms[room_id]
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   133
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   134
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   135
    #[inline]
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   136
    pub fn room_mut(&mut self, room_id: RoomId) -> &mut HwRoom {
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   137
        &mut self.rooms[room_id]
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   138
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   139
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   140
    #[inline]
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   141
    pub fn is_admin(&self, client_id: ClientId) -> bool {
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   142
        self.clients
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   143
            .get(client_id)
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   144
            .map(|c| c.is_admin())
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   145
            .unwrap_or(false)
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   146
    }
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   147
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   148
    pub fn add_client(&mut self, client_id: ClientId, data: HwAnteClient) {
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
   149
        if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) {
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   150
            let mut client = HwClient::new(client_id, protocol.get(), nick);
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
   151
            client.set_is_checker(data.is_checker);
15181
f6115638aa92 enable local admins for non-official servers
alfadur
parents: 15080
diff changeset
   152
            #[cfg(not(feature = "official-server"))]
f6115638aa92 enable local admins for non-official servers
alfadur
parents: 15080
diff changeset
   153
            client.set_is_admin(data.is_local_admin);
f6115638aa92 enable local admins for non-official servers
alfadur
parents: 15080
diff changeset
   154
15446
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   155
            #[cfg(feature = "official-server")]
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   156
            {
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   157
                client.set_is_registered(info.is_registered);
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   158
                client.set_is_admin(info.is_admin);
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   159
                client.set_is_contributor(info.is_contributor);
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   160
            }
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   161
14698
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14691
diff changeset
   162
            self.clients.insert(client_id, client);
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12857
diff changeset
   163
        }
12131
4348997e502b Refactor code to add more structure to it
unc0rr
parents:
diff changeset
   164
    }
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
   165
14678
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14677
diff changeset
   166
    pub fn remove_client(&mut self, client_id: ClientId) {
14701
8a45c90f4580 fix client removal
alfadur
parents: 14700
diff changeset
   167
        self.clients.remove(client_id);
12132
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
   168
    }
36ac9c075d0d - Use netbuf buffers for client connection stream
unc0rr
parents: 12131
diff changeset
   169
15446
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   170
    pub fn get_greetings(&self, client: &HwClient) -> &str {
61a0bd0bb021 refactor root handler
alfadur
parents: 15444
diff changeset
   171
        if client.protocol_number < self.latest_protocol {
14788
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
   172
            &self.greetings.for_old_protocols
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
   173
        } else {
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
   174
            &self.greetings.for_latest_protocol
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
   175
        }
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
   176
    }
b3adc030104b implement server vars
alfadur
parents: 14786
diff changeset
   177
14509
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   178
    #[inline]
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   179
    pub fn get_client_nick(&self, client_id: ClientId) -> &str {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   180
        &self.clients[client_id].nick
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   181
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   182
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   183
    #[inline]
14509
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   184
    pub fn create_room(
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   185
        &mut self,
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   186
        creator_id: ClientId,
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   187
        name: String,
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   188
        password: Option<String>,
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   189
    ) -> Result<(&HwClient, &HwRoom), CreateRoomError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   190
        use CreateRoomError::*;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   191
        if utils::is_name_illegal(&name) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   192
            Err(InvalidName)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   193
        } else if self.has_room(&name) {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   194
            Err(AlreadyExists)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   195
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   196
            Ok(create_room(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   197
                &mut self.clients[creator_id],
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   198
                &mut self.rooms,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   199
                name,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   200
                password,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   201
            ))
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   202
        }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   203
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   204
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   205
    pub fn join_room(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   206
        &mut self,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   207
        client_id: ClientId,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   208
        room_id: RoomId,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   209
    ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   210
        use JoinRoomError::*;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   211
        let room = &mut self.rooms[room_id];
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   212
        let client = &mut self.clients[client_id];
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   213
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   214
        if client.protocol_number != room.protocol_number {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   215
            Err(WrongProtocol)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   216
        } else if room.is_join_restricted() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   217
            Err(Restricted)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   218
        } else if room.players_number == u8::max_value() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   219
            Err(Full)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   220
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   221
            move_to_room(client, room);
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   222
            let room_id = room.id;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   223
            Ok((
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   224
                &self.clients[client_id],
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   225
                &self.rooms[room_id],
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   226
                self.clients.iter().map(|(_, c)| c),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   227
            ))
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   228
        }
14509
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   229
    }
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   230
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14509
diff changeset
   231
    #[inline]
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   232
    pub fn join_room_by_name(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   233
        &mut self,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   234
        client_id: ClientId,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   235
        room_name: &str,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   236
    ) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   237
        use JoinRoomError::*;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   238
        let room = self.rooms.iter().find(|(_, r)| r.name == room_name);
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   239
        if let Some((_, room)) = room {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   240
            let room_id = room.id;
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   241
            self.join_room(client_id, room_id)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   242
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   243
            Err(DoesntExist)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   244
        }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   245
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   246
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   247
    #[inline]
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   248
    pub fn set_var(&mut self, client_id: ClientId, var: ServerVar) -> Result<(), AccessError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   249
        if self.clients[client_id].is_admin() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   250
            match var {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   251
                ServerVar::MOTDNew(msg) => self.greetings.for_latest_protocol = msg,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   252
                ServerVar::MOTDOld(msg) => self.greetings.for_old_protocols = msg,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   253
                ServerVar::LatestProto(n) => self.latest_protocol = n,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   254
            }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   255
            Ok(())
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   256
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   257
            Err(AccessError())
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   258
        }
14676
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14509
diff changeset
   259
    }
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14509
diff changeset
   260
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   261
    #[inline]
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   262
    pub fn get_vars(&self, client_id: ClientId) -> Result<[ServerVar; 3], AccessError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   263
        if self.clients[client_id].is_admin() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   264
            Ok([
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   265
                ServerVar::MOTDNew(self.greetings.for_latest_protocol.clone()),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   266
                ServerVar::MOTDOld(self.greetings.for_old_protocols.clone()),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   267
                ServerVar::LatestProto(self.latest_protocol),
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   268
            ])
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   269
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   270
            Err(AccessError())
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   271
        }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   272
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   273
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   274
    pub fn get_used_protocols(&self, client_id: ClientId) -> Result<Vec<u16>, AccessError> {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   275
        if self.clients[client_id].is_admin() {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   276
            let mut protocols: HashSet<_> = self
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   277
                .clients
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   278
                .iter()
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   279
                .map(|(_, c)| c.protocol_number)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   280
                .chain(self.rooms.iter().map(|(_, r)| r.protocol_number))
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   281
                .collect();
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   282
            let mut protocols: Vec<_> = protocols.drain().collect();
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   283
            protocols.sort();
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   284
            Ok(protocols)
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   285
        } else {
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   286
            Err(AccessError())
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   287
        }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   288
    }
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   289
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   290
    #[inline]
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   291
    pub fn has_room(&self, name: &str) -> bool {
14702
f64e21f164a5 fix some warnings
alfadur
parents: 14701
diff changeset
   292
        self.find_room(name).is_some()
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   293
    }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   294
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   295
    #[inline]
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   296
    pub fn find_room(&self, name: &str) -> Option<&HwRoom> {
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   297
        self.rooms
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   298
            .iter()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   299
            .find_map(|(_, r)| Some(r).filter(|r| r.name == name))
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   300
    }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   301
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   302
    pub fn find_room_mut(&mut self, name: &str) -> Option<&mut HwRoom> {
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   303
        self.rooms
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   304
            .iter_mut()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   305
            .find_map(|(_, r)| Some(r).filter(|r| r.name == name))
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   306
    }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   307
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   308
    pub fn find_client(&self, nick: &str) -> Option<&HwClient> {
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   309
        self.clients
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   310
            .iter()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   311
            .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick))
13483
d79795acaa73 Mostly implement voting
alfadur
parents: 13450
diff changeset
   312
    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13450
diff changeset
   313
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   314
    pub fn find_client_mut(&mut self, nick: &str) -> Option<&mut HwClient> {
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   315
        self.clients
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   316
            .iter_mut()
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   317
            .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick))
13483
d79795acaa73 Mostly implement voting
alfadur
parents: 13450
diff changeset
   318
    }
d79795acaa73 Mostly implement voting
alfadur
parents: 13450
diff changeset
   319
14794
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   320
    pub fn all_clients(&self) -> impl Iterator<Item = ClientId> + '_ {
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   321
        self.clients.iter().map(|(id, _)| id)
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   322
    }
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   323
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   324
    pub fn filter_clients<'a, F>(&'a self, f: F) -> impl Iterator<Item = ClientId> + 'a
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   325
    where
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   326
        F: Fn(&(usize, &HwClient)) -> bool + 'a,
14794
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   327
    {
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   328
        self.clients.iter().filter(f).map(|(_, c)| c.id)
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   329
    }
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   330
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   331
    pub fn filter_rooms<'a, F>(&'a self, f: F) -> impl Iterator<Item = RoomId> + 'a
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   332
    where
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   333
        F: Fn(&(usize, &HwRoom)) -> bool + 'a,
14794
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   334
    {
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   335
        self.rooms.iter().filter(f).map(|(_, c)| c.id)
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   336
    }
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   337
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
   338
    pub fn collect_clients<F>(&self, f: F) -> Vec<ClientId>
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   339
    where
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   340
        F: Fn(&(usize, &HwClient)) -> bool,
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
   341
    {
14794
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   342
        self.filter_clients(f).collect()
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   343
    }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   344
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
   345
    pub fn collect_nicks<F>(&self, f: F) -> Vec<String>
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
   346
    where
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   347
        F: Fn(&(usize, &HwClient)) -> bool,
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
   348
    {
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
   349
        self.clients
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
   350
            .iter()
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
   351
            .filter(f)
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
   352
            .map(|(_, c)| c.nick.clone())
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
   353
            .collect()
14699
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14698
diff changeset
   354
    }
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14698
diff changeset
   355
14794
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   356
    pub fn lobby_clients(&self) -> impl Iterator<Item = ClientId> + '_ {
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   357
        self.filter_clients(|(_, c)| c.room_id == None)
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
   358
    }
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
   359
14794
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   360
    pub fn room_clients(&self, room_id: RoomId) -> impl Iterator<Item = ClientId> + '_ {
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   361
        self.filter_clients(move |(_, c)| c.room_id == Some(room_id))
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   362
    }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   363
14794
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   364
    pub fn protocol_clients(&self, protocol: u16) -> impl Iterator<Item = ClientId> + '_ {
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   365
        self.filter_clients(move |(_, c)| c.protocol_number == protocol)
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   366
    }
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   367
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   368
    pub fn protocol_rooms(&self, protocol: u16) -> impl Iterator<Item = RoomId> + '_ {
18240b308505 implement stats message
alfadur
parents: 14791
diff changeset
   369
        self.filter_rooms(move |(_, r)| r.protocol_number == protocol)
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   370
    }
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   371
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   372
    pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> {
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   373
        let room_id = self.clients[self_id].room_id;
14786
01f8ab45f806 fix lobby joining
alfadur
parents: 14785
diff changeset
   374
        self.collect_clients(|(id, c)| *id != self_id && c.room_id == room_id)
13421
cdf69667593b partial room implementation
alfadur
parents: 13124
diff changeset
   375
    }
14791
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
   376
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
   377
    pub fn is_registered_only(&self) -> bool {
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
   378
        self.flags.contains(ServerFlags::REGISTERED_ONLY)
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
   379
    }
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
   380
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
   381
    pub fn set_is_registered_only(&mut self, value: bool) {
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
   382
        self.flags.set(ServerFlags::REGISTERED_ONLY, value)
8ecdb5c6bb2a implement info, registered only & super power messages
alfadur
parents: 14788
diff changeset
   383
    }
13450
d3c86ade3d4d Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents: 13447
diff changeset
   384
}
14509
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   385
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   386
fn allocate_room(rooms: &mut Slab<HwRoom>) -> &mut HwRoom {
14509
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   387
    let entry = rooms.vacant_entry();
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   388
    let room = HwRoom::new(entry.key());
14509
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   389
    entry.insert(room)
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   390
}
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   391
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   392
fn create_room<'a, 'b>(
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   393
    client: &'a mut HwClient,
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   394
    rooms: &'b mut Slab<HwRoom>,
14509
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   395
    name: String,
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   396
    password: Option<String>,
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   397
) -> (&'a HwClient, &'b HwRoom) {
14509
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   398
    let room = allocate_room(rooms);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   399
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   400
    room.master_id = Some(client.id);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   401
    room.name = name;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   402
    room.password = password;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   403
    room.protocol_number = client.protocol_number;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   404
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   405
    room.players_number = 1;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   406
    room.ready_players_number = 1;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   407
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   408
    client.room_id = Some(room.id);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   409
    client.set_is_master(true);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   410
    client.set_is_ready(true);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   411
    client.set_is_joined_mid_game(false);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   412
15444
a158ff8f84ef refactor the lobby handler
alfadur
parents: 15181
diff changeset
   413
    (client, room)
14509
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   414
}
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   415
15080
e935b1ad23f3 normalize type names
alfadur
parents: 15079
diff changeset
   416
fn move_to_room(client: &mut HwClient, room: &mut HwRoom) {
14509
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   417
    debug_assert!(client.room_id != Some(room.id));
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   418
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   419
    room.players_number += 1;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   420
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   421
    client.room_id = Some(room.id);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   422
    client.set_is_joined_mid_game(room.game_info.is_some());
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   423
    client.set_is_in_game(room.game_info.is_some());
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   424
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   425
    if let Some(ref mut info) = room.game_info {
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   426
        let teams = info.client_teams(client.id);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   427
        client.teams_in_game = teams.clone().count() as u8;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   428
        client.clan = teams.clone().next().map(|t| t.color);
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   429
        let team_names: Vec<_> = teams.map(|t| t.name.clone()).collect();
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   430
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   431
        if !team_names.is_empty() {
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   432
            info.left_teams.retain(|name| !team_names.contains(&name));
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   433
            info.teams_in_game += team_names.len() as u8;
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   434
            room.teams = info
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   435
                .teams_at_start
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   436
                .iter()
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   437
                .filter(|(_, t)| !team_names.contains(&t.name))
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   438
                .cloned()
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   439
                .collect();
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   440
        }
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   441
    }
6cc0fce249f9 Server action refactoring part 1 of N
alfadur <mail@none>
parents: 14462
diff changeset
   442
}