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