author | alfadur <mail@none> |
Wed, 06 Feb 2019 00:57:01 +0300 | |
changeset 14687 | 5122c584804e |
parent 14686 | 9f98086de1b6 |
child 14688 | 4569d8d50286 |
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>), |
13423 | 106 |
StartRoomGame(RoomId), |
107 |
SendTeamRemovalMessage(String), |
|
108 |
FinishRoomGame(RoomId), |
|
14457 | 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 { |
14457 | 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 { |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
130 |
/* actions.push( |
14457 | 131 |
ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config()) |
132 |
.send(to) |
|
133 |
.action(), |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
134 |
)*/ |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
135 |
; |
13524 | 136 |
for cfg in r.game_config() { |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
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, |
|
14457 | 143 |
None => &r.teams, |
13427 | 144 |
}; |
145 |
for (owner_id, team) in current_teams.iter() { |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
146 |
/*actions.push( |
14457 | 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(), |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
156 |
);*/ |
13424 | 157 |
} |
158 |
} |
|
159 |
if flags { |
|
160 |
if let Some(id) = r.master_id { |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
161 |
/* |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
162 |
actions.push( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
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:
14675
diff
changeset
|
164 |
.send(to) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
165 |
.action(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
166 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
167 |
*/ |
13424 | 168 |
} |
14457 | 169 |
let nicks: Vec<_> = server |
170 |
.clients |
|
171 |
.iter() |
|
13520 | 172 |
.filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready()) |
14457 | 173 |
.map(|(_, c)| c.nick.clone()) |
174 |
.collect(); |
|
13424 | 175 |
if !nicks.is_empty() { |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
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:
14675
diff
changeset
|
177 |
; |
13424 | 178 |
} |
179 |
} |
|
180 |
} |
|
13478 | 181 |
} |
13416 | 182 |
ChangeMaster(room_id, new_id) => { |
183 |
let room_client_ids = server.room_clients(room_id); |
|
14457 | 184 |
let new_id = if server |
185 |
.room(client_id) |
|
186 |
.map(|r| r.is_fixed()) |
|
187 |
.unwrap_or(false) |
|
188 |
{ |
|
13477 | 189 |
new_id |
190 |
} else { |
|
14457 | 191 |
new_id.or_else(|| room_client_ids.iter().find(|id| **id != client_id).cloned()) |
13477 | 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 => { |
|
13520 | 198 |
c.set_is_master(false); |
13419 | 199 |
r.master_id = None; |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
200 |
/*actions.push( |
14457 | 201 |
ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
202 |
.send_all() |
|
203 |
.in_room(r.id) |
|
204 |
.action(), |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
205 |
);*/ |
13419 | 206 |
} |
207 |
Some(_) => unreachable!(), |
|
208 |
None => {} |
|
13416 | 209 |
} |
210 |
r.master_id = new_id; |
|
13801 | 211 |
if !r.is_fixed() && c.protocol_number < 42 { |
14457 | 212 |
r.name |
213 |
.replace_range(.., new_nick.as_ref().map_or("[]", String::as_str)); |
|
13801 | 214 |
} |
13523 | 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 { |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
220 |
/*actions.push( |
14457 | 221 |
ClientFlags("+h".to_string(), vec![nick]) |
222 |
.send_all() |
|
223 |
.in_room(r.id) |
|
224 |
.action(), |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
225 |
);*/ |
13416 | 226 |
} |
227 |
} |
|
13666 | 228 |
if let Some(id) = new_id { |
229 |
server.clients[id].set_is_master(true) |
|
230 |
} |
|
14457 | 231 |
} |
13423 | 232 |
StartRoomGame(room_id) => { |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
233 |
let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
234 |
.clients |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
235 |
.iter() |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
236 |
.map(|(id, c)| (id, c.nick.clone())) |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
237 |
.unzip(); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
238 |
let room = &mut server.rooms[room_id]; |
13423 | 239 |
|
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
240 |
if !room.has_multiple_clans() { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
241 |
/*Warn( |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
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:
14683
diff
changeset
|
243 |
)*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
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:
14683
diff
changeset
|
245 |
{ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
246 |
/*Warn("Not all players are ready".to_string())*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
247 |
} else if room.game_info.is_some() { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
248 |
/*Warn("The game is already in progress".to_string())*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
249 |
} else { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
250 |
room.start_round(); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
251 |
for id in room_clients { |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
252 |
let c = &mut server.clients[id]; |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
253 |
c.set_is_in_game(false); |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
254 |
c.team_indices = room.client_team_indices(c.id); |
13423 | 255 |
} |
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
256 |
/*RunGame.send_all().in_room(room.id).action(),*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
257 |
//SendRoomUpdate(None), |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
258 |
/*ClientFlags("+g".to_string(), room_nicks) |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
259 |
.send_all() |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
260 |
.in_room(room.id) |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
261 |
.action(),*/ |
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
262 |
} |
13416 | 263 |
} |
13423 | 264 |
SendTeamRemovalMessage(team_name) => { |
265 |
let mut actions = Vec::new(); |
|
13527 | 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()); |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
269 |
/*actions.push( |
14457 | 270 |
ForwardEngineMessage(vec![to_engine_msg(msg)]) |
271 |
.send_all() |
|
272 |
.in_room(r.id) |
|
273 |
.but_self() |
|
274 |
.action(), |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
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()); |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
288 |
/*actions.push( |
14457 | 289 |
ForwardEngineMessage(vec![remove_msg]) |
290 |
.send_all() |
|
291 |
.in_room(r.id) |
|
292 |
.but_self() |
|
293 |
.action(), |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
294 |
);*/ |
13423 | 295 |
} |
296 |
} |
|
297 |
} |
|
298 |
FinishRoomGame(room_id) => { |
|
13426 | 299 |
let mut actions = Vec::new(); |
300 |
||
13801 | 301 |
let r = &mut server.rooms[room_id]; |
302 |
r.ready_players_number = 1; |
|
14686
9f98086de1b6
Server action refactoring part 9 of N
alfadur <mail@none>
parents:
14683
diff
changeset
|
303 |
//actions.push(SendRoomUpdate(None)); |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
304 |
//actions.push(RoundFinished.send_all().in_room(r.id).action()); |
13801 | 305 |
|
306 |
if let Some(info) = replace(&mut r.game_info, None) { |
|
13426 | 307 |
for (_, c) in server.clients.iter() { |
13520 | 308 |
if c.room_id == Some(room_id) && c.is_joined_mid_game() { |
14457 | 309 |
actions.push(SendRoomData { |
310 |
to: c.id, |
|
311 |
teams: false, |
|
312 |
config: true, |
|
313 |
flags: false, |
|
314 |
}); |
|
13524 | 315 |
for name in &info.left_teams { |
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
316 |
//actions.push(TeamRemove(name.clone()).send(c.id).action()); |
13426 | 317 |
} |
318 |
} |
|
319 |
} |
|
320 |
} |
|
321 |
||
14457 | 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()); |
13520 | 328 |
c.set_is_joined_mid_game(false); |
13426 | 329 |
c |
14457 | 330 |
}) |
331 |
.filter_map(|c| { |
|
332 |
if !c.is_master() { |
|
333 |
Some(c.nick.clone()) |
|
334 |
} else { |
|
335 |
None |
|
336 |
} |
|
337 |
}) |
|
338 |
.collect(); |
|
13801 | 339 |
|
13426 | 340 |
if !nicks.is_empty() { |
13801 | 341 |
let msg = if r.protocol_number < 38 { |
342 |
LegacyReady(false, nicks) |
|
343 |
} else { |
|
344 |
ClientFlags("-r".to_string(), nicks) |
|
345 |
}; |
|
14683
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14675
diff
changeset
|
346 |
//actions.push(msg.send_all().in_room(room_id).action()); |
13426 | 347 |
} |
13423 | 348 |
} |
12144 | 349 |
} |
350 |
} |