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