author | alfadur <mail@none> |
Mon, 04 Feb 2019 20:25:35 +0300 | |
changeset 14694 | 08a8605bafaf |
parent 14692 | 455865ccd36c |
child 14696 | dfe652c53470 |
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 |
||
14478 | 91 |
pub fn action(self) -> Action { |
92 |
Send(self) |
|
93 |
} |
|
13419 | 94 |
} |
95 |
||
96 |
impl Into<Action> for PendingMessage { |
|
14478 | 97 |
fn into(self) -> Action { |
98 |
self.action() |
|
99 |
} |
|
13419 | 100 |
} |
101 |
||
102 |
impl HWServerMessage { |
|
14478 | 103 |
pub fn send(self, client_id: ClientId) -> PendingMessage { |
104 |
PendingMessage::send(self, client_id) |
|
105 |
} |
|
106 |
pub fn send_self(self) -> PendingMessage { |
|
107 |
PendingMessage::send_self(self) |
|
108 |
} |
|
109 |
pub fn send_all(self) -> PendingMessage { |
|
110 |
PendingMessage::send_all(self) |
|
111 |
} |
|
13419 | 112 |
} |
113 |
||
12138 | 114 |
pub enum Action { |
13419 | 115 |
Send(PendingMessage), |
12145 | 116 |
CheckRegistered, |
117 |
JoinLobby, |
|
13416 | 118 |
RemoveRoom(RoomId), |
119 |
MoveToRoom(RoomId), |
|
120 |
MoveToLobby(String), |
|
121 |
ChangeMaster(RoomId, Option<ClientId>), |
|
13419 | 122 |
RemoveTeam(String), |
123 |
RemoveClientTeams, |
|
13416 | 124 |
SendRoomUpdate(Option<String>), |
13423 | 125 |
StartRoomGame(RoomId), |
126 |
SendTeamRemovalMessage(String), |
|
127 |
FinishRoomGame(RoomId), |
|
14478 | 128 |
SendRoomData { |
129 |
to: ClientId, |
|
130 |
teams: bool, |
|
131 |
config: bool, |
|
132 |
flags: bool, |
|
133 |
}, |
|
134 |
AddVote { |
|
135 |
vote: bool, |
|
136 |
is_forced: bool, |
|
137 |
}, |
|
13450 | 138 |
ApplyVoting(VoteType, RoomId), |
12147 | 139 |
Warn(String), |
14478 | 140 |
ProtocolError(String), |
12138 | 141 |
} |
12144 | 142 |
|
143 |
use self::Action::*; |
|
144 |
||
13419 | 145 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12144 | 146 |
match action { |
13500 | 147 |
Send(msg) => server.send(client_id, &msg.destination, msg.message), |
13775 | 148 |
CheckRegistered => { |
149 |
let client = &server.clients[client_id]; |
|
150 |
if client.protocol_number > 0 && client.nick != "" { |
|
14478 | 151 |
let has_nick_clash = server |
152 |
.clients |
|
153 |
.iter() |
|
154 |
.any(|(id, c)| id != client_id && c.nick == client.nick); |
|
13775 | 155 |
|
156 |
let actions = if !client.is_checker() && has_nick_clash { |
|
157 |
if client.protocol_number < 38 { |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
158 |
//ByeClient("Nickname is already in use".to_string()) |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
159 |
vec![] |
13775 | 160 |
} else { |
161 |
server.clients[client_id].nick.clear(); |
|
162 |
vec![Notice("NickAlreadyInUse".to_string()).send_self().action()] |
|
163 |
} |
|
164 |
} else { |
|
165 |
vec![JoinLobby] |
|
166 |
}; |
|
167 |
server.react(client_id, actions); |
|
168 |
} |
|
14478 | 169 |
} |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
170 |
JoinLobby => { |
13419 | 171 |
server.clients[client_id].room_id = Some(server.lobby_id); |
12147 | 172 |
|
13666 | 173 |
let mut lobby_nicks = Vec::new(); |
174 |
for (_, c) in server.clients.iter() { |
|
175 |
if c.room_id.is_some() { |
|
176 |
lobby_nicks.push(c.nick.clone()); |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
177 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
178 |
} |
13666 | 179 |
let joined_msg = LobbyJoined(lobby_nicks); |
180 |
||
13419 | 181 |
let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]); |
13416 | 182 |
let flags_msg = ClientFlags( |
183 |
"+i".to_string(), |
|
14478 | 184 |
server |
185 |
.clients |
|
186 |
.iter() |
|
13416 | 187 |
.filter(|(_, c)| c.room_id.is_some()) |
188 |
.map(|(_, c)| c.nick.clone()) |
|
14478 | 189 |
.collect(), |
190 |
); |
|
13416 | 191 |
let server_msg = ServerMessage("\u{1f994} is watching".to_string()); |
14478 | 192 |
let rooms_msg = Rooms( |
193 |
server |
|
194 |
.rooms |
|
195 |
.iter() |
|
196 |
.filter(|(id, _)| *id != server.lobby_id) |
|
197 |
.flat_map(|(_, r)| r.info(r.master_id.map(|id| &server.clients[id]))) |
|
198 |
.collect(), |
|
199 |
); |
|
200 |
server.react( |
|
201 |
client_id, |
|
202 |
vec![ |
|
203 |
everyone_msg.send_all().but_self().action(), |
|
204 |
joined_msg.send_self().action(), |
|
205 |
flags_msg.send_self().action(), |
|
206 |
server_msg.send_self().action(), |
|
207 |
rooms_msg.send_self().action(), |
|
208 |
], |
|
209 |
); |
|
210 |
} |
|
13416 | 211 |
RemoveRoom(room_id) => { |
13666 | 212 |
let r = &mut server.rooms[room_id]; |
14478 | 213 |
let actions = vec![RoomRemove(r.name.clone()) |
214 |
.send_all() |
|
215 |
.with_protocol(r.protocol_number) |
|
216 |
.action()]; |
|
13416 | 217 |
server.rooms.remove(room_id); |
13419 | 218 |
server.react(client_id, actions); |
13416 | 219 |
} |
220 |
MoveToRoom(room_id) => { |
|
13666 | 221 |
let r = &mut server.rooms[room_id]; |
222 |
let c = &mut server.clients[client_id]; |
|
223 |
r.players_number += 1; |
|
224 |
c.room_id = Some(room_id); |
|
225 |
||
226 |
let is_master = r.master_id == Some(c.id); |
|
227 |
c.set_is_master(is_master); |
|
228 |
c.set_is_ready(is_master); |
|
229 |
c.set_is_joined_mid_game(false); |
|
230 |
||
231 |
if is_master { |
|
232 |
r.ready_players_number += 1; |
|
233 |
} |
|
234 |
||
235 |
let mut v = vec![ |
|
14478 | 236 |
RoomJoined(vec![c.nick.clone()]) |
237 |
.send_all() |
|
238 |
.in_room(room_id) |
|
239 |
.action(), |
|
240 |
ClientFlags("+i".to_string(), vec![c.nick.clone()]) |
|
241 |
.send_all() |
|
242 |
.action(), |
|
243 |
SendRoomUpdate(None), |
|
244 |
]; |
|
13666 | 245 |
|
246 |
if !r.greeting.is_empty() { |
|
14478 | 247 |
v.push( |
248 |
ChatMsg { |
|
249 |
nick: "[greeting]".to_string(), |
|
250 |
msg: r.greeting.clone(), |
|
251 |
} |
|
252 |
.send_self() |
|
253 |
.action(), |
|
254 |
); |
|
13666 | 255 |
} |
13486 | 256 |
|
13666 | 257 |
if !c.is_master() { |
258 |
let team_names: Vec<_>; |
|
259 |
if let Some(ref mut info) = r.game_info { |
|
260 |
c.set_is_in_game(true); |
|
261 |
c.set_is_joined_mid_game(true); |
|
13486 | 262 |
|
13666 | 263 |
{ |
264 |
let teams = info.client_teams(c.id); |
|
265 |
c.teams_in_game = teams.clone().count() as u8; |
|
266 |
c.clan = teams.clone().next().map(|t| t.color); |
|
267 |
team_names = teams.map(|t| t.name.clone()).collect(); |
|
268 |
} |
|
269 |
||
270 |
if !team_names.is_empty() { |
|
14478 | 271 |
info.left_teams.retain(|name| !team_names.contains(&name)); |
13666 | 272 |
info.teams_in_game += team_names.len() as u8; |
14478 | 273 |
r.teams = info |
274 |
.teams_at_start |
|
275 |
.iter() |
|
13666 | 276 |
.filter(|(_, t)| !team_names.contains(&t.name)) |
14478 | 277 |
.cloned() |
278 |
.collect(); |
|
13666 | 279 |
} |
280 |
} else { |
|
281 |
team_names = Vec::new(); |
|
13416 | 282 |
} |
13419 | 283 |
|
14478 | 284 |
v.push(SendRoomData { |
285 |
to: client_id, |
|
286 |
teams: true, |
|
287 |
config: true, |
|
288 |
flags: true, |
|
289 |
}); |
|
13427 | 290 |
|
13666 | 291 |
if let Some(ref info) = r.game_info { |
292 |
v.push(RunGame.send_self().action()); |
|
14478 | 293 |
v.push( |
294 |
ClientFlags("+g".to_string(), vec![c.nick.clone()]) |
|
295 |
.send_all() |
|
296 |
.in_room(r.id) |
|
297 |
.action(), |
|
298 |
); |
|
299 |
v.push( |
|
300 |
ForwardEngineMessage(vec![to_engine_msg("e$spectate 1".bytes())]) |
|
301 |
.send_self() |
|
302 |
.action(), |
|
303 |
); |
|
304 |
v.push( |
|
305 |
ForwardEngineMessage(info.msg_log.clone()) |
|
306 |
.send_self() |
|
307 |
.action(), |
|
308 |
); |
|
13427 | 309 |
|
13666 | 310 |
for name in &team_names { |
14478 | 311 |
v.push( |
312 |
ForwardEngineMessage(vec![to_engine_msg( |
|
313 |
once(b'G').chain(name.bytes()), |
|
314 |
)]) |
|
315 |
.send_all() |
|
316 |
.in_room(r.id) |
|
317 |
.action(), |
|
318 |
); |
|
13666 | 319 |
} |
320 |
if info.is_paused { |
|
14478 | 321 |
v.push( |
322 |
ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
|
323 |
.send_all() |
|
324 |
.in_room(r.id) |
|
325 |
.action(), |
|
326 |
) |
|
13427 | 327 |
} |
13422 | 328 |
} |
13666 | 329 |
} |
330 |
server.react(client_id, v); |
|
13424 | 331 |
} |
14478 | 332 |
SendRoomData { |
333 |
to, |
|
334 |
teams, |
|
335 |
config, |
|
336 |
flags, |
|
337 |
} => { |
|
13424 | 338 |
let mut actions = Vec::new(); |
339 |
let room_id = server.clients[client_id].room_id; |
|
340 |
if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) { |
|
341 |
if config { |
|
14478 | 342 |
actions.push( |
343 |
ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config()) |
|
344 |
.send(to) |
|
345 |
.action(), |
|
346 |
); |
|
13500 | 347 |
for cfg in r.game_config() { |
13439 | 348 |
actions.push(cfg.to_server_msg().send(to).action()); |
13424 | 349 |
} |
350 |
} |
|
351 |
if teams { |
|
13427 | 352 |
let current_teams = match r.game_info { |
353 |
Some(ref info) => &info.teams_at_start, |
|
14478 | 354 |
None => &r.teams, |
13427 | 355 |
}; |
356 |
for (owner_id, team) in current_teams.iter() { |
|
14478 | 357 |
actions.push( |
358 |
TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team)) |
|
359 |
.send(to) |
|
360 |
.action(), |
|
361 |
); |
|
362 |
actions.push(TeamColor(team.name.clone(), team.color).send(to).action()); |
|
363 |
actions.push( |
|
364 |
HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
365 |
.send(to) |
|
366 |
.action(), |
|
367 |
); |
|
13424 | 368 |
} |
369 |
} |
|
370 |
if flags { |
|
371 |
if let Some(id) = r.master_id { |
|
14478 | 372 |
actions.push( |
373 |
ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()]) |
|
374 |
.send(to) |
|
375 |
.action(), |
|
376 |
); |
|
13424 | 377 |
} |
14478 | 378 |
let nicks: Vec<_> = server |
379 |
.clients |
|
380 |
.iter() |
|
13486 | 381 |
.filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready()) |
14478 | 382 |
.map(|(_, c)| c.nick.clone()) |
383 |
.collect(); |
|
13424 | 384 |
if !nicks.is_empty() { |
14478 | 385 |
actions.push(ClientFlags("+r".to_string(), nicks).send(to).action()); |
13424 | 386 |
} |
387 |
} |
|
388 |
} |
|
389 |
server.react(client_id, actions); |
|
390 |
} |
|
14478 | 391 |
AddVote { vote, is_forced } => { |
13450 | 392 |
let mut actions = Vec::new(); |
13521 | 393 |
if let Some(r) = server.room(client_id) { |
13450 | 394 |
let mut result = None; |
395 |
if let Some(ref mut voting) = r.voting { |
|
14371 | 396 |
if is_forced || voting.votes.iter().all(|(id, _)| client_id != *id) { |
14478 | 397 |
actions.push( |
398 |
server_chat("Your vote has been counted.".to_string()) |
|
399 |
.send_self() |
|
400 |
.action(), |
|
401 |
); |
|
13450 | 402 |
voting.votes.push((client_id, vote)); |
403 |
let i = voting.votes.iter(); |
|
404 |
let pro = i.clone().filter(|(_, v)| *v).count(); |
|
405 |
let contra = i.filter(|(_, v)| !*v).count(); |
|
406 |
let success_quota = voting.voters.len() / 2 + 1; |
|
407 |
if is_forced && vote || pro >= success_quota { |
|
408 |
result = Some(true); |
|
14478 | 409 |
} else if is_forced && !vote || contra > voting.voters.len() - success_quota |
410 |
{ |
|
13450 | 411 |
result = Some(false); |
412 |
} |
|
413 |
} else { |
|
14478 | 414 |
actions.push( |
415 |
server_chat("You already have voted.".to_string()) |
|
416 |
.send_self() |
|
417 |
.action(), |
|
418 |
); |
|
13450 | 419 |
} |
420 |
} else { |
|
14478 | 421 |
actions.push( |
422 |
server_chat("There's no voting going on.".to_string()) |
|
423 |
.send_self() |
|
424 |
.action(), |
|
425 |
); |
|
13450 | 426 |
} |
427 |
||
428 |
if let Some(res) = result { |
|
14478 | 429 |
actions.push( |
430 |
server_chat("Voting closed.".to_string()) |
|
431 |
.send_all() |
|
432 |
.in_room(r.id) |
|
433 |
.action(), |
|
434 |
); |
|
13450 | 435 |
let voting = replace(&mut r.voting, None).unwrap(); |
436 |
if res { |
|
437 |
actions.push(ApplyVoting(voting.kind, r.id)); |
|
438 |
} |
|
439 |
} |
|
440 |
} |
|
441 |
||
442 |
server.react(client_id, actions); |
|
443 |
} |
|
444 |
ApplyVoting(kind, room_id) => { |
|
445 |
let mut actions = Vec::new(); |
|
446 |
let mut id = client_id; |
|
447 |
match kind { |
|
448 |
VoteType::Kick(nick) => { |
|
449 |
if let Some(c) = server.find_client(&nick) { |
|
450 |
if c.room_id == Some(room_id) { |
|
451 |
id = c.id; |
|
452 |
actions.push(Kicked.send_self().action()); |
|
453 |
actions.push(MoveToLobby("kicked".to_string())); |
|
454 |
} |
|
455 |
} |
|
14478 | 456 |
} |
13521 | 457 |
VoteType::Map(None) => (), |
458 |
VoteType::Map(Some(name)) => { |
|
459 |
if let Some(location) = server.rooms[room_id].load_config(&name) { |
|
14478 | 460 |
actions.push( |
461 |
server_chat(location.to_string()) |
|
462 |
.send_all() |
|
463 |
.in_room(room_id) |
|
464 |
.action(), |
|
465 |
); |
|
13521 | 466 |
actions.push(SendRoomUpdate(None)); |
467 |
for (_, c) in server.clients.iter() { |
|
468 |
if c.room_id == Some(room_id) { |
|
14478 | 469 |
actions.push(SendRoomData { |
470 |
to: c.id, |
|
471 |
teams: false, |
|
472 |
config: true, |
|
473 |
flags: false, |
|
474 |
}) |
|
13521 | 475 |
} |
476 |
} |
|
477 |
} |
|
14478 | 478 |
} |
13450 | 479 |
VoteType::Pause => { |
13460 | 480 |
if let Some(ref mut info) = server.rooms[room_id].game_info { |
13450 | 481 |
info.is_paused = !info.is_paused; |
14478 | 482 |
actions.push( |
483 |
server_chat("Pause toggled.".to_string()) |
|
484 |
.send_all() |
|
485 |
.in_room(room_id) |
|
486 |
.action(), |
|
487 |
); |
|
488 |
actions.push( |
|
489 |
ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
|
490 |
.send_all() |
|
491 |
.in_room(room_id) |
|
492 |
.action(), |
|
493 |
); |
|
13450 | 494 |
} |
14478 | 495 |
} |
13450 | 496 |
VoteType::NewSeed => { |
13460 | 497 |
let seed = thread_rng().gen_range(0, 1_000_000_000).to_string(); |
498 |
let cfg = GameCfg::Seed(seed); |
|
499 |
actions.push(cfg.to_server_msg().send_all().in_room(room_id).action()); |
|
500 |
server.rooms[room_id].set_config(cfg); |
|
14478 | 501 |
} |
13450 | 502 |
VoteType::HedgehogsPerTeam(number) => { |
503 |
let r = &mut server.rooms[room_id]; |
|
504 |
let nicks = r.set_hedgehogs_number(number); |
|
14478 | 505 |
actions.extend(nicks.into_iter().map(|n| { |
506 |
HedgehogsNumber(n, number) |
|
507 |
.send_all() |
|
508 |
.in_room(room_id) |
|
509 |
.action() |
|
510 |
})); |
|
511 |
} |
|
13450 | 512 |
} |
513 |
server.react(id, actions); |
|
514 |
} |
|
13416 | 515 |
MoveToLobby(msg) => { |
516 |
let mut actions = Vec::new(); |
|
517 |
let lobby_id = server.lobby_id; |
|
13419 | 518 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13416 | 519 |
r.players_number -= 1; |
13486 | 520 |
if c.is_ready() && r.ready_players_number > 0 { |
13416 | 521 |
r.ready_players_number -= 1; |
522 |
} |
|
13494 | 523 |
if c.is_master() && (r.players_number > 0 || r.is_fixed()) { |
13416 | 524 |
actions.push(ChangeMaster(r.id, None)); |
525 |
} |
|
14478 | 526 |
actions.push( |
527 |
ClientFlags("-i".to_string(), vec![c.nick.clone()]) |
|
528 |
.send_all() |
|
529 |
.action(), |
|
530 |
); |
|
13416 | 531 |
} |
13419 | 532 |
server.react(client_id, actions); |
13416 | 533 |
actions = Vec::new(); |
534 |
||
13419 | 535 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13416 | 536 |
c.room_id = Some(lobby_id); |
13494 | 537 |
if r.players_number == 0 && !r.is_fixed() { |
13416 | 538 |
actions.push(RemoveRoom(r.id)); |
13447 | 539 |
} else { |
540 |
actions.push(RemoveClientTeams); |
|
14478 | 541 |
actions.push( |
542 |
RoomLeft(c.nick.clone(), msg) |
|
543 |
.send_all() |
|
544 |
.in_room(r.id) |
|
545 |
.but_self() |
|
546 |
.action(), |
|
547 |
); |
|
13447 | 548 |
actions.push(SendRoomUpdate(Some(r.name.clone()))); |
13416 | 549 |
} |
550 |
} |
|
13419 | 551 |
server.react(client_id, actions) |
13416 | 552 |
} |
553 |
ChangeMaster(room_id, new_id) => { |
|
554 |
let mut actions = Vec::new(); |
|
555 |
let room_client_ids = server.room_clients(room_id); |
|
14478 | 556 |
let new_id = if server |
557 |
.room(client_id) |
|
558 |
.map(|r| r.is_fixed()) |
|
559 |
.unwrap_or(false) |
|
560 |
{ |
|
13447 | 561 |
new_id |
562 |
} else { |
|
14478 | 563 |
new_id.or_else(|| room_client_ids.iter().find(|id| **id != client_id).cloned()) |
13447 | 564 |
}; |
13416 | 565 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
566 |
||
13419 | 567 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
568 |
match r.master_id { |
|
569 |
Some(id) if id == c.id => { |
|
13486 | 570 |
c.set_is_master(false); |
13419 | 571 |
r.master_id = None; |
14478 | 572 |
actions.push( |
573 |
ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
|
574 |
.send_all() |
|
575 |
.in_room(r.id) |
|
576 |
.action(), |
|
577 |
); |
|
13419 | 578 |
} |
579 |
Some(_) => unreachable!(), |
|
580 |
None => {} |
|
13416 | 581 |
} |
582 |
r.master_id = new_id; |
|
13775 | 583 |
if !r.is_fixed() && c.protocol_number < 42 { |
14478 | 584 |
r.name |
585 |
.replace_range(.., new_nick.as_ref().map_or("[]", String::as_str)); |
|
13775 | 586 |
} |
13494 | 587 |
r.set_join_restriction(false); |
588 |
r.set_team_add_restriction(false); |
|
589 |
let is_fixed = r.is_fixed(); |
|
590 |
r.set_unregistered_players_restriction(is_fixed); |
|
13416 | 591 |
if let Some(nick) = new_nick { |
14478 | 592 |
actions.push( |
593 |
ClientFlags("+h".to_string(), vec![nick]) |
|
594 |
.send_all() |
|
595 |
.in_room(r.id) |
|
596 |
.action(), |
|
597 |
); |
|
13416 | 598 |
} |
599 |
} |
|
13666 | 600 |
if let Some(id) = new_id { |
601 |
server.clients[id].set_is_master(true) |
|
602 |
} |
|
13419 | 603 |
server.react(client_id, actions); |
604 |
} |
|
605 |
RemoveTeam(name) => { |
|
13426 | 606 |
let mut actions = Vec::new(); |
607 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13419 | 608 |
r.remove_team(&name); |
13426 | 609 |
if let Some(ref mut info) = r.game_info { |
610 |
info.left_teams.push(name.clone()); |
|
611 |
} |
|
612 |
actions.push(TeamRemove(name.clone()).send_all().in_room(r.id).action()); |
|
613 |
actions.push(SendRoomUpdate(None)); |
|
13486 | 614 |
if r.game_info.is_some() && c.is_in_game() { |
13426 | 615 |
actions.push(SendTeamRemovalMessage(name)); |
616 |
} |
|
617 |
} |
|
13419 | 618 |
server.react(client_id, actions); |
14478 | 619 |
} |
13419 | 620 |
RemoveClientTeams => { |
13666 | 621 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
14478 | 622 |
let actions = r |
623 |
.client_teams(c.id) |
|
624 |
.map(|t| RemoveTeam(t.name.clone())) |
|
625 |
.collect(); |
|
13666 | 626 |
server.react(client_id, actions); |
627 |
} |
|
13416 | 628 |
} |
629 |
SendRoomUpdate(old_name) => { |
|
13666 | 630 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13419 | 631 |
let name = old_name.unwrap_or_else(|| r.name.clone()); |
13666 | 632 |
let actions = vec![RoomUpdated(name, r.info(Some(&c))) |
14478 | 633 |
.send_all() |
634 |
.with_protocol(r.protocol_number) |
|
635 |
.action()]; |
|
13666 | 636 |
server.react(client_id, actions); |
637 |
} |
|
14478 | 638 |
} |
13423 | 639 |
StartRoomGame(room_id) => { |
640 |
let actions = { |
|
14478 | 641 |
let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server |
642 |
.clients |
|
643 |
.iter() |
|
644 |
.map(|(id, c)| (id, c.nick.clone())) |
|
645 |
.unzip(); |
|
13423 | 646 |
let room = &mut server.rooms[room_id]; |
647 |
||
648 |
if !room.has_multiple_clans() { |
|
14478 | 649 |
vec![Warn( |
650 |
"The game can't be started with less than two clans!".to_string(), |
|
651 |
)] |
|
652 |
} else if room.protocol_number <= 43 |
|
653 |
&& room.players_number != room.ready_players_number |
|
654 |
{ |
|
13775 | 655 |
vec![Warn("Not all players are ready".to_string())] |
13423 | 656 |
} else if room.game_info.is_some() { |
657 |
vec![Warn("The game is already in progress".to_string())] |
|
658 |
} else { |
|
13427 | 659 |
room.start_round(); |
13423 | 660 |
for id in room_clients { |
661 |
let c = &mut server.clients[id]; |
|
13486 | 662 |
c.set_is_in_game(false); |
13423 | 663 |
c.team_indices = room.client_team_indices(c.id); |
664 |
} |
|
14478 | 665 |
vec![ |
666 |
RunGame.send_all().in_room(room.id).action(), |
|
667 |
SendRoomUpdate(None), |
|
668 |
ClientFlags("+g".to_string(), room_nicks) |
|
669 |
.send_all() |
|
670 |
.in_room(room.id) |
|
671 |
.action(), |
|
672 |
] |
|
13423 | 673 |
} |
674 |
}; |
|
675 |
server.react(client_id, actions); |
|
13416 | 676 |
} |
13423 | 677 |
SendTeamRemovalMessage(team_name) => { |
678 |
let mut actions = Vec::new(); |
|
13521 | 679 |
if let Some(r) = server.room(client_id) { |
13423 | 680 |
if let Some(ref mut info) = r.game_info { |
681 |
let msg = once(b'F').chain(team_name.bytes()); |
|
14478 | 682 |
actions.push( |
683 |
ForwardEngineMessage(vec![to_engine_msg(msg)]) |
|
684 |
.send_all() |
|
685 |
.in_room(r.id) |
|
686 |
.but_self() |
|
687 |
.action(), |
|
688 |
); |
|
13423 | 689 |
info.teams_in_game -= 1; |
690 |
if info.teams_in_game == 0 { |
|
691 |
actions.push(FinishRoomGame(r.id)); |
|
692 |
} |
|
13427 | 693 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
13443 | 694 |
if let Some(m) = &info.sync_msg { |
695 |
info.msg_log.push(m.clone()); |
|
13427 | 696 |
} |
13443 | 697 |
if info.sync_msg.is_some() { |
698 |
info.sync_msg = None |
|
699 |
} |
|
700 |
info.msg_log.push(remove_msg.clone()); |
|
14478 | 701 |
actions.push( |
702 |
ForwardEngineMessage(vec![remove_msg]) |
|
703 |
.send_all() |
|
704 |
.in_room(r.id) |
|
705 |
.but_self() |
|
706 |
.action(), |
|
707 |
); |
|
13423 | 708 |
} |
709 |
} |
|
710 |
server.react(client_id, actions); |
|
711 |
} |
|
712 |
FinishRoomGame(room_id) => { |
|
13426 | 713 |
let mut actions = Vec::new(); |
714 |
||
13775 | 715 |
let r = &mut server.rooms[room_id]; |
716 |
r.ready_players_number = 1; |
|
717 |
actions.push(SendRoomUpdate(None)); |
|
718 |
actions.push(RoundFinished.send_all().in_room(r.id).action()); |
|
719 |
||
720 |
if let Some(info) = replace(&mut r.game_info, None) { |
|
13426 | 721 |
for (_, c) in server.clients.iter() { |
13486 | 722 |
if c.room_id == Some(room_id) && c.is_joined_mid_game() { |
14478 | 723 |
actions.push(SendRoomData { |
724 |
to: c.id, |
|
725 |
teams: false, |
|
726 |
config: true, |
|
727 |
flags: false, |
|
728 |
}); |
|
13500 | 729 |
for name in &info.left_teams { |
14478 | 730 |
actions.push(TeamRemove(name.clone()).send(c.id).action()); |
13426 | 731 |
} |
732 |
} |
|
733 |
} |
|
734 |
} |
|
735 |
||
14478 | 736 |
let nicks: Vec<_> = server |
737 |
.clients |
|
738 |
.iter_mut() |
|
13426 | 739 |
.filter(|(_, c)| c.room_id == Some(room_id)) |
740 |
.map(|(_, c)| { |
|
13666 | 741 |
c.set_is_ready(c.is_master()); |
13486 | 742 |
c.set_is_joined_mid_game(false); |
13426 | 743 |
c |
14478 | 744 |
}) |
745 |
.filter_map(|c| { |
|
746 |
if !c.is_master() { |
|
747 |
Some(c.nick.clone()) |
|
748 |
} else { |
|
749 |
None |
|
750 |
} |
|
751 |
}) |
|
752 |
.collect(); |
|
13775 | 753 |
|
13426 | 754 |
if !nicks.is_empty() { |
13775 | 755 |
let msg = if r.protocol_number < 38 { |
756 |
LegacyReady(false, nicks) |
|
757 |
} else { |
|
758 |
ClientFlags("-r".to_string(), nicks) |
|
759 |
}; |
|
760 |
actions.push(msg.send_all().in_room(room_id).action()); |
|
13426 | 761 |
} |
13423 | 762 |
server.react(client_id, actions); |
763 |
} |
|
12147 | 764 |
Warn(msg) => { |
13419 | 765 |
run_action(server, client_id, Warning(msg).send_self().action()); |
12147 | 766 |
} |
14478 | 767 |
ProtocolError(msg) => run_action(server, client_id, Error(msg).send_self().action()), |
12144 | 768 |
} |
769 |
} |