rust/hedgewars-server/src/server/actions.rs
author alfadur <mail@none>
Thu, 07 Feb 2019 17:17:42 +0300
changeset 14694 25c564f77b7d
parent 14690 f61ce544d436
child 14788 6dea1ca64992
permissions -rw-r--r--
remove lobby room
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     1
use super::{
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
     2
    client::HWClient,
14374
e5db279308d7 dispose of server mods
alfadur
parents: 14350
diff changeset
     3
    core::HWServer,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
     4
    coretypes::{ClientId, GameCfg, RoomId, VoteType},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
     5
    handlers,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
     6
    room::HWRoom,
13523
8c5dd562c9f7 Add room flags
alfadur
parents: 13520
diff changeset
     7
    room::{GameInfo, RoomFlags},
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
     8
};
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13527
diff changeset
     9
use crate::{
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    10
    protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage, HWServerMessage::*},
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    11
    utils::to_engine_msg,
13416
cdf69667593b partial room implementation
alfadur
parents: 13119
diff changeset
    12
};
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    13
use rand::{distributions::Uniform, thread_rng, Rng};
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    14
use std::{io, io::Write, iter::once, mem::replace};
12138
e0bf51609062 Introduce actions, just like in the old server
unc0rr
parents:
diff changeset
    15
14456
a077aac9df01 Start database interaction implementation
unc0rr
parents: 14415
diff changeset
    16
#[cfg(feature = "official-server")]
a077aac9df01 Start database interaction implementation
unc0rr
parents: 14415
diff changeset
    17
use super::database;
a077aac9df01 Start database interaction implementation
unc0rr
parents: 14415
diff changeset
    18
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    19
pub enum DestinationRoom {
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),
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    23
}
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    24
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    25
pub enum Destination {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    26
    ToId(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 {
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    29
        room_id: DestinationRoom,
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
    30
        protocol: Option<u16>,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    31
        skip_self: bool,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    32
    },
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    33
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    34
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    35
pub struct PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    36
    pub destination: Destination,
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    37
    pub message: HWServerMessage,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    38
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    39
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    40
impl PendingMessage {
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    41
    pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    42
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    43
            destination: Destination::ToId(client_id),
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    44
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    45
        }
13426
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    46
    }
f091f69d59e4 Additional round cleanup
alfadur
parents: 13424
diff changeset
    47
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    48
    pub fn send_self(message: HWServerMessage) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    49
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    50
            destination: Destination::ToSelf,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    51
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    52
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    53
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    54
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    55
    pub fn send_all(message: HWServerMessage) -> PendingMessage {
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    56
        let destination = Destination::ToAll {
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    57
            room_id: DestinationRoom::All,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    58
            protocol: None,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    59
            skip_self: false,
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    60
        };
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    61
        PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    62
            destination,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    63
            message,
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    64
        }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    65
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    66
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    67
    pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    68
        if let Destination::ToAll {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    69
            ref mut room_id, ..
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    70
        } = self.destination
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    71
        {
14694
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    72
            *room_id = DestinationRoom::Room(clients_room_id)
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 {
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    78
        if let Destination::ToAll {
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    79
            ref mut room_id, ..
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    80
        } = self.destination
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    81
        {
25c564f77b7d remove lobby room
alfadur <mail@none>
parents: 14690
diff changeset
    82
            *room_id = DestinationRoom::Lobby
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    83
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    84
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    85
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    86
13520
1ee192f13456 Better packing for clients
alfadur
parents: 13480
diff changeset
    87
    pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    88
        if let Destination::ToAll {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    89
            ref mut protocol, ..
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    90
        } = self.destination
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    91
        {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    92
            *protocol = Some(protocol_number)
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    93
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    94
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    95
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    96
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    97
    pub fn but_self(mut self) -> PendingMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    98
        if let Destination::ToAll {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
    99
            ref mut skip_self, ..
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   100
        } = self.destination
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   101
        {
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   102
            *skip_self = true
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   103
        }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   104
        self
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   105
    }
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   106
}
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   107
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   108
impl HWServerMessage {
14457
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   109
    pub fn send(self, client_id: ClientId) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   110
        PendingMessage::send(self, client_id)
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_self(self) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   113
        PendingMessage::send_self(self)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   114
    }
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   115
    pub fn send_all(self) -> PendingMessage {
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   116
        PendingMessage::send_all(self)
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14456
diff changeset
   117
    }
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
   118
}