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