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