author | alfadur <mail@none> |
Wed, 06 Feb 2019 21:33:22 +0300 | |
changeset 14710 | aae29ba56aec |
parent 14709 | 4569d8d50286 |
child 14711 | f61ce544d436 |
permissions | -rw-r--r-- |
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 |
} |
103 |
||
12138 | 104 |
pub enum Action { |
13416 | 105 |
ChangeMaster(RoomId, Option<ClientId>), |
12138 | 106 |
} |
12144 | 107 |
|
108 |
use self::Action::*; |
|
109 |
||
13419 | 110 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12144 | 111 |
match action { |
13416 | 112 |
ChangeMaster(room_id, new_id) => { |
113 |
let room_client_ids = server.room_clients(room_id); |
|
14478 | 114 |
let new_id = if server |
115 |
.room(client_id) |
|
116 |
.map(|r| r.is_fixed()) |
|
117 |
.unwrap_or(false) |
|
118 |
{ |
|
13447 | 119 |
new_id |
120 |
} else { |
|
14478 | 121 |
new_id.or_else(|| room_client_ids.iter().find(|id| **id != client_id).cloned()) |
13447 | 122 |
}; |
13416 | 123 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
124 |
||
13419 | 125 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
126 |
match r.master_id { |
|
127 |
Some(id) if id == c.id => { |
|
13486 | 128 |
c.set_is_master(false); |
13419 | 129 |
r.master_id = None; |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
130 |
/*actions.push( |
14478 | 131 |
ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
132 |
.send_all() |
|
133 |
.in_room(r.id) |
|
134 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
135 |
);*/ |
13419 | 136 |
} |
137 |
Some(_) => unreachable!(), |
|
138 |
None => {} |
|
13416 | 139 |
} |
140 |
r.master_id = new_id; |
|
13775 | 141 |
if !r.is_fixed() && c.protocol_number < 42 { |
14478 | 142 |
r.name |
143 |
.replace_range(.., new_nick.as_ref().map_or("[]", String::as_str)); |
|
13775 | 144 |
} |
13494 | 145 |
r.set_join_restriction(false); |
146 |
r.set_team_add_restriction(false); |
|
147 |
let is_fixed = r.is_fixed(); |
|
148 |
r.set_unregistered_players_restriction(is_fixed); |
|
13416 | 149 |
if let Some(nick) = new_nick { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
150 |
/*actions.push( |
14478 | 151 |
ClientFlags("+h".to_string(), vec![nick]) |
152 |
.send_all() |
|
153 |
.in_room(r.id) |
|
154 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
155 |
);*/ |
13416 | 156 |
} |
157 |
} |
|
13666 | 158 |
if let Some(id) = new_id { |
159 |
server.clients[id].set_is_master(true) |
|
160 |
} |
|
14478 | 161 |
} |
12144 | 162 |
} |
163 |
} |