author | alfadur <mail@none> |
Wed, 06 Feb 2019 21:33:22 +0300 | |
changeset 14689 | aae29ba56aec |
parent 14688 | 4569d8d50286 |
child 14690 | f61ce544d436 |
permissions | -rw-r--r-- |
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 |
||
13419 | 19 |
pub enum Destination { |
13426 | 20 |
ToId(ClientId), |
13419 | 21 |
ToSelf, |
13423 | 22 |
ToAll { |
13419 | 23 |
room_id: Option<RoomId>, |
13520 | 24 |
protocol: Option<u16>, |
14457 | 25 |
skip_self: bool, |
26 |
}, |
|
13419 | 27 |
} |
28 |
||
29 |
pub struct PendingMessage { |
|
30 |
pub destination: Destination, |
|
14457 | 31 |
pub message: HWServerMessage, |
13419 | 32 |
} |
33 |
||
34 |
impl PendingMessage { |
|
13426 | 35 |
pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage { |
14457 | 36 |
PendingMessage { |
37 |
destination: Destination::ToId(client_id), |
|
38 |
message, |
|
39 |
} |
|
13426 | 40 |
} |
41 |
||
13419 | 42 |
pub fn send_self(message: HWServerMessage) -> PendingMessage { |
14457 | 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 |
}; |
|
14457 | 55 |
PendingMessage { |
56 |
destination, |
|
57 |
message, |
|
58 |
} |
|
13419 | 59 |
} |
60 |
||
61 |
pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage { |
|
14457 | 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 |
||
13520 | 71 |
pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage { |
14457 | 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 { |
|
14457 | 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 { |
|
14457 | 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); |
|
14457 | 114 |
let new_id = if server |
115 |
.room(client_id) |
|
116 |
.map(|r| r.is_fixed()) |
|
117 |
.unwrap_or(false) |
|
118 |
{ |
|
13477 | 119 |
new_id |
120 |
} else { |
|
14457 | 121 |
new_id.or_else(|| room_client_ids.iter().find(|id| **id != client_id).cloned()) |
13477 | 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 => { |
|
13520 | 128 |
c.set_is_master(false); |
13419 | 129 |
r.master_id = None; |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
130 |
/*actions.push( |
14457 | 131 |
ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
132 |
.send_all() |
|
133 |
.in_room(r.id) |
|
134 |
.action(), |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
135 |
);*/ |
13419 | 136 |
} |
137 |
Some(_) => unreachable!(), |
|
138 |
None => {} |
|
13416 | 139 |
} |
140 |
r.master_id = new_id; |
|
13801 | 141 |
if !r.is_fixed() && c.protocol_number < 42 { |
14457 | 142 |
r.name |
143 |
.replace_range(.., new_nick.as_ref().map_or("[]", String::as_str)); |
|
13801 | 144 |
} |
13523 | 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 { |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
150 |
/*actions.push( |
14457 | 151 |
ClientFlags("+h".to_string(), vec![nick]) |
152 |
.send_all() |
|
153 |
.in_room(r.id) |
|
154 |
.action(), |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
155 |
);*/ |
13416 | 156 |
} |
157 |
} |
|
13666 | 158 |
if let Some(id) = new_id { |
159 |
server.clients[id].set_is_master(true) |
|
160 |
} |
|
14457 | 161 |
} |
12144 | 162 |
} |
163 |
} |