1 use crate::{ |
1 use crate::{ |
2 core::{ |
2 core::{ |
3 client::HWClient, |
3 client::HwClient, |
4 server::HWServer, |
4 room::HwRoom, |
|
5 room::{GameInfo, RoomFlags}, |
|
6 server::HwServer, |
5 types::{ClientId, GameCfg, RoomId, VoteType}, |
7 types::{ClientId, GameCfg, RoomId, VoteType}, |
6 room::HWRoom, |
|
7 room::{GameInfo, RoomFlags} |
|
8 }, |
8 }, |
9 protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage, HWServerMessage::*}, |
9 protocol::messages::{server_chat, HwProtocolMessage, HwServerMessage, HwServerMessage::*}, |
10 utils::to_engine_msg, |
10 utils::to_engine_msg, |
11 }; |
11 }; |
12 use rand::{distributions::Uniform, thread_rng, Rng}; |
12 use rand::{distributions::Uniform, thread_rng, Rng}; |
13 use std::{io, io::Write, iter::once, mem::replace}; |
13 use std::{io, io::Write, iter::once, mem::replace}; |
14 |
14 |
29 }, |
29 }, |
30 } |
30 } |
31 |
31 |
32 pub struct PendingMessage { |
32 pub struct PendingMessage { |
33 pub destination: Destination, |
33 pub destination: Destination, |
34 pub message: HWServerMessage, |
34 pub message: HwServerMessage, |
35 } |
35 } |
36 |
36 |
37 impl PendingMessage { |
37 impl PendingMessage { |
38 pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage { |
38 pub fn send(message: HwServerMessage, client_id: ClientId) -> PendingMessage { |
39 PendingMessage { |
39 PendingMessage { |
40 destination: Destination::ToId(client_id), |
40 destination: Destination::ToId(client_id), |
41 message, |
41 message, |
42 } |
42 } |
43 } |
43 } |
44 |
44 |
45 pub fn send_many(message: HWServerMessage, client_ids: Vec<ClientId>) -> PendingMessage { |
45 pub fn send_many(message: HwServerMessage, client_ids: Vec<ClientId>) -> PendingMessage { |
46 PendingMessage { |
46 PendingMessage { |
47 destination: Destination::ToIds(client_ids), |
47 destination: Destination::ToIds(client_ids), |
48 message, |
48 message, |
49 } |
49 } |
50 } |
50 } |
51 |
51 |
52 pub fn send_self(message: HWServerMessage) -> PendingMessage { |
52 pub fn send_self(message: HwServerMessage) -> PendingMessage { |
53 PendingMessage { |
53 PendingMessage { |
54 destination: Destination::ToSelf, |
54 destination: Destination::ToSelf, |
55 message, |
55 message, |
56 } |
56 } |
57 } |
57 } |
58 |
58 |
59 pub fn send_all(message: HWServerMessage) -> PendingMessage { |
59 pub fn send_all(message: HwServerMessage) -> PendingMessage { |
60 let destination = Destination::ToAll { |
60 let destination = Destination::ToAll { |
61 group: DestinationGroup::All, |
61 group: DestinationGroup::All, |
62 skip_self: false, |
62 skip_self: false, |
63 }; |
63 }; |
64 PendingMessage { |
64 PendingMessage { |
97 } |
97 } |
98 self |
98 self |
99 } |
99 } |
100 } |
100 } |
101 |
101 |
102 impl HWServerMessage { |
102 impl HwServerMessage { |
103 pub fn send(self, client_id: ClientId) -> PendingMessage { |
103 pub fn send(self, client_id: ClientId) -> PendingMessage { |
104 PendingMessage::send(self, client_id) |
104 PendingMessage::send(self, client_id) |
105 } |
105 } |
106 pub fn send_many(self, client_ids: Vec<ClientId>) -> PendingMessage { |
106 pub fn send_many(self, client_ids: Vec<ClientId>) -> PendingMessage { |
107 PendingMessage::send_many(self, client_ids) |
107 PendingMessage::send_many(self, client_ids) |