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