rust/hedgewars-server/src/server/actions.rs
changeset 14809 6dea1ca64992
parent 14715 25c564f77b7d
equal deleted inserted replaced
14808:0e64acbc3f8b 14809:6dea1ca64992
    14 use std::{io, io::Write, iter::once, mem::replace};
    14 use std::{io, io::Write, iter::once, mem::replace};
    15 
    15 
    16 #[cfg(feature = "official-server")]
    16 #[cfg(feature = "official-server")]
    17 use super::database;
    17 use super::database;
    18 
    18 
    19 pub enum DestinationRoom {
    19 pub enum DestinationGroup {
    20     All,
    20     All,
    21     Lobby,
    21     Lobby,
    22     Room(RoomId),
    22     Room(RoomId),
       
    23     Protocol(u16),
    23 }
    24 }
    24 
    25 
    25 pub enum Destination {
    26 pub enum Destination {
    26     ToId(ClientId),
    27     ToId(ClientId),
       
    28     ToIds(Vec<ClientId>),
    27     ToSelf,
    29     ToSelf,
    28     ToAll {
    30     ToAll {
    29         room_id: DestinationRoom,
    31         group: DestinationGroup,
    30         protocol: Option<u16>,
       
    31         skip_self: bool,
    32         skip_self: bool,
    32     },
    33     },
    33 }
    34 }
    34 
    35 
    35 pub struct PendingMessage {
    36 pub struct PendingMessage {
    43             destination: Destination::ToId(client_id),
    44             destination: Destination::ToId(client_id),
    44             message,
    45             message,
    45         }
    46         }
    46     }
    47     }
    47 
    48 
       
    49     pub fn send_many(message: HWServerMessage, client_ids: Vec<ClientId>) -> PendingMessage {
       
    50         PendingMessage {
       
    51             destination: Destination::ToIds(client_ids),
       
    52             message,
       
    53         }
       
    54     }
       
    55 
    48     pub fn send_self(message: HWServerMessage) -> PendingMessage {
    56     pub fn send_self(message: HWServerMessage) -> PendingMessage {
    49         PendingMessage {
    57         PendingMessage {
    50             destination: Destination::ToSelf,
    58             destination: Destination::ToSelf,
    51             message,
    59             message,
    52         }
    60         }
    53     }
    61     }
    54 
    62 
    55     pub fn send_all(message: HWServerMessage) -> PendingMessage {
    63     pub fn send_all(message: HWServerMessage) -> PendingMessage {
    56         let destination = Destination::ToAll {
    64         let destination = Destination::ToAll {
    57             room_id: DestinationRoom::All,
    65             group: DestinationGroup::All,
    58             protocol: None,
       
    59             skip_self: false,
    66             skip_self: false,
    60         };
    67         };
    61         PendingMessage {
    68         PendingMessage {
    62             destination,
    69             destination,
    63             message,
    70             message,
    64         }
    71         }
    65     }
    72     }
    66 
    73 
    67     pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
    74     pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
    68         if let Destination::ToAll {
    75         if let Destination::ToAll { ref mut group, .. } = self.destination {
    69             ref mut room_id, ..
    76             *group = DestinationGroup::Room(clients_room_id)
    70         } = self.destination
       
    71         {
       
    72             *room_id = DestinationRoom::Room(clients_room_id)
       
    73         }
    77         }
    74         self
    78         self
    75     }
    79     }
    76 
    80 
    77     pub fn in_lobby(mut self) -> PendingMessage {
    81     pub fn in_lobby(mut self) -> PendingMessage {
    78         if let Destination::ToAll {
    82         if let Destination::ToAll { ref mut group, .. } = self.destination {
    79             ref mut room_id, ..
    83             *group = DestinationGroup::Lobby
    80         } = self.destination
       
    81         {
       
    82             *room_id = DestinationRoom::Lobby
       
    83         }
    84         }
    84         self
    85         self
    85     }
    86     }
    86 
    87 
    87     pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage {
    88     pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage {
    88         if let Destination::ToAll {
    89         if let Destination::ToAll { ref mut group, .. } = self.destination {
    89             ref mut protocol, ..
    90             *group = DestinationGroup::Protocol(protocol_number)
    90         } = self.destination
       
    91         {
       
    92             *protocol = Some(protocol_number)
       
    93         }
    91         }
    94         self
    92         self
    95     }
    93     }
    96 
    94 
    97     pub fn but_self(mut self) -> PendingMessage {
    95     pub fn but_self(mut self) -> PendingMessage {
   107 
   105 
   108 impl HWServerMessage {
   106 impl HWServerMessage {
   109     pub fn send(self, client_id: ClientId) -> PendingMessage {
   107     pub fn send(self, client_id: ClientId) -> PendingMessage {
   110         PendingMessage::send(self, client_id)
   108         PendingMessage::send(self, client_id)
   111     }
   109     }
       
   110     pub fn send_many(self, client_ids: Vec<ClientId>) -> PendingMessage {
       
   111         PendingMessage::send_many(self, client_ids)
       
   112     }
   112     pub fn send_self(self) -> PendingMessage {
   113     pub fn send_self(self) -> PendingMessage {
   113         PendingMessage::send_self(self)
   114         PendingMessage::send_self(self)
   114     }
   115     }
   115     pub fn send_all(self) -> PendingMessage {
   116     pub fn send_all(self) -> PendingMessage {
   116         PendingMessage::send_all(self)
   117         PendingMessage::send_all(self)