rust/hedgewars-server/src/handlers/actions.rs
author S.D.
Tue, 27 Sep 2022 14:59:03 +0300
changeset 15878 fc3cb23fd26f
parent 15804 747278149393
permissions -rw-r--r--
Allow to see rooms of incompatible versions in the lobby For the new clients the room version is shown in a separate column. There is also a hack for previous versions clients: the room vesion specifier is prepended to the room names for rooms of incompatible versions, and the server shows 'incompatible version' error if the client tries to join them.
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,
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
     7
        types::{ClientId, RoomId},
15074
c5a6e8566425 shuffle server files
alfadur
parents: 14788
diff changeset
     8
    },
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
     9
    utils::to_engine_msg,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    10
};
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
    11
use hedgewars_network_protocol::{
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
    12
    messages::{server_chat, HwProtocolMessage, HwServerMessage, HwServerMessage::*},
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
    13
    types::{GameCfg, VoteType},
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
    14
};
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    15
use rand::{distributions::Uniform, thread_rng, Rng};
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    16
use std::{io, io::Write, iter::once, mem::replace};
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    17
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
    18
#[derive(Clone)]
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    19
pub enum DestinationGroup {
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    20
    All,
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    21
    Lobby,
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    22
    Room(RoomId),
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    23
    Protocol(u16),
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    24
}
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    25
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
    26
#[derive(Clone)]
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    27
pub enum Destination {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    28
    ToId(ClientId),
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    29
    ToIds(Vec<ClientId>),
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    30
    ToSelf,
13423
87a6cad20c90 Implement game start & engine messages
alfadur
parents: 13422
diff changeset
    31
    ToAll {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    32
        group: DestinationGroup,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    33
        skip_self: bool,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    34
    },
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
pub struct PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    38
    pub destination: Destination,
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    39
    pub message: HwServerMessage,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    40
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    41
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    42
impl PendingMessage {
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    43
    pub fn send(message: HwServerMessage, client_id: ClientId) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    44
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    45
            destination: Destination::ToId(client_id),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    46
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    47
        }
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    48
    }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    49
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    50
    pub fn send_many(message: HwServerMessage, client_ids: Vec<ClientId>) -> PendingMessage {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    51
        PendingMessage {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    52
            destination: Destination::ToIds(client_ids),
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    53
            message,
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    54
        }
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    55
    }
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    56
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    57
    pub fn send_self(message: HwServerMessage) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    58
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    59
            destination: Destination::ToSelf,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    60
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    61
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    62
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    63
15075
e935b1ad23f3 normalize type names
alfadur
parents: 15074
diff changeset
    64
    pub fn send_all(message: HwServerMessage) -> PendingMessage {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    65
        let destination = Destination::ToAll {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    66
            group: DestinationGroup::All,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    67
            skip_self: false,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    68
        };
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    69
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    70
            destination,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    71
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    72
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    73
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    74
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    75
    pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    76
        if let Destination::ToAll { ref mut group, .. } = self.destination {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    77
            *group = DestinationGroup::Room(clients_room_id)
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    78
        }
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    79
        self
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    80
    }
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    81
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    82
    pub fn in_lobby(mut self) -> PendingMessage {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    83
        if let Destination::ToAll { ref mut group, .. } = self.destination {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    84
            *group = DestinationGroup::Lobby
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    85
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    86
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    87
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    88
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
    89
    pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    90
        if let Destination::ToAll { ref mut group, .. } = self.destination {
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
    91
            *group = DestinationGroup::Protocol(protocol_number)
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    92
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    93
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    94
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    95
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    96
    pub fn but_self(mut self) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    97
        if let Destination::ToAll {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    98
            ref mut skip_self, ..
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    99
        } = self.destination
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   100
        {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   101
            *skip_self = true
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   102
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   103
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   104
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   105
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   106
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
   107
pub trait ToPendingMessage {
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
   108
    fn send(self, client_id: ClientId) -> PendingMessage;
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
   109
    fn send_many(self, client_ids: Vec<ClientId>) -> PendingMessage;
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
   110
    fn send_self(self) -> PendingMessage;
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
   111
    fn send_all(self) -> PendingMessage;
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
   112
    fn send_to_destination(self, destination: Destination) -> PendingMessage;
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
   113
}
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
   114
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
   115
impl ToPendingMessage for HwServerMessage {
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
   116
    fn send(self, client_id: ClientId) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   117
        PendingMessage::send(self, client_id)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   118
    }
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
   119
    fn send_many(self, client_ids: Vec<ClientId>) -> PendingMessage {
14788
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
   120
        PendingMessage::send_many(self, client_ids)
6dea1ca64992 implement maxteams and teamchat messages
alfadur
parents: 14694
diff changeset
   121
    }
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
   122
    fn send_self(self) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   123
        PendingMessage::send_self(self)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   124
    }
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
   125
    fn send_all(self) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   126
        PendingMessage::send_all(self)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   127
    }
15804
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15543
diff changeset
   128
    fn send_to_destination(self, destination: Destination) -> PendingMessage {
15543
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
   129
        PendingMessage {
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
   130
            destination,
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
   131
            message: self,
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
   132
        }
02f648f7cbe1 better response grouping
alfadur <mail@none>
parents: 15075
diff changeset
   133
    }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   134
}