13416
|
1 |
use super::{
|
14478
|
2 |
client::HWClient,
|
14395
|
3 |
core::HWServer,
|
14478
|
4 |
coretypes::{ClientId, GameCfg, RoomId, VoteType},
|
|
5 |
handlers,
|
|
6 |
room::HWRoom,
|
13494
|
7 |
room::{GameInfo, RoomFlags},
|
13416
|
8 |
};
|
13666
|
9 |
use crate::{
|
14478
|
10 |
protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage, HWServerMessage::*},
|
|
11 |
utils::to_engine_msg,
|
13416
|
12 |
};
|
14478
|
13 |
use rand::{distributions::Uniform, thread_rng, Rng};
|
|
14 |
use std::{io, io::Write, iter::once, mem::replace};
|
12138
|
15 |
|
14477
|
16 |
#[cfg(feature = "official-server")]
|
|
17 |
use super::database;
|
|
18 |
|
13419
|
19 |
pub enum Destination {
|
13426
|
20 |
ToId(ClientId),
|
13419
|
21 |
ToSelf,
|
13423
|
22 |
ToAll {
|
13419
|
23 |
room_id: Option<RoomId>,
|
13486
|
24 |
protocol: Option<u16>,
|
14478
|
25 |
skip_self: bool,
|
|
26 |
},
|
13419
|
27 |
}
|
|
28 |
|
|
29 |
pub struct PendingMessage {
|
|
30 |
pub destination: Destination,
|
14478
|
31 |
pub message: HWServerMessage,
|
13419
|
32 |
}
|
|
33 |
|
|
34 |
impl PendingMessage {
|
13426
|
35 |
pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage {
|
14478
|
36 |
PendingMessage {
|
|
37 |
destination: Destination::ToId(client_id),
|
|
38 |
message,
|
|
39 |
}
|
13426
|
40 |
}
|
|
41 |
|
13419
|
42 |
pub fn send_self(message: HWServerMessage) -> PendingMessage {
|
14478
|
43 |
PendingMessage {
|
|
44 |
destination: Destination::ToSelf,
|
|
45 |
message,
|
|
46 |
}
|
13419
|
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 |
};
|
14478
|
55 |
PendingMessage {
|
|
56 |
destination,
|
|
57 |
message,
|
|
58 |
}
|
13419
|
59 |
}
|
|
60 |
|
|
61 |
pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage {
|
14478
|
62 |
if let Destination::ToAll {
|
|
63 |
ref mut room_id, ..
|
|
64 |
} = self.destination
|
|
65 |
{
|
13419
|
66 |
*room_id = Some(clients_room_id)
|
|
67 |
}
|
|
68 |
self
|
|
69 |
}
|
|
70 |
|
13486
|
71 |
pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage {
|
14478
|
72 |
if let Destination::ToAll {
|
|
73 |
ref mut protocol, ..
|
|
74 |
} = self.destination
|
|
75 |
{
|
13419
|
76 |
*protocol = Some(protocol_number)
|
|
77 |
}
|
|
78 |
self
|
|
79 |
}
|
|
80 |
|
|
81 |
pub fn but_self(mut self) -> PendingMessage {
|
14478
|
82 |
if let Destination::ToAll {
|
|
83 |
ref mut skip_self, ..
|
|
84 |
} = self.destination
|
|
85 |
{
|
13419
|
86 |
*skip_self = true
|
|
87 |
}
|
|
88 |
self
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
|
92 |
impl HWServerMessage {
|
14478
|
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 |
}
|
13419
|
102 |
}
|