13671
|
1 |
use crate::{
|
15079
|
2 |
core::{
|
15080
|
3 |
client::HwClient,
|
|
4 |
room::HwRoom,
|
|
5 |
room::{GameInfo, RoomFlags},
|
|
6 |
server::HwServer,
|
15079
|
7 |
types::{ClientId, GameCfg, RoomId, VoteType},
|
|
8 |
},
|
15080
|
9 |
protocol::messages::{server_chat, HwProtocolMessage, HwServerMessage, HwServerMessage::*},
|
14462
|
10 |
utils::to_engine_msg,
|
13421
|
11 |
};
|
14462
|
12 |
use rand::{distributions::Uniform, thread_rng, Rng};
|
|
13 |
use std::{io, io::Write, iter::once, mem::replace};
|
12143
|
14 |
|
14793
|
15 |
pub enum DestinationGroup {
|
14699
|
16 |
All,
|
|
17 |
Lobby,
|
|
18 |
Room(RoomId),
|
14793
|
19 |
Protocol(u16),
|
14699
|
20 |
}
|
|
21 |
|
13424
|
22 |
pub enum Destination {
|
13431
|
23 |
ToId(ClientId),
|
14793
|
24 |
ToIds(Vec<ClientId>),
|
13424
|
25 |
ToSelf,
|
13428
|
26 |
ToAll {
|
14793
|
27 |
group: DestinationGroup,
|
14462
|
28 |
skip_self: bool,
|
|
29 |
},
|
13424
|
30 |
}
|
|
31 |
|
|
32 |
pub struct PendingMessage {
|
|
33 |
pub destination: Destination,
|
15080
|
34 |
pub message: HwServerMessage,
|
13424
|
35 |
}
|
|
36 |
|
|
37 |
impl PendingMessage {
|
15080
|
38 |
pub fn send(message: HwServerMessage, client_id: ClientId) -> PendingMessage {
|
14462
|
39 |
PendingMessage {
|
|
40 |
destination: Destination::ToId(client_id),
|
|
41 |
message,
|
|
42 |
}
|
13431
|
43 |
}
|
|
44 |
|
15080
|
45 |
pub fn send_many(message: HwServerMessage, client_ids: Vec<ClientId>) -> PendingMessage {
|
14793
|
46 |
PendingMessage {
|
|
47 |
destination: Destination::ToIds(client_ids),
|
|
48 |
message,
|
|
49 |
}
|
|
50 |
}
|
|
51 |
|
15080
|
52 |
pub fn send_self(message: HwServerMessage) -> PendingMessage {
|
14462
|
53 |
PendingMessage {
|
|
54 |
destination: Destination::ToSelf,
|
|
55 |
message,
|
|
56 |
}
|
13424
|
57 |
}
|
|
58 |
|
15080
|
59 |
pub fn send_all(message: HwServerMessage) -> PendingMessage {
|
13424
|
60 |
let destination = Destination::ToAll {
|
14793
|
61 |
group: DestinationGroup::All,
|
13424
|
62 |
skip_self: false,
|
|
63 |
};
|
14462
|
64 |
PendingMessage {
|
|
65 |
destination,
|
|
66 |
message,
|
|
67 |
}
|
13424
|
68 |
}
|
|
69 |
|
|
70 |
pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
|
14793
|
71 |
if let Destination::ToAll { ref mut group, .. } = self.destination {
|
|
72 |
*group = DestinationGroup::Room(clients_room_id)
|
14699
|
73 |
}
|
|
74 |
self
|
|
75 |
}
|
|
76 |
|
|
77 |
pub fn in_lobby(mut self) -> PendingMessage {
|
14793
|
78 |
if let Destination::ToAll { ref mut group, .. } = self.destination {
|
|
79 |
*group = DestinationGroup::Lobby
|
13424
|
80 |
}
|
|
81 |
self
|
|
82 |
}
|
|
83 |
|
13525
|
84 |
pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage {
|
14793
|
85 |
if let Destination::ToAll { ref mut group, .. } = self.destination {
|
|
86 |
*group = DestinationGroup::Protocol(protocol_number)
|
13424
|
87 |
}
|
|
88 |
self
|
|
89 |
}
|
|
90 |
|
|
91 |
pub fn but_self(mut self) -> PendingMessage {
|
14462
|
92 |
if let Destination::ToAll {
|
|
93 |
ref mut skip_self, ..
|
|
94 |
} = self.destination
|
|
95 |
{
|
13424
|
96 |
*skip_self = true
|
|
97 |
}
|
|
98 |
self
|
|
99 |
}
|
|
100 |
}
|
|
101 |
|
15080
|
102 |
impl HwServerMessage {
|
14462
|
103 |
pub fn send(self, client_id: ClientId) -> PendingMessage {
|
|
104 |
PendingMessage::send(self, client_id)
|
|
105 |
}
|
14793
|
106 |
pub fn send_many(self, client_ids: Vec<ClientId>) -> PendingMessage {
|
|
107 |
PendingMessage::send_many(self, client_ids)
|
|
108 |
}
|
14462
|
109 |
pub fn send_self(self) -> PendingMessage {
|
|
110 |
PendingMessage::send_self(self)
|
|
111 |
}
|
|
112 |
pub fn send_all(self) -> PendingMessage {
|
|
113 |
PendingMessage::send_all(self)
|
|
114 |
}
|
13424
|
115 |
}
|