13416
|
1 |
use super::{
|
14478
|
2 |
client::HWClient,
|
14395
|
3 |
core::HWServer,
|
14478
|
4 |
coretypes::{ClientId, GameCfg, RoomId, VoteType},
|
|
5 |
handlers,
|
|
6 |
room::HWRoom,
|
13494
|
7 |
room::{GameInfo, RoomFlags},
|
13416
|
8 |
};
|
13666
|
9 |
use crate::{
|
14478
|
10 |
protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage, HWServerMessage::*},
|
|
11 |
utils::to_engine_msg,
|
13416
|
12 |
};
|
14478
|
13 |
use rand::{distributions::Uniform, thread_rng, Rng};
|
|
14 |
use std::{io, io::Write, iter::once, mem::replace};
|
12138
|
15 |
|
14477
|
16 |
#[cfg(feature = "official-server")]
|
|
17 |
use super::database;
|
|
18 |
|
14809
|
19 |
pub enum DestinationGroup {
|
14715
|
20 |
All,
|
|
21 |
Lobby,
|
|
22 |
Room(RoomId),
|
14809
|
23 |
Protocol(u16),
|
14715
|
24 |
}
|
|
25 |
|
13419
|
26 |
pub enum Destination {
|
13426
|
27 |
ToId(ClientId),
|
14809
|
28 |
ToIds(Vec<ClientId>),
|
13419
|
29 |
ToSelf,
|
13423
|
30 |
ToAll {
|
14809
|
31 |
group: DestinationGroup,
|
14478
|
32 |
skip_self: bool,
|
|
33 |
},
|
13419
|
34 |
}
|
|
35 |
|
|
36 |
pub struct PendingMessage {
|
|
37 |
pub destination: Destination,
|
14478
|
38 |
pub message: HWServerMessage,
|
13419
|
39 |
}
|
|
40 |
|
|
41 |
impl PendingMessage {
|
13426
|
42 |
pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage {
|
14478
|
43 |
PendingMessage {
|
|
44 |
destination: Destination::ToId(client_id),
|
|
45 |
message,
|
|
46 |
}
|
13426
|
47 |
}
|
|
48 |
|
14809
|
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 |
|
13419
|
56 |
pub fn send_self(message: HWServerMessage) -> PendingMessage {
|
14478
|
57 |
PendingMessage {
|
|
58 |
destination: Destination::ToSelf,
|
|
59 |
message,
|
|
60 |
}
|
13419
|
61 |
}
|
|
62 |
|
|
63 |
pub fn send_all(message: HWServerMessage) -> PendingMessage {
|
|
64 |
let destination = Destination::ToAll {
|
14809
|
65 |
group: DestinationGroup::All,
|
13419
|
66 |
skip_self: false,
|
|
67 |
};
|
14478
|
68 |
PendingMessage {
|
|
69 |
destination,
|
|
70 |
message,
|
|
71 |
}
|
13419
|
72 |
}
|
|
73 |
|
|
74 |
pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
|
14809
|
75 |
if let Destination::ToAll { ref mut group, .. } = self.destination {
|
|
76 |
*group = DestinationGroup::Room(clients_room_id)
|
14715
|
77 |
}
|
|
78 |
self
|
|
79 |
}
|
|
80 |
|
|
81 |
pub fn in_lobby(mut self) -> PendingMessage {
|
14809
|
82 |
if let Destination::ToAll { ref mut group, .. } = self.destination {
|
|
83 |
*group = DestinationGroup::Lobby
|
13419
|
84 |
}
|
|
85 |
self
|
|
86 |
}
|
|
87 |
|
13486
|
88 |
pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage {
|
14809
|
89 |
if let Destination::ToAll { ref mut group, .. } = self.destination {
|
|
90 |
*group = DestinationGroup::Protocol(protocol_number)
|
13419
|
91 |
}
|
|
92 |
self
|
|
93 |
}
|
|
94 |
|
|
95 |
pub fn but_self(mut self) -> PendingMessage {
|
14478
|
96 |
if let Destination::ToAll {
|
|
97 |
ref mut skip_self, ..
|
|
98 |
} = self.destination
|
|
99 |
{
|
13419
|
100 |
*skip_self = true
|
|
101 |
}
|
|
102 |
self
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
impl HWServerMessage {
|
14478
|
107 |
pub fn send(self, client_id: ClientId) -> PendingMessage {
|
|
108 |
PendingMessage::send(self, client_id)
|
|
109 |
}
|
14809
|
110 |
pub fn send_many(self, client_ids: Vec<ClientId>) -> PendingMessage {
|
|
111 |
PendingMessage::send_many(self, client_ids)
|
|
112 |
}
|
14478
|
113 |
pub fn send_self(self) -> PendingMessage {
|
|
114 |
PendingMessage::send_self(self)
|
|
115 |
}
|
|
116 |
pub fn send_all(self) -> PendingMessage {
|
|
117 |
PendingMessage::send_all(self)
|
|
118 |
}
|
13419
|
119 |
}
|