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