rust/hedgewars-server/src/handlers/actions.rs
author alfadur <mail@none>
Sat, 21 Dec 2019 00:26:17 +0300
changeset 15518 e705d30e0f10
parent 15075 e935b1ad23f3
child 15543 02f648f7cbe1
permissions -rw-r--r--
some more room handler cleanup
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
     1
use crate::{
15074
c5a6e8566425 shuffle server files
alfadur
parents: 14788
diff changeset
     2
    core::{
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     3
        client::HwClient,
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     4
        room::HwRoom,
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     5
        room::{GameInfo, RoomFlags},
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     6
        server::HwServer,
15074
c5a6e8566425 shuffle server files
alfadur
parents: 14788
diff changeset
     7
        types::{ClientId, GameCfg, RoomId, VoteType},
c5a6e8566425 shuffle server files
alfadur
parents: 14788
diff changeset
     8
    },
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
     9
    protocol::messages::{server_chat, HwProtocolMessage, HwServerMessage, HwServerMessage::*},
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    10
    utils::to_engine_msg,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    11
};
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    12
use rand::{distributions::Uniform, thread_rng, Rng};
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    13
use std::{io, io::Write, iter::once, mem::replace};
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    14
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    15
pub enum DestinationGroup {
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    16
    All,
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    17
    Lobby,
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    18
    Room(RoomId),
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    19
    Protocol(u16),
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    20
}
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    21
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    22
pub enum Destination {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    23
    ToId(ClientId),
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    24
    ToIds(Vec<ClientId>),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    25
    ToSelf,
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    26
    ToAll {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    27
        group: DestinationGroup,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    28
        skip_self: bool,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    29
    },
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    30
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    31
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    32
pub struct PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    33
    pub destination: Destination,
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    34
    pub message: HwServerMessage,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    35
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    36
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    37
impl PendingMessage {
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    38
    pub fn send(message: HwServerMessage, client_id: ClientId) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    39
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    40
            destination: Destination::ToId(client_id),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    41
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    42
        }
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    43
    }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    44
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    45
    pub fn send_many(message: HwServerMessage, client_ids: Vec<ClientId>) -> PendingMessage {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    46
        PendingMessage {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    47
            destination: Destination::ToIds(client_ids),
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    48
            message,
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    49
        }
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    50
    }
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    51
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    52
    pub fn send_self(message: HwServerMessage) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    53
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    54
            destination: Destination::ToSelf,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    55
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    56
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    57
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    58
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    59
    pub fn send_all(message: HwServerMessage) -> PendingMessage {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    60
        let destination = Destination::ToAll {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    61
            group: DestinationGroup::All,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    62
            skip_self: false,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    63
        };
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    64
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    65
            destination,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    66
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    67
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    68
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    69
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    70
    pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    71
        if let Destination::ToAll { ref mut group, .. } = self.destination {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    72
            *group = DestinationGroup::Room(clients_room_id)
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    73
        }
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    74
        self
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    75
    }
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    76
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    77
    pub fn in_lobby(mut self) -> PendingMessage {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    78
        if let Destination::ToAll { ref mut group, .. } = self.destination {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    79
            *group = DestinationGroup::Lobby
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    80
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    81
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    82
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    83
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
    84
    pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    85
        if let Destination::ToAll { ref mut group, .. } = self.destination {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    86
            *group = DestinationGroup::Protocol(protocol_number)
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    87
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    88
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    89
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    90
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    91
    pub fn but_self(mut self) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    92
        if let Destination::ToAll {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    93
            ref mut skip_self, ..
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    94
        } = self.destination
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    95
        {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    96
            *skip_self = true
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    97
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    98
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    99
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   100
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   101
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   102
impl HwServerMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   103
    pub fn send(self, client_id: ClientId) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   104
        PendingMessage::send(self, client_id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   105
    }
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
   106
    pub fn send_many(self, client_ids: Vec<ClientId>) -> PendingMessage {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
   107
        PendingMessage::send_many(self, client_ids)
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
   108
    }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   109
    pub fn send_self(self) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   110
        PendingMessage::send_self(self)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   111
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   112
    pub fn send_all(self) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   113
        PendingMessage::send_all(self)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   114
    }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   115
}