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