13421
|
1 |
use super::{
|
14462
|
2 |
client::HWClient,
|
14379
|
3 |
core::HWServer,
|
14462
|
4 |
coretypes::{ClientId, GameCfg, RoomId, VoteType},
|
|
5 |
handlers,
|
|
6 |
room::HWRoom,
|
13528
|
7 |
room::{GameInfo, RoomFlags},
|
13421
|
8 |
};
|
13671
|
9 |
use crate::{
|
14462
|
10 |
protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage, HWServerMessage::*},
|
|
11 |
utils::to_engine_msg,
|
13421
|
12 |
};
|
14462
|
13 |
use rand::{distributions::Uniform, thread_rng, Rng};
|
|
14 |
use std::{io, io::Write, iter::once, mem::replace};
|
12143
|
15 |
|
14461
|
16 |
#[cfg(feature = "official-server")]
|
|
17 |
use super::database;
|
|
18 |
|
13424
|
19 |
pub enum Destination {
|
13431
|
20 |
ToId(ClientId),
|
13424
|
21 |
ToSelf,
|
13428
|
22 |
ToAll {
|
13424
|
23 |
room_id: Option<RoomId>,
|
13525
|
24 |
protocol: Option<u16>,
|
14462
|
25 |
skip_self: bool,
|
|
26 |
},
|
13424
|
27 |
}
|
|
28 |
|
|
29 |
pub struct PendingMessage {
|
|
30 |
pub destination: Destination,
|
14462
|
31 |
pub message: HWServerMessage,
|
13424
|
32 |
}
|
|
33 |
|
|
34 |
impl PendingMessage {
|
13431
|
35 |
pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage {
|
14462
|
36 |
PendingMessage {
|
|
37 |
destination: Destination::ToId(client_id),
|
|
38 |
message,
|
|
39 |
}
|
13431
|
40 |
}
|
|
41 |
|
13424
|
42 |
pub fn send_self(message: HWServerMessage) -> PendingMessage {
|
14462
|
43 |
PendingMessage {
|
|
44 |
destination: Destination::ToSelf,
|
|
45 |
message,
|
|
46 |
}
|
13424
|
47 |
}
|
|
48 |
|
|
49 |
pub fn send_all(message: HWServerMessage) -> PendingMessage {
|
|
50 |
let destination = Destination::ToAll {
|
|
51 |
room_id: None,
|
|
52 |
protocol: None,
|
|
53 |
skip_self: false,
|
|
54 |
};
|
14462
|
55 |
PendingMessage {
|
|
56 |
destination,
|
|
57 |
message,
|
|
58 |
}
|
13424
|
59 |
}
|
|
60 |
|
|
61 |
pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
|
14462
|
62 |
if let Destination::ToAll {
|
|
63 |
ref mut room_id, ..
|
|
64 |
} = self.destination
|
|
65 |
{
|
13424
|
66 |
*room_id = Some(clients_room_id)
|
|
67 |
}
|
|
68 |
self
|
|
69 |
}
|
|
70 |
|
13525
|
71 |
pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage {
|
14462
|
72 |
if let Destination::ToAll {
|
|
73 |
ref mut protocol, ..
|
|
74 |
} = self.destination
|
|
75 |
{
|
13424
|
76 |
*protocol = Some(protocol_number)
|
|
77 |
}
|
|
78 |
self
|
|
79 |
}
|
|
80 |
|
|
81 |
pub fn but_self(mut self) -> PendingMessage {
|
14462
|
82 |
if let Destination::ToAll {
|
|
83 |
ref mut skip_self, ..
|
|
84 |
} = self.destination
|
|
85 |
{
|
13424
|
86 |
*skip_self = true
|
|
87 |
}
|
|
88 |
self
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
|
92 |
impl HWServerMessage {
|
14462
|
93 |
pub fn send(self, client_id: ClientId) -> PendingMessage {
|
|
94 |
PendingMessage::send(self, client_id)
|
|
95 |
}
|
|
96 |
pub fn send_self(self) -> PendingMessage {
|
|
97 |
PendingMessage::send_self(self)
|
|
98 |
}
|
|
99 |
pub fn send_all(self) -> PendingMessage {
|
|
100 |
PendingMessage::send_all(self)
|
|
101 |
}
|
13424
|
102 |
}
|