author | alfadur <mail@none> |
Wed, 06 Feb 2019 00:57:01 +0300 | |
changeset 14692 | 5122c584804e |
parent 14691 | 9f98086de1b6 |
child 14693 | 4569d8d50286 |
permissions | -rw-r--r-- |
13421 | 1 |
use super::{ |
14462 | 2 |
client::HWClient, |
14379 | 3 |
core::HWServer, |
14462 | 4 |
coretypes::{ClientId, GameCfg, RoomId, VoteType}, |
5 |
handlers, |
|
6 |
room::HWRoom, |
|
13528 | 7 |
room::{GameInfo, RoomFlags}, |
13421 | 8 |
}; |
13671 | 9 |
use crate::{ |
14462 | 10 |
protocol::messages::{server_chat, HWProtocolMessage, HWServerMessage, HWServerMessage::*}, |
11 |
utils::to_engine_msg, |
|
13421 | 12 |
}; |
14462 | 13 |
use rand::{distributions::Uniform, thread_rng, Rng}; |
14 |
use std::{io, io::Write, iter::once, mem::replace}; |
|
12143 | 15 |
|
14461 | 16 |
#[cfg(feature = "official-server")] |
17 |
use super::database; |
|
18 |
||
13424 | 19 |
pub enum Destination { |
13431 | 20 |
ToId(ClientId), |
13424 | 21 |
ToSelf, |
13428 | 22 |
ToAll { |
13424 | 23 |
room_id: Option<RoomId>, |
13525 | 24 |
protocol: Option<u16>, |
14462 | 25 |
skip_self: bool, |
26 |
}, |
|
13424 | 27 |
} |
28 |
||
29 |
pub struct PendingMessage { |
|
30 |
pub destination: Destination, |
|
14462 | 31 |
pub message: HWServerMessage, |
13424 | 32 |
} |
33 |
||
34 |
impl PendingMessage { |
|
13431 | 35 |
pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage { |
14462 | 36 |
PendingMessage { |
37 |
destination: Destination::ToId(client_id), |
|
38 |
message, |
|
39 |
} |
|
13431 | 40 |
} |
41 |
||
13424 | 42 |
pub fn send_self(message: HWServerMessage) -> PendingMessage { |
14462 | 43 |
PendingMessage { |
44 |
destination: Destination::ToSelf, |
|
45 |
message, |
|
46 |
} |
|
13424 | 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 |
}; |
|
14462 | 55 |
PendingMessage { |
56 |
destination, |
|
57 |
message, |
|
58 |
} |
|
13424 | 59 |
} |
60 |
||
61 |
pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage { |
|
14462 | 62 |
if let Destination::ToAll { |
63 |
ref mut room_id, .. |
|
64 |
} = self.destination |
|
65 |
{ |
|
13424 | 66 |
*room_id = Some(clients_room_id) |
67 |
} |
|
68 |
self |
|
69 |
} |
|
70 |
||
13525 | 71 |
pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage { |
14462 | 72 |
if let Destination::ToAll { |
73 |
ref mut protocol, .. |
|
74 |
} = self.destination |
|
75 |
{ |
|
13424 | 76 |
*protocol = Some(protocol_number) |
77 |
} |
|
78 |
self |
|
79 |
} |
|
80 |
||
81 |
pub fn but_self(mut self) -> PendingMessage { |
|
14462 | 82 |
if let Destination::ToAll { |
83 |
ref mut skip_self, .. |
|
84 |
} = self.destination |
|
85 |
{ |
|
13424 | 86 |
*skip_self = true |
87 |
} |
|
88 |
self |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
impl HWServerMessage { |
|
14462 | 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 |
} |
|
13424 | 102 |
} |
103 |
||
12143 | 104 |
pub enum Action { |
13421 | 105 |
ChangeMaster(RoomId, Option<ClientId>), |
13428 | 106 |
StartRoomGame(RoomId), |
107 |
SendTeamRemovalMessage(String), |
|
108 |
FinishRoomGame(RoomId), |
|
14462 | 109 |
SendRoomData { |
110 |
to: ClientId, |
|
111 |
teams: bool, |
|
112 |
config: bool, |
|
113 |
flags: bool, |
|
114 |
}, |
|
12143 | 115 |
} |
12149 | 116 |
|
117 |
use self::Action::*; |
|
118 |
||
13424 | 119 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12149 | 120 |
match action { |
14462 | 121 |
SendRoomData { |
122 |
to, |
|
123 |
teams, |
|
124 |
config, |
|
125 |
flags, |
|
126 |
} => { |
|
13429 | 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 { |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
130 |
/* actions.push( |
14462 | 131 |
ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config()) |
132 |
.send(to) |
|
133 |
.action(), |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
134 |
)*/ |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
135 |
; |
13529 | 136 |
for cfg in r.game_config() { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
137 |
//actions.push(cfg.to_server_msg().send(to).action()); |
13429 | 138 |
} |
139 |
} |
|
140 |
if teams { |
|
13432 | 141 |
let current_teams = match r.game_info { |
142 |
Some(ref info) => &info.teams_at_start, |
|
14462 | 143 |
None => &r.teams, |
13432 | 144 |
}; |
145 |
for (owner_id, team) in current_teams.iter() { |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
146 |
/*actions.push( |
14462 | 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(), |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
156 |
);*/ |
13429 | 157 |
} |
158 |
} |
|
159 |
if flags { |
|
160 |
if let Some(id) = r.master_id { |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
161 |
/* |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
162 |
actions.push( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
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:
14680
diff
changeset
|
164 |
.send(to) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
165 |
.action(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
166 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
167 |
*/ |
13429 | 168 |
} |
14462 | 169 |
let nicks: Vec<_> = server |
170 |
.clients |
|
171 |
.iter() |
|
13525 | 172 |
.filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready()) |
14462 | 173 |
.map(|(_, c)| c.nick.clone()) |
174 |
.collect(); |
|
13429 | 175 |
if !nicks.is_empty() { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
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:
14680
diff
changeset
|
177 |
; |
13429 | 178 |
} |
179 |
} |
|
180 |
} |
|
13483 | 181 |
} |
13421 | 182 |
ChangeMaster(room_id, new_id) => { |
183 |
let room_client_ids = server.room_clients(room_id); |
|
14462 | 184 |
let new_id = if server |
185 |
.room(client_id) |
|
186 |
.map(|r| r.is_fixed()) |
|
187 |
.unwrap_or(false) |
|
188 |
{ |
|
13482 | 189 |
new_id |
190 |
} else { |
|
14462 | 191 |
new_id.or_else(|| room_client_ids.iter().find(|id| **id != client_id).cloned()) |
13482 | 192 |
}; |
13421 | 193 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
194 |
||
13424 | 195 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
196 |
match r.master_id { |
|
197 |
Some(id) if id == c.id => { |
|
13525 | 198 |
c.set_is_master(false); |
13424 | 199 |
r.master_id = None; |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
200 |
/*actions.push( |
14462 | 201 |
ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
202 |
.send_all() |
|
203 |
.in_room(r.id) |
|
204 |
.action(), |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
205 |
);*/ |
13424 | 206 |
} |
207 |
Some(_) => unreachable!(), |
|
208 |
None => {} |
|
13421 | 209 |
} |
210 |
r.master_id = new_id; |
|
13806 | 211 |
if !r.is_fixed() && c.protocol_number < 42 { |
14462 | 212 |
r.name |
213 |
.replace_range(.., new_nick.as_ref().map_or("[]", String::as_str)); |
|
13806 | 214 |
} |
13528 | 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); |
|
13421 | 219 |
if let Some(nick) = new_nick { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
220 |
/*actions.push( |
14462 | 221 |
ClientFlags("+h".to_string(), vec![nick]) |
222 |
.send_all() |
|
223 |
.in_room(r.id) |
|
224 |
.action(), |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
225 |
);*/ |
13421 | 226 |
} |
227 |
} |
|
13671 | 228 |
if let Some(id) = new_id { |
229 |
server.clients[id].set_is_master(true) |
|
230 |
} |
|
14462 | 231 |
} |
13428 | 232 |
StartRoomGame(room_id) => { |
14691
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
233 |
let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
234 |
.clients |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
235 |
.iter() |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
236 |
.map(|(id, c)| (id, c.nick.clone())) |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
237 |
.unzip(); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
238 |
let room = &mut server.rooms[room_id]; |
13428 | 239 |
|
14691
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
240 |
if !room.has_multiple_clans() { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
241 |
/*Warn( |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
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:
14688
diff
changeset
|
243 |
)*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
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:
14688
diff
changeset
|
245 |
{ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
246 |
/*Warn("Not all players are ready".to_string())*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
247 |
} else if room.game_info.is_some() { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
248 |
/*Warn("The game is already in progress".to_string())*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
249 |
} else { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
250 |
room.start_round(); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
251 |
for id in room_clients { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
252 |
let c = &mut server.clients[id]; |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
253 |
c.set_is_in_game(false); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
254 |
c.team_indices = room.client_team_indices(c.id); |
13428 | 255 |
} |
14691
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
256 |
/*RunGame.send_all().in_room(room.id).action(),*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
257 |
//SendRoomUpdate(None), |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
258 |
/*ClientFlags("+g".to_string(), room_nicks) |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
259 |
.send_all() |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
260 |
.in_room(room.id) |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
261 |
.action(),*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
262 |
} |
13421 | 263 |
} |
13428 | 264 |
SendTeamRemovalMessage(team_name) => { |
265 |
let mut actions = Vec::new(); |
|
13532 | 266 |
if let Some(r) = server.room(client_id) { |
13428 | 267 |
if let Some(ref mut info) = r.game_info { |
268 |
let msg = once(b'F').chain(team_name.bytes()); |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
269 |
/*actions.push( |
14462 | 270 |
ForwardEngineMessage(vec![to_engine_msg(msg)]) |
271 |
.send_all() |
|
272 |
.in_room(r.id) |
|
273 |
.but_self() |
|
274 |
.action(), |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
275 |
);*/ |
13428 | 276 |
info.teams_in_game -= 1; |
277 |
if info.teams_in_game == 0 { |
|
278 |
actions.push(FinishRoomGame(r.id)); |
|
279 |
} |
|
13432 | 280 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
13448 | 281 |
if let Some(m) = &info.sync_msg { |
282 |
info.msg_log.push(m.clone()); |
|
13432 | 283 |
} |
13448 | 284 |
if info.sync_msg.is_some() { |
285 |
info.sync_msg = None |
|
286 |
} |
|
287 |
info.msg_log.push(remove_msg.clone()); |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
288 |
/*actions.push( |
14462 | 289 |
ForwardEngineMessage(vec![remove_msg]) |
290 |
.send_all() |
|
291 |
.in_room(r.id) |
|
292 |
.but_self() |
|
293 |
.action(), |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
294 |
);*/ |
13428 | 295 |
} |
296 |
} |
|
297 |
} |
|
298 |
FinishRoomGame(room_id) => { |
|
13431 | 299 |
let mut actions = Vec::new(); |
300 |
||
13806 | 301 |
let r = &mut server.rooms[room_id]; |
302 |
r.ready_players_number = 1; |
|
14691
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14688
diff
changeset
|
303 |
//actions.push(SendRoomUpdate(None)); |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
304 |
//actions.push(RoundFinished.send_all().in_room(r.id).action()); |
13806 | 305 |
|
306 |
if let Some(info) = replace(&mut r.game_info, None) { |
|
13431 | 307 |
for (_, c) in server.clients.iter() { |
13525 | 308 |
if c.room_id == Some(room_id) && c.is_joined_mid_game() { |
14462 | 309 |
actions.push(SendRoomData { |
310 |
to: c.id, |
|
311 |
teams: false, |
|
312 |
config: true, |
|
313 |
flags: false, |
|
314 |
}); |
|
13529 | 315 |
for name in &info.left_teams { |
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
316 |
//actions.push(TeamRemove(name.clone()).send(c.id).action()); |
13431 | 317 |
} |
318 |
} |
|
319 |
} |
|
320 |
} |
|
321 |
||
14462 | 322 |
let nicks: Vec<_> = server |
323 |
.clients |
|
324 |
.iter_mut() |
|
13431 | 325 |
.filter(|(_, c)| c.room_id == Some(room_id)) |
326 |
.map(|(_, c)| { |
|
13671 | 327 |
c.set_is_ready(c.is_master()); |
13525 | 328 |
c.set_is_joined_mid_game(false); |
13431 | 329 |
c |
14462 | 330 |
}) |
331 |
.filter_map(|c| { |
|
332 |
if !c.is_master() { |
|
333 |
Some(c.nick.clone()) |
|
334 |
} else { |
|
335 |
None |
|
336 |
} |
|
337 |
}) |
|
338 |
.collect(); |
|
13806 | 339 |
|
13431 | 340 |
if !nicks.is_empty() { |
13806 | 341 |
let msg = if r.protocol_number < 38 { |
342 |
LegacyReady(false, nicks) |
|
343 |
} else { |
|
344 |
ClientFlags("-r".to_string(), nicks) |
|
345 |
}; |
|
14688
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14680
diff
changeset
|
346 |
//actions.push(msg.send_all().in_room(room_id).action()); |
13431 | 347 |
} |
13428 | 348 |
} |
12149 | 349 |
} |
350 |
} |