rust/hedgewars-server/src/handlers/actions.rs
author Wuzzy <Wuzzy2@mail.ru>
Mon, 06 Jul 2020 12:46:22 +0200
changeset 15687 326b024a24e3
parent 15543 02f648f7cbe1
child 15804 747278149393
permissions -rw-r--r--
Fix typo
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
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
    15
#[derive(Clone)]
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    16
pub enum DestinationGroup {
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    17
    All,
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    18
    Lobby,
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    19
    Room(RoomId),
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    20
    Protocol(u16),
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    21
}
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    22
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
    23
#[derive(Clone)]
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    24
pub enum Destination {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    25
    ToId(ClientId),
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    26
    ToIds(Vec<ClientId>),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    27
    ToSelf,
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    28
    ToAll {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    29
        group: DestinationGroup,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    30
        skip_self: bool,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    31
    },
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    32
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    33
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    34
pub struct PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    35
    pub destination: Destination,
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    36
    pub message: HwServerMessage,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    37
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    38
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    39
impl PendingMessage {
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    40
    pub fn send(message: HwServerMessage, client_id: ClientId) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    41
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    42
            destination: Destination::ToId(client_id),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    43
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    44
        }
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    45
    }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    46
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    47
    pub fn send_many(message: HwServerMessage, client_ids: Vec<ClientId>) -> PendingMessage {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    48
        PendingMessage {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    49
            destination: Destination::ToIds(client_ids),
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    50
            message,
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    51
        }
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    52
    }
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    53
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    54
    pub fn send_self(message: HwServerMessage) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    55
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    56
            destination: Destination::ToSelf,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    57
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    58
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    59
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    60
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    61
    pub fn send_all(message: HwServerMessage) -> PendingMessage {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    62
        let destination = Destination::ToAll {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    63
            group: DestinationGroup::All,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    64
            skip_self: false,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    65
        };
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    66
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    67
            destination,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    68
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    69
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    70
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    71
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    72
    pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    73
        if let Destination::ToAll { ref mut group, .. } = self.destination {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    74
            *group = DestinationGroup::Room(clients_room_id)
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    75
        }
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    76
        self
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    77
    }
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    78
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    79
    pub fn in_lobby(mut self) -> PendingMessage {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    80
        if let Destination::ToAll { ref mut group, .. } = self.destination {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    81
            *group = DestinationGroup::Lobby
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    82
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    83
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    84
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    85
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
    86
    pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    87
        if let Destination::ToAll { ref mut group, .. } = self.destination {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    88
            *group = DestinationGroup::Protocol(protocol_number)
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    89
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    90
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    91
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    92
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    93
    pub fn but_self(mut self) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    94
        if let Destination::ToAll {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    95
            ref mut skip_self, ..
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    96
        } = self.destination
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    97
        {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    98
            *skip_self = true
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    99
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   100
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   101
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   102
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   103
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
   104
impl HwServerMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   105
    pub fn send(self, client_id: ClientId) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   106
        PendingMessage::send(self, client_id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   107
    }
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
   108
    pub fn send_many(self, client_ids: Vec<ClientId>) -> PendingMessage {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
   109
        PendingMessage::send_many(self, client_ids)
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
   110
    }
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   111
    pub fn send_self(self) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   112
        PendingMessage::send_self(self)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   113
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   114
    pub fn send_all(self) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   115
        PendingMessage::send_all(self)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   116
    }
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
   117
    pub fn send_to_destination(self, destination: Destination) -> PendingMessage {
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
   118
        PendingMessage {
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
   119
            destination,
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
   120
            message: self,
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
   121
        }
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
   122
    }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   123
}