author | alfadur <mail@none> |
Wed, 06 Feb 2019 00:57:01 +0300 | |
changeset 14708 | 5122c584804e |
parent 14707 | 9f98086de1b6 |
child 14709 | 4569d8d50286 |
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 |
StartRoomGame(RoomId), |
107 |
SendTeamRemovalMessage(String), |
|
108 |
FinishRoomGame(RoomId), |
|
14478 | 109 |
SendRoomData { |
110 |
to: ClientId, |
|
111 |
teams: bool, |
|
112 |
config: bool, |
|
113 |
flags: bool, |
|
114 |
}, |
|
12138 | 115 |
} |
12144 | 116 |
|
117 |
use self::Action::*; |
|
118 |
||
13419 | 119 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12144 | 120 |
match action { |
14478 | 121 |
SendRoomData { |
122 |
to, |
|
123 |
teams, |
|
124 |
config, |
|
125 |
flags, |
|
126 |
} => { |
|
13424 | 127 |
let room_id = server.clients[client_id].room_id; |
128 |
if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) { |
|
129 |
if config { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
130 |
/* actions.push( |
14478 | 131 |
ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config()) |
132 |
.send(to) |
|
133 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
134 |
)*/ |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
135 |
; |
13500 | 136 |
for cfg in r.game_config() { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
137 |
//actions.push(cfg.to_server_msg().send(to).action()); |
13424 | 138 |
} |
139 |
} |
|
140 |
if teams { |
|
13427 | 141 |
let current_teams = match r.game_info { |
142 |
Some(ref info) => &info.teams_at_start, |
|
14478 | 143 |
None => &r.teams, |
13427 | 144 |
}; |
145 |
for (owner_id, team) in current_teams.iter() { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
146 |
/*actions.push( |
14478 | 147 |
TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team)) |
148 |
.send(to) |
|
149 |
.action(), |
|
150 |
); |
|
151 |
actions.push(TeamColor(team.name.clone(), team.color).send(to).action()); |
|
152 |
actions.push( |
|
153 |
HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
154 |
.send(to) |
|
155 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
156 |
);*/ |
13424 | 157 |
} |
158 |
} |
|
159 |
if flags { |
|
160 |
if let Some(id) = r.master_id { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
161 |
/* |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
162 |
actions.push( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
163 |
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
|
164 |
.send(to) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
165 |
.action(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
166 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
167 |
*/ |
13424 | 168 |
} |
14478 | 169 |
let nicks: Vec<_> = server |
170 |
.clients |
|
171 |
.iter() |
|
13486 | 172 |
.filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready()) |
14478 | 173 |
.map(|(_, c)| c.nick.clone()) |
174 |
.collect(); |
|
13424 | 175 |
if !nicks.is_empty() { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
176 |
/*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
|
177 |
; |
13424 | 178 |
} |
179 |
} |
|
180 |
} |
|
13450 | 181 |
} |
13416 | 182 |
ChangeMaster(room_id, new_id) => { |
183 |
let room_client_ids = server.room_clients(room_id); |
|
14478 | 184 |
let new_id = if server |
185 |
.room(client_id) |
|
186 |
.map(|r| r.is_fixed()) |
|
187 |
.unwrap_or(false) |
|
188 |
{ |
|
13447 | 189 |
new_id |
190 |
} else { |
|
14478 | 191 |
new_id.or_else(|| room_client_ids.iter().find(|id| **id != client_id).cloned()) |
13447 | 192 |
}; |
13416 | 193 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
194 |
||
13419 | 195 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
196 |
match r.master_id { |
|
197 |
Some(id) if id == c.id => { |
|
13486 | 198 |
c.set_is_master(false); |
13419 | 199 |
r.master_id = None; |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
200 |
/*actions.push( |
14478 | 201 |
ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
202 |
.send_all() |
|
203 |
.in_room(r.id) |
|
204 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
205 |
);*/ |
13419 | 206 |
} |
207 |
Some(_) => unreachable!(), |
|
208 |
None => {} |
|
13416 | 209 |
} |
210 |
r.master_id = new_id; |
|
13775 | 211 |
if !r.is_fixed() && c.protocol_number < 42 { |
14478 | 212 |
r.name |
213 |
.replace_range(.., new_nick.as_ref().map_or("[]", String::as_str)); |
|
13775 | 214 |
} |
13494 | 215 |
r.set_join_restriction(false); |
216 |
r.set_team_add_restriction(false); |
|
217 |
let is_fixed = r.is_fixed(); |
|
218 |
r.set_unregistered_players_restriction(is_fixed); |
|
13416 | 219 |
if let Some(nick) = new_nick { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
220 |
/*actions.push( |
14478 | 221 |
ClientFlags("+h".to_string(), vec![nick]) |
222 |
.send_all() |
|
223 |
.in_room(r.id) |
|
224 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
225 |
);*/ |
13416 | 226 |
} |
227 |
} |
|
13666 | 228 |
if let Some(id) = new_id { |
229 |
server.clients[id].set_is_master(true) |
|
230 |
} |
|
14478 | 231 |
} |
13423 | 232 |
StartRoomGame(room_id) => { |
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
233 |
let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
234 |
.clients |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
235 |
.iter() |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
236 |
.map(|(id, c)| (id, c.nick.clone())) |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
237 |
.unzip(); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
238 |
let room = &mut server.rooms[room_id]; |
13423 | 239 |
|
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
240 |
if !room.has_multiple_clans() { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
241 |
/*Warn( |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
242 |
"The game can't be started with less than two clans!".to_string(), |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
243 |
)*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
244 |
} else if room.protocol_number <= 43 && room.players_number != room.ready_players_number |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
245 |
{ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
246 |
/*Warn("Not all players are ready".to_string())*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
247 |
} else if room.game_info.is_some() { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
248 |
/*Warn("The game is already in progress".to_string())*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
249 |
} else { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
250 |
room.start_round(); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
251 |
for id in room_clients { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
252 |
let c = &mut server.clients[id]; |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
253 |
c.set_is_in_game(false); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
254 |
c.team_indices = room.client_team_indices(c.id); |
13423 | 255 |
} |
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
256 |
/*RunGame.send_all().in_room(room.id).action(),*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
257 |
//SendRoomUpdate(None), |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
258 |
/*ClientFlags("+g".to_string(), room_nicks) |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
259 |
.send_all() |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
260 |
.in_room(room.id) |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
261 |
.action(),*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
262 |
} |
13416 | 263 |
} |
13423 | 264 |
SendTeamRemovalMessage(team_name) => { |
265 |
let mut actions = Vec::new(); |
|
13521 | 266 |
if let Some(r) = server.room(client_id) { |
13423 | 267 |
if let Some(ref mut info) = r.game_info { |
268 |
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
|
269 |
/*actions.push( |
14478 | 270 |
ForwardEngineMessage(vec![to_engine_msg(msg)]) |
271 |
.send_all() |
|
272 |
.in_room(r.id) |
|
273 |
.but_self() |
|
274 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
275 |
);*/ |
13423 | 276 |
info.teams_in_game -= 1; |
277 |
if info.teams_in_game == 0 { |
|
278 |
actions.push(FinishRoomGame(r.id)); |
|
279 |
} |
|
13427 | 280 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
13443 | 281 |
if let Some(m) = &info.sync_msg { |
282 |
info.msg_log.push(m.clone()); |
|
13427 | 283 |
} |
13443 | 284 |
if info.sync_msg.is_some() { |
285 |
info.sync_msg = None |
|
286 |
} |
|
287 |
info.msg_log.push(remove_msg.clone()); |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
288 |
/*actions.push( |
14478 | 289 |
ForwardEngineMessage(vec![remove_msg]) |
290 |
.send_all() |
|
291 |
.in_room(r.id) |
|
292 |
.but_self() |
|
293 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
294 |
);*/ |
13423 | 295 |
} |
296 |
} |
|
297 |
} |
|
298 |
FinishRoomGame(room_id) => { |
|
13426 | 299 |
let mut actions = Vec::new(); |
300 |
||
13775 | 301 |
let r = &mut server.rooms[room_id]; |
302 |
r.ready_players_number = 1; |
|
14707
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14704
diff
changeset
|
303 |
//actions.push(SendRoomUpdate(None)); |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
304 |
//actions.push(RoundFinished.send_all().in_room(r.id).action()); |
13775 | 305 |
|
306 |
if let Some(info) = replace(&mut r.game_info, None) { |
|
13426 | 307 |
for (_, c) in server.clients.iter() { |
13486 | 308 |
if c.room_id == Some(room_id) && c.is_joined_mid_game() { |
14478 | 309 |
actions.push(SendRoomData { |
310 |
to: c.id, |
|
311 |
teams: false, |
|
312 |
config: true, |
|
313 |
flags: false, |
|
314 |
}); |
|
13500 | 315 |
for name in &info.left_teams { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
316 |
//actions.push(TeamRemove(name.clone()).send(c.id).action()); |
13426 | 317 |
} |
318 |
} |
|
319 |
} |
|
320 |
} |
|
321 |
||
14478 | 322 |
let nicks: Vec<_> = server |
323 |
.clients |
|
324 |
.iter_mut() |
|
13426 | 325 |
.filter(|(_, c)| c.room_id == Some(room_id)) |
326 |
.map(|(_, c)| { |
|
13666 | 327 |
c.set_is_ready(c.is_master()); |
13486 | 328 |
c.set_is_joined_mid_game(false); |
13426 | 329 |
c |
14478 | 330 |
}) |
331 |
.filter_map(|c| { |
|
332 |
if !c.is_master() { |
|
333 |
Some(c.nick.clone()) |
|
334 |
} else { |
|
335 |
None |
|
336 |
} |
|
337 |
}) |
|
338 |
.collect(); |
|
13775 | 339 |
|
13426 | 340 |
if !nicks.is_empty() { |
13775 | 341 |
let msg = if r.protocol_number < 38 { |
342 |
LegacyReady(false, nicks) |
|
343 |
} else { |
|
344 |
ClientFlags("-r".to_string(), nicks) |
|
345 |
}; |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
346 |
//actions.push(msg.send_all().in_room(room_id).action()); |
13426 | 347 |
} |
13423 | 348 |
} |
12144 | 349 |
} |
350 |
} |