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