author | alfadur <mail@none> |
Wed, 06 Feb 2019 20:48:40 +0300 | |
changeset 14709 | 4569d8d50286 |
parent 14708 | 5122c584804e |
child 14710 | aae29ba56aec |
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>), |
13423 | 106 |
SendTeamRemovalMessage(String), |
14478 | 107 |
SendRoomData { |
108 |
to: ClientId, |
|
109 |
teams: bool, |
|
110 |
config: bool, |
|
111 |
flags: bool, |
|
112 |
}, |
|
12138 | 113 |
} |
12144 | 114 |
|
115 |
use self::Action::*; |
|
116 |
||
13419 | 117 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12144 | 118 |
match action { |
14478 | 119 |
SendRoomData { |
120 |
to, |
|
121 |
teams, |
|
122 |
config, |
|
123 |
flags, |
|
124 |
} => { |
|
13424 | 125 |
let room_id = server.clients[client_id].room_id; |
126 |
if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) { |
|
127 |
if config { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
128 |
/* actions.push( |
14478 | 129 |
ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config()) |
130 |
.send(to) |
|
131 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
132 |
)*/ |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
133 |
; |
13500 | 134 |
for cfg in r.game_config() { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
135 |
//actions.push(cfg.to_server_msg().send(to).action()); |
13424 | 136 |
} |
137 |
} |
|
138 |
if teams { |
|
13427 | 139 |
let current_teams = match r.game_info { |
140 |
Some(ref info) => &info.teams_at_start, |
|
14478 | 141 |
None => &r.teams, |
13427 | 142 |
}; |
143 |
for (owner_id, team) in current_teams.iter() { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
144 |
/*actions.push( |
14478 | 145 |
TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team)) |
146 |
.send(to) |
|
147 |
.action(), |
|
148 |
); |
|
149 |
actions.push(TeamColor(team.name.clone(), team.color).send(to).action()); |
|
150 |
actions.push( |
|
151 |
HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
152 |
.send(to) |
|
153 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
154 |
);*/ |
13424 | 155 |
} |
156 |
} |
|
157 |
if flags { |
|
158 |
if let Some(id) = r.master_id { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
159 |
/* |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
160 |
actions.push( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
161 |
ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()]) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
162 |
.send(to) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
163 |
.action(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
164 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
165 |
*/ |
13424 | 166 |
} |
14478 | 167 |
let nicks: Vec<_> = server |
168 |
.clients |
|
169 |
.iter() |
|
13486 | 170 |
.filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready()) |
14478 | 171 |
.map(|(_, c)| c.nick.clone()) |
172 |
.collect(); |
|
13424 | 173 |
if !nicks.is_empty() { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
174 |
/*actions.push(ClientFlags("+r".to_string(), nicks).send(to).action())*/ |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
175 |
; |
13424 | 176 |
} |
177 |
} |
|
178 |
} |
|
13450 | 179 |
} |
13416 | 180 |
ChangeMaster(room_id, new_id) => { |
181 |
let room_client_ids = server.room_clients(room_id); |
|
14478 | 182 |
let new_id = if server |
183 |
.room(client_id) |
|
184 |
.map(|r| r.is_fixed()) |
|
185 |
.unwrap_or(false) |
|
186 |
{ |
|
13447 | 187 |
new_id |
188 |
} else { |
|
14478 | 189 |
new_id.or_else(|| room_client_ids.iter().find(|id| **id != client_id).cloned()) |
13447 | 190 |
}; |
13416 | 191 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
192 |
||
13419 | 193 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
194 |
match r.master_id { |
|
195 |
Some(id) if id == c.id => { |
|
13486 | 196 |
c.set_is_master(false); |
13419 | 197 |
r.master_id = None; |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
198 |
/*actions.push( |
14478 | 199 |
ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
200 |
.send_all() |
|
201 |
.in_room(r.id) |
|
202 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
203 |
);*/ |
13419 | 204 |
} |
205 |
Some(_) => unreachable!(), |
|
206 |
None => {} |
|
13416 | 207 |
} |
208 |
r.master_id = new_id; |
|
13775 | 209 |
if !r.is_fixed() && c.protocol_number < 42 { |
14478 | 210 |
r.name |
211 |
.replace_range(.., new_nick.as_ref().map_or("[]", String::as_str)); |
|
13775 | 212 |
} |
13494 | 213 |
r.set_join_restriction(false); |
214 |
r.set_team_add_restriction(false); |
|
215 |
let is_fixed = r.is_fixed(); |
|
216 |
r.set_unregistered_players_restriction(is_fixed); |
|
13416 | 217 |
if let Some(nick) = new_nick { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
218 |
/*actions.push( |
14478 | 219 |
ClientFlags("+h".to_string(), vec![nick]) |
220 |
.send_all() |
|
221 |
.in_room(r.id) |
|
222 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
223 |
);*/ |
13416 | 224 |
} |
225 |
} |
|
13666 | 226 |
if let Some(id) = new_id { |
227 |
server.clients[id].set_is_master(true) |
|
228 |
} |
|
14478 | 229 |
} |
13423 | 230 |
SendTeamRemovalMessage(team_name) => { |
13521 | 231 |
if let Some(r) = server.room(client_id) { |
13423 | 232 |
if let Some(ref mut info) = r.game_info { |
233 |
let msg = once(b'F').chain(team_name.bytes()); |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
234 |
/*actions.push( |
14478 | 235 |
ForwardEngineMessage(vec![to_engine_msg(msg)]) |
236 |
.send_all() |
|
237 |
.in_room(r.id) |
|
238 |
.but_self() |
|
239 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
240 |
);*/ |
13423 | 241 |
info.teams_in_game -= 1; |
242 |
if info.teams_in_game == 0 { |
|
14709
4569d8d50286
Server action refactoring part B of N
alfadur <mail@none>
parents:
14708
diff
changeset
|
243 |
//actions.push(FinishRoomGame(r.id)); |
13423 | 244 |
} |
13427 | 245 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
13443 | 246 |
if let Some(m) = &info.sync_msg { |
247 |
info.msg_log.push(m.clone()); |
|
13427 | 248 |
} |
13443 | 249 |
if info.sync_msg.is_some() { |
250 |
info.sync_msg = None |
|
251 |
} |
|
252 |
info.msg_log.push(remove_msg.clone()); |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
253 |
/*actions.push( |
14478 | 254 |
ForwardEngineMessage(vec![remove_msg]) |
255 |
.send_all() |
|
256 |
.in_room(r.id) |
|
257 |
.but_self() |
|
258 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
259 |
);*/ |
13423 | 260 |
} |
261 |
} |
|
262 |
} |
|
12144 | 263 |
} |
264 |
} |