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