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 |
|
14788
|
15 |
pub enum DestinationGroup {
|
14694
|
16 |
All,
|
|
17 |
Lobby,
|
|
18 |
Room(RoomId),
|
14788
|
19 |
Protocol(u16),
|
14694
|
20 |
}
|
|
21 |
|
13419
|
22 |
pub enum Destination {
|
13426
|
23 |
ToId(ClientId),
|
14788
|
24 |
ToIds(Vec<ClientId>),
|
13419
|
25 |
ToSelf,
|
13423
|
26 |
ToAll {
|
14788
|
27 |
group: DestinationGroup,
|
14457
|
28 |
skip_self: bool,
|
|
29 |
},
|
13419
|
30 |
}
|
|
31 |
|
|
32 |
pub struct PendingMessage {
|
|
33 |
pub destination: Destination,
|
15075
|
34 |
pub message: HwServerMessage,
|
13419
|
35 |
}
|
|
36 |
|
|
37 |
impl PendingMessage {
|
15075
|
38 |
pub fn send(message: HwServerMessage, client_id: ClientId) -> PendingMessage {
|
14457
|
39 |
PendingMessage {
|
|
40 |
destination: Destination::ToId(client_id),
|
|
41 |
message,
|
|
42 |
}
|
13426
|
43 |
}
|
|
44 |
|
15075
|
45 |
pub fn send_many(message: HwServerMessage, client_ids: Vec<ClientId>) -> PendingMessage {
|
14788
|
46 |
PendingMessage {
|
|
47 |
destination: Destination::ToIds(client_ids),
|
|
48 |
message,
|
|
49 |
}
|
|
50 |
}
|
|
51 |
|
15075
|
52 |
pub fn send_self(message: HwServerMessage) -> PendingMessage {
|
14457
|
53 |
PendingMessage {
|
|
54 |
destination: Destination::ToSelf,
|
|
55 |
message,
|
|
56 |
}
|
13419
|
57 |
}
|
|
58 |
|
15075
|
59 |
pub fn send_all(message: HwServerMessage) -> PendingMessage {
|
13419
|
60 |
let destination = Destination::ToAll {
|
14788
|
61 |
group: DestinationGroup::All,
|
13419
|
62 |
skip_self: false,
|
|
63 |
};
|
14457
|
64 |
PendingMessage {
|
|
65 |
destination,
|
|
66 |
message,
|
|
67 |
}
|
13419
|
68 |
}
|
|
69 |
|
|
70 |
pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
|
14788
|
71 |
if let Destination::ToAll { ref mut group, .. } = self.destination {
|
|
72 |
*group = DestinationGroup::Room(clients_room_id)
|
14694
|
73 |
}
|
|
74 |
self
|
|
75 |
}
|
|
76 |
|
|
77 |
pub fn in_lobby(mut self) -> PendingMessage {
|
14788
|
78 |
if let Destination::ToAll { ref mut group, .. } = self.destination {
|
|
79 |
*group = DestinationGroup::Lobby
|
13419
|
80 |
}
|
|
81 |
self
|
|
82 |
}
|
|
83 |
|
13520
|
84 |
pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage {
|
14788
|
85 |
if let Destination::ToAll { ref mut group, .. } = self.destination {
|
|
86 |
*group = DestinationGroup::Protocol(protocol_number)
|
13419
|
87 |
}
|
|
88 |
self
|
|
89 |
}
|
|
90 |
|
|
91 |
pub fn but_self(mut self) -> PendingMessage {
|
14457
|
92 |
if let Destination::ToAll {
|
|
93 |
ref mut skip_self, ..
|
|
94 |
} = self.destination
|
|
95 |
{
|
13419
|
96 |
*skip_self = true
|
|
97 |
}
|
|
98 |
self
|
|
99 |
}
|
|
100 |
}
|
|
101 |
|
15075
|
102 |
impl HwServerMessage {
|
14457
|
103 |
pub fn send(self, client_id: ClientId) -> PendingMessage {
|
|
104 |
PendingMessage::send(self, client_id)
|
|
105 |
}
|
14788
|
106 |
pub fn send_many(self, client_ids: Vec<ClientId>) -> PendingMessage {
|
|
107 |
PendingMessage::send_many(self, client_ids)
|
|
108 |
}
|
14457
|
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 |
}
|
13419
|
115 |
}
|