rust/hedgewars-server/src/server/client.rs
author alfadur <mail@none>
Sat, 02 Feb 2019 15:06:39 +0300
changeset 14676 455865ccd36c
parent 14462 98ef2913ec73
child 14698 6a2e13e36b7f
permissions -rw-r--r--
Server action refactoring part 2 of N
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13483
d79795acaa73 Mostly implement voting
alfadur
parents: 13482
diff changeset
     1
use super::coretypes::ClientId;
13810
0463a4221327 cleanup crate imports
alfadur
parents: 13803
diff changeset
     2
use bitflags::*;
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
     3
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
     4
bitflags! {
13527
282e5e54386f Something down in the food chain already uses bitflags, so might as well switch to them
alfadur
parents: 13525
diff changeset
     5
    pub struct ClientFlags: u8 {
282e5e54386f Something down in the food chain already uses bitflags, so might as well switch to them
alfadur
parents: 13525
diff changeset
     6
        const IS_ADMIN = 0b0000_0001;
282e5e54386f Something down in the food chain already uses bitflags, so might as well switch to them
alfadur
parents: 13525
diff changeset
     7
        const IS_MASTER = 0b0000_0010;
282e5e54386f Something down in the food chain already uses bitflags, so might as well switch to them
alfadur
parents: 13525
diff changeset
     8
        const IS_READY = 0b0000_0100;
282e5e54386f Something down in the food chain already uses bitflags, so might as well switch to them
alfadur
parents: 13525
diff changeset
     9
        const IS_IN_GAME = 0b0000_1000;
282e5e54386f Something down in the food chain already uses bitflags, so might as well switch to them
alfadur
parents: 13525
diff changeset
    10
        const IS_JOINED_MID_GAME = 0b0001_0000;
13803
4664da990556 Add official server feature to cargo
alfadur
parents: 13534
diff changeset
    11
        const IS_CHECKER = 0b0010_0000;
13534
662f7df89d06 Implement room config export
alfadur
parents: 13527
diff changeset
    12
662f7df89d06 Implement room config export
alfadur
parents: 13527
diff changeset
    13
        const NONE = 0b0000_0000;
662f7df89d06 Implement room config export
alfadur
parents: 13527
diff changeset
    14
        const DEFAULT = Self::NONE.bits;
13527
282e5e54386f Something down in the food chain already uses bitflags, so might as well switch to them
alfadur
parents: 13525
diff changeset
    15
    }
282e5e54386f Something down in the food chain already uses bitflags, so might as well switch to them
alfadur
parents: 13525
diff changeset
    16
}
13525
1ee192f13456 Better packing for clients
alfadur
parents: 13483
diff changeset
    17
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    18
pub struct HWClient {
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12858
diff changeset
    19
    pub id: ClientId,
12857
bd35cb2302b3 Quick dirty fix for building
unc0rr
parents: 12152
diff changeset
    20
    pub room_id: Option<usize>,
12146
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    21
    pub nick: String,
13803
4664da990556 Add official server feature to cargo
alfadur
parents: 13534
diff changeset
    22
    pub web_password: String,
4664da990556 Add official server feature to cargo
alfadur
parents: 13534
diff changeset
    23
    pub server_salt: String,
13525
1ee192f13456 Better packing for clients
alfadur
parents: 13483
diff changeset
    24
    pub protocol_number: u16,
13527
282e5e54386f Something down in the food chain already uses bitflags, so might as well switch to them
alfadur
parents: 13525
diff changeset
    25
    pub flags: ClientFlags,
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13124
diff changeset
    26
    pub teams_in_game: u8,
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13424
diff changeset
    27
    pub team_indices: Vec<u8>,
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    28
    pub clan: Option<u8>,
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    29
}
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    30
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    31
impl HWClient {
13803
4664da990556 Add official server feature to cargo
alfadur
parents: 13534
diff changeset
    32
    pub fn new(id: ClientId, salt: String) -> HWClient {
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    33
        HWClient {
13124
1e39b8749072 separated the server logic from all the async io mess.
alfadur
parents: 12858
diff changeset
    34
            id,
12151
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12149
diff changeset
    35
            room_id: None,
12146
78925eff02c2 Basic support for NICK message
unc0rr
parents: 12144
diff changeset
    36
            nick: String::new(),
13803
4664da990556 Add official server feature to cargo
alfadur
parents: 13534
diff changeset
    37
            web_password: String::new(),
4664da990556 Add official server feature to cargo
alfadur
parents: 13534
diff changeset
    38
            server_salt: salt,
12151
8d8fb85bc09c SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents: 12149
diff changeset
    39
            protocol_number: 0,
13534
662f7df89d06 Implement room config export
alfadur
parents: 13527
diff changeset
    40
            flags: ClientFlags::DEFAULT,
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13124
diff changeset
    41
            teams_in_game: 0,
13428
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13424
diff changeset
    42
            team_indices: Vec::new(),
13424
81e0ed105f5d implementation of team related messages
alfadur
parents: 13124
diff changeset
    43
            clan: None,
12133
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    44
        }
f50876f3eff8 Refactor modules layout
unc0rr
parents:
diff changeset
    45
    }
13525
1ee192f13456 Better packing for clients
alfadur
parents: 13483
diff changeset
    46
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    47
    fn contains(&self, mask: ClientFlags) -> bool {
13527
282e5e54386f Something down in the food chain already uses bitflags, so might as well switch to them
alfadur
parents: 13525
diff changeset
    48
        self.flags.contains(mask)
282e5e54386f Something down in the food chain already uses bitflags, so might as well switch to them
alfadur
parents: 13525
diff changeset
    49
    }
282e5e54386f Something down in the food chain already uses bitflags, so might as well switch to them
alfadur
parents: 13525
diff changeset
    50
282e5e54386f Something down in the food chain already uses bitflags, so might as well switch to them
alfadur
parents: 13525
diff changeset
    51
    fn set(&mut self, mask: ClientFlags, value: bool) {
282e5e54386f Something down in the food chain already uses bitflags, so might as well switch to them
alfadur
parents: 13525
diff changeset
    52
        self.flags.set(mask, value);
13525
1ee192f13456 Better packing for clients
alfadur
parents: 13483
diff changeset
    53
    }
1ee192f13456 Better packing for clients
alfadur
parents: 13483
diff changeset
    54
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    55
    pub fn is_admin(&self) -> bool {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    56
        self.contains(ClientFlags::IS_ADMIN)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    57
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    58
    pub fn is_master(&self) -> bool {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    59
        self.contains(ClientFlags::IS_MASTER)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    60
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    61
    pub fn is_ready(&self) -> bool {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    62
        self.contains(ClientFlags::IS_READY)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    63
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    64
    pub fn is_in_game(&self) -> bool {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    65
        self.contains(ClientFlags::IS_IN_GAME)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    66
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    67
    pub fn is_joined_mid_game(&self) -> bool {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    68
        self.contains(ClientFlags::IS_JOINED_MID_GAME)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    69
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    70
    pub fn is_checker(&self) -> bool {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    71
        self.contains(ClientFlags::IS_CHECKER)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    72
    }
13525
1ee192f13456 Better packing for clients
alfadur
parents: 13483
diff changeset
    73
14462
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    74
    pub fn set_is_admin(&mut self, value: bool) {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    75
        self.set(ClientFlags::IS_ADMIN, value)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    76
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    77
    pub fn set_is_master(&mut self, value: bool) {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    78
        self.set(ClientFlags::IS_MASTER, value)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    79
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    80
    pub fn set_is_ready(&mut self, value: bool) {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    81
        self.set(ClientFlags::IS_READY, value)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    82
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    83
    pub fn set_is_in_game(&mut self, value: bool) {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    84
        self.set(ClientFlags::IS_IN_GAME, value)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    85
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    86
    pub fn set_is_joined_mid_game(&mut self, value: bool) {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    87
        self.set(ClientFlags::IS_JOINED_MID_GAME, value)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    88
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    89
    pub fn set_is_checker(&mut self, value: bool) {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    90
        self.set(ClientFlags::IS_CHECKER, value)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    91
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14420
diff changeset
    92
}