author | Wuzzy <Wuzzy2@mail.ru> |
Sat, 08 Dec 2018 14:13:18 +0100 | |
changeset 14384 | b33127bc2424 |
parent 14379 | e5db279308d7 |
permissions | -rw-r--r-- |
13421 | 1 |
use std::{ |
13428 | 2 |
io, io::Write, |
13431 | 3 |
iter::once, |
13483 | 4 |
mem::replace |
13421 | 5 |
}; |
6 |
use super::{ |
|
14379 | 7 |
core::HWServer, |
13528 | 8 |
room::{GameInfo, RoomFlags}, |
13483 | 9 |
client::HWClient, |
13485 | 10 |
coretypes::{ClientId, RoomId, GameCfg, VoteType}, |
13421 | 11 |
room::HWRoom, |
12 |
handlers |
|
13 |
}; |
|
13671 | 14 |
use crate::{ |
15 |
protocol::messages::{ |
|
16 |
HWProtocolMessage, |
|
17 |
HWServerMessage, |
|
18 |
HWServerMessage::*, |
|
19 |
server_chat |
|
20 |
}, |
|
21 |
utils::to_engine_msg |
|
13421 | 22 |
}; |
13485 | 23 |
use rand::{thread_rng, Rng, distributions::Uniform}; |
12143 | 24 |
|
13424 | 25 |
pub enum Destination { |
13431 | 26 |
ToId(ClientId), |
13424 | 27 |
ToSelf, |
13428 | 28 |
ToAll { |
13424 | 29 |
room_id: Option<RoomId>, |
13525 | 30 |
protocol: Option<u16>, |
13424 | 31 |
skip_self: bool |
32 |
} |
|
33 |
} |
|
34 |
||
35 |
pub struct PendingMessage { |
|
36 |
pub destination: Destination, |
|
37 |
pub message: HWServerMessage |
|
38 |
} |
|
39 |
||
40 |
impl PendingMessage { |
|
13431 | 41 |
pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage { |
42 |
PendingMessage{ destination: Destination::ToId(client_id), message} |
|
43 |
} |
|
44 |
||
13424 | 45 |
pub fn send_self(message: HWServerMessage) -> PendingMessage { |
46 |
PendingMessage{ destination: Destination::ToSelf, message } |
|
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 |
}; |
|
55 |
PendingMessage{ destination, message } |
|
56 |
} |
|
57 |
||
58 |
pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage { |
|
59 |
if let Destination::ToAll {ref mut room_id, ..} = self.destination { |
|
60 |
*room_id = Some(clients_room_id) |
|
61 |
} |
|
62 |
self |
|
63 |
} |
|
64 |
||
13525 | 65 |
pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage { |
13424 | 66 |
if let Destination::ToAll {ref mut protocol, ..} = self.destination { |
67 |
*protocol = Some(protocol_number) |
|
68 |
} |
|
69 |
self |
|
70 |
} |
|
71 |
||
72 |
pub fn but_self(mut self) -> PendingMessage { |
|
73 |
if let Destination::ToAll {ref mut skip_self, ..} = self.destination { |
|
74 |
*skip_self = true |
|
75 |
} |
|
76 |
self |
|
77 |
} |
|
78 |
||
79 |
pub fn action(self) -> Action { Send(self) } |
|
80 |
} |
|
81 |
||
82 |
impl Into<Action> for PendingMessage { |
|
83 |
fn into(self) -> Action { self.action() } |
|
84 |
} |
|
85 |
||
86 |
impl HWServerMessage { |
|
13431 | 87 |
pub fn send(self, client_id: ClientId) -> PendingMessage { PendingMessage::send(self, client_id) } |
13424 | 88 |
pub fn send_self(self) -> PendingMessage { PendingMessage::send_self(self) } |
89 |
pub fn send_all(self) -> PendingMessage { PendingMessage::send_all(self) } |
|
90 |
} |
|
91 |
||
12143 | 92 |
pub enum Action { |
13424 | 93 |
Send(PendingMessage), |
12144 | 94 |
RemoveClient, |
95 |
ByeClient(String), |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
96 |
ReactProtocolMessage(HWProtocolMessage), |
12150 | 97 |
CheckRegistered, |
98 |
JoinLobby, |
|
12152 | 99 |
AddRoom(String, Option<String>), |
13421 | 100 |
RemoveRoom(RoomId), |
101 |
MoveToRoom(RoomId), |
|
102 |
MoveToLobby(String), |
|
103 |
ChangeMaster(RoomId, Option<ClientId>), |
|
13424 | 104 |
RemoveTeam(String), |
105 |
RemoveClientTeams, |
|
13421 | 106 |
SendRoomUpdate(Option<String>), |
13428 | 107 |
StartRoomGame(RoomId), |
108 |
SendTeamRemovalMessage(String), |
|
109 |
FinishRoomGame(RoomId), |
|
13431 | 110 |
SendRoomData{to: ClientId, teams: bool, config: bool, flags: bool}, |
13483 | 111 |
AddVote{vote: bool, is_forced: bool}, |
112 |
ApplyVoting(VoteType, RoomId), |
|
12152 | 113 |
Warn(String), |
13421 | 114 |
ProtocolError(String) |
12143 | 115 |
} |
12149 | 116 |
|
117 |
use self::Action::*; |
|
118 |
||
13424 | 119 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12149 | 120 |
match action { |
13529 | 121 |
Send(msg) => server.send(client_id, &msg.destination, msg.message), |
12149 | 122 |
ByeClient(msg) => { |
13671 | 123 |
let c = &server.clients[client_id]; |
124 |
let nick = c.nick.clone(); |
|
13421 | 125 |
|
13671 | 126 |
if let Some(id) = c.room_id{ |
13424 | 127 |
if id != server.lobby_id { |
128 |
server.react(client_id, vec![ |
|
129 |
MoveToLobby(format!("quit: {}", msg.clone()))]); |
|
13421 | 130 |
} |
13529 | 131 |
} |
13421 | 132 |
|
13424 | 133 |
server.react(client_id, vec![ |
134 |
LobbyLeft(nick, msg.clone()).send_all().action(), |
|
135 |
Bye(msg).send_self().action(), |
|
13421 | 136 |
RemoveClient]); |
12149 | 137 |
}, |
138 |
RemoveClient => { |
|
13424 | 139 |
server.removed_clients.push(client_id); |
140 |
if server.clients.contains(client_id) { |
|
141 |
server.clients.remove(client_id); |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
142 |
} |
12149 | 143 |
}, |
144 |
ReactProtocolMessage(msg) => |
|
13424 | 145 |
handlers::handle(server, client_id, msg), |
13806 | 146 |
CheckRegistered => { |
147 |
let client = &server.clients[client_id]; |
|
148 |
if client.protocol_number > 0 && client.nick != "" { |
|
149 |
let has_nick_clash = server.clients.iter().any( |
|
150 |
|(id, c)| id != client_id && c.nick == client.nick); |
|
151 |
||
152 |
let actions = if !client.is_checker() && has_nick_clash { |
|
153 |
if client.protocol_number < 38 { |
|
154 |
vec![ByeClient("Nickname is already in use".to_string())] |
|
155 |
} else { |
|
156 |
server.clients[client_id].nick.clear(); |
|
157 |
vec![Notice("NickAlreadyInUse".to_string()).send_self().action()] |
|
158 |
} |
|
159 |
} else { |
|
160 |
vec![JoinLobby] |
|
161 |
}; |
|
162 |
server.react(client_id, actions); |
|
163 |
} |
|
164 |
}, |
|
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
165 |
JoinLobby => { |
13424 | 166 |
server.clients[client_id].room_id = Some(server.lobby_id); |
12152 | 167 |
|
13671 | 168 |
let mut lobby_nicks = Vec::new(); |
169 |
for (_, c) in server.clients.iter() { |
|
170 |
if c.room_id.is_some() { |
|
171 |
lobby_nicks.push(c.nick.clone()); |
|
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
172 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
173 |
} |
13671 | 174 |
let joined_msg = LobbyJoined(lobby_nicks); |
175 |
||
13424 | 176 |
let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]); |
13421 | 177 |
let flags_msg = ClientFlags( |
178 |
"+i".to_string(), |
|
179 |
server.clients.iter() |
|
180 |
.filter(|(_, c)| c.room_id.is_some()) |
|
181 |
.map(|(_, c)| c.nick.clone()) |
|
182 |
.collect()); |
|
183 |
let server_msg = ServerMessage("\u{1f994} is watching".to_string()); |
|
184 |
let rooms_msg = Rooms(server.rooms.iter() |
|
185 |
.filter(|(id, _)| *id != server.lobby_id) |
|
186 |
.flat_map(|(_, r)| |
|
187 |
r.info(r.master_id.map(|id| &server.clients[id]))) |
|
188 |
.collect()); |
|
13424 | 189 |
server.react(client_id, vec![ |
190 |
everyone_msg.send_all().but_self().action(), |
|
191 |
joined_msg.send_self().action(), |
|
192 |
flags_msg.send_self().action(), |
|
193 |
server_msg.send_self().action(), |
|
194 |
rooms_msg.send_self().action(), |
|
12150 | 195 |
]); |
196 |
}, |
|
12152 | 197 |
AddRoom(name, password) => { |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
198 |
let room_id = server.add_room();; |
13421 | 199 |
|
13671 | 200 |
let r = &mut server.rooms[room_id]; |
201 |
let c = &mut server.clients[client_id]; |
|
202 |
r.master_id = Some(c.id); |
|
203 |
r.name = name; |
|
204 |
r.password = password; |
|
205 |
r.protocol_number = c.protocol_number; |
|
206 |
||
207 |
let actions = vec![ |
|
208 |
RoomAdd(r.info(Some(&c))).send_all() |
|
209 |
.with_protocol(r.protocol_number).action(), |
|
210 |
MoveToRoom(room_id)]; |
|
211 |
||
13424 | 212 |
server.react(client_id, actions); |
13421 | 213 |
}, |
214 |
RemoveRoom(room_id) => { |
|
13671 | 215 |
let r = &mut server.rooms[room_id]; |
216 |
let actions = vec![RoomRemove(r.name.clone()).send_all() |
|
217 |
.with_protocol(r.protocol_number).action()]; |
|
13421 | 218 |
server.rooms.remove(room_id); |
13424 | 219 |
server.react(client_id, actions); |
13421 | 220 |
} |
221 |
MoveToRoom(room_id) => { |
|
13671 | 222 |
let r = &mut server.rooms[room_id]; |
223 |
let c = &mut server.clients[client_id]; |
|
224 |
r.players_number += 1; |
|
225 |
c.room_id = Some(room_id); |
|
226 |
||
227 |
let is_master = r.master_id == Some(c.id); |
|
228 |
c.set_is_master(is_master); |
|
229 |
c.set_is_ready(is_master); |
|
230 |
c.set_is_joined_mid_game(false); |
|
231 |
||
232 |
if is_master { |
|
233 |
r.ready_players_number += 1; |
|
234 |
} |
|
235 |
||
236 |
let mut v = vec![ |
|
237 |
RoomJoined(vec![c.nick.clone()]).send_all().in_room(room_id).action(), |
|
238 |
ClientFlags("+i".to_string(), vec![c.nick.clone()]).send_all().action(), |
|
239 |
SendRoomUpdate(None)]; |
|
240 |
||
241 |
if !r.greeting.is_empty() { |
|
242 |
v.push(ChatMsg {nick: "[greeting]".to_string(), msg: r.greeting.clone()} |
|
243 |
.send_self().action()); |
|
244 |
} |
|
13525 | 245 |
|
13671 | 246 |
if !c.is_master() { |
247 |
let team_names: Vec<_>; |
|
248 |
if let Some(ref mut info) = r.game_info { |
|
249 |
c.set_is_in_game(true); |
|
250 |
c.set_is_joined_mid_game(true); |
|
13525 | 251 |
|
13671 | 252 |
{ |
253 |
let teams = info.client_teams(c.id); |
|
254 |
c.teams_in_game = teams.clone().count() as u8; |
|
255 |
c.clan = teams.clone().next().map(|t| t.color); |
|
256 |
team_names = teams.map(|t| t.name.clone()).collect(); |
|
257 |
} |
|
258 |
||
259 |
if !team_names.is_empty() { |
|
260 |
info.left_teams.retain(|name| |
|
261 |
!team_names.contains(&name)); |
|
262 |
info.teams_in_game += team_names.len() as u8; |
|
263 |
r.teams = info.teams_at_start.iter() |
|
264 |
.filter(|(_, t)| !team_names.contains(&t.name)) |
|
265 |
.cloned().collect(); |
|
266 |
} |
|
267 |
} else { |
|
268 |
team_names = Vec::new(); |
|
13421 | 269 |
} |
13424 | 270 |
|
13671 | 271 |
v.push(SendRoomData{ to: client_id, teams: true, config: true, flags: true}); |
13432 | 272 |
|
13671 | 273 |
if let Some(ref info) = r.game_info { |
274 |
v.push(RunGame.send_self().action()); |
|
275 |
v.push(ClientFlags("+g".to_string(), vec![c.nick.clone()]) |
|
276 |
.send_all().in_room(r.id).action()); |
|
277 |
v.push(ForwardEngineMessage( |
|
278 |
vec![to_engine_msg("e$spectate 1".bytes())]) |
|
279 |
.send_self().action()); |
|
280 |
v.push(ForwardEngineMessage(info.msg_log.clone()) |
|
281 |
.send_self().action()); |
|
13432 | 282 |
|
13671 | 283 |
for name in &team_names { |
13432 | 284 |
v.push(ForwardEngineMessage( |
13671 | 285 |
vec![to_engine_msg(once(b'G').chain(name.bytes()))]) |
286 |
.send_all().in_room(r.id).action()); |
|
287 |
} |
|
288 |
if info.is_paused { |
|
289 |
v.push(ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
|
290 |
.send_all().in_room(r.id).action()) |
|
13432 | 291 |
} |
13427 | 292 |
} |
13671 | 293 |
} |
294 |
server.react(client_id, v); |
|
13429 | 295 |
} |
13431 | 296 |
SendRoomData {to, teams, config, flags} => { |
13429 | 297 |
let mut actions = Vec::new(); |
298 |
let room_id = server.clients[client_id].room_id; |
|
299 |
if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) { |
|
300 |
if config { |
|
301 |
actions.push(ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config()) |
|
13431 | 302 |
.send(to).action()); |
13529 | 303 |
for cfg in r.game_config() { |
13444 | 304 |
actions.push(cfg.to_server_msg().send(to).action()); |
13429 | 305 |
} |
306 |
} |
|
307 |
if teams { |
|
13432 | 308 |
let current_teams = match r.game_info { |
309 |
Some(ref info) => &info.teams_at_start, |
|
310 |
None => &r.teams |
|
311 |
}; |
|
312 |
for (owner_id, team) in current_teams.iter() { |
|
13429 | 313 |
actions.push(TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team)) |
13431 | 314 |
.send(to).action()); |
13432 | 315 |
actions.push(TeamColor(team.name.clone(), team.color) |
316 |
.send(to).action()); |
|
317 |
actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
318 |
.send(to).action()); |
|
13429 | 319 |
} |
320 |
} |
|
321 |
if flags { |
|
322 |
if let Some(id) = r.master_id { |
|
323 |
actions.push(ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()]) |
|
13431 | 324 |
.send(to).action()); |
13429 | 325 |
} |
326 |
let nicks: Vec<_> = server.clients.iter() |
|
13525 | 327 |
.filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready()) |
13429 | 328 |
.map(|(_, c)| c.nick.clone()).collect(); |
329 |
if !nicks.is_empty() { |
|
330 |
actions.push(ClientFlags("+r".to_string(), nicks) |
|
13431 | 331 |
.send(to).action()); |
13429 | 332 |
} |
333 |
} |
|
334 |
} |
|
335 |
server.react(client_id, actions); |
|
336 |
} |
|
13483 | 337 |
AddVote{vote, is_forced} => { |
338 |
let mut actions = Vec::new(); |
|
13532 | 339 |
if let Some(r) = server.room(client_id) { |
13483 | 340 |
let mut result = None; |
341 |
if let Some(ref mut voting) = r.voting { |
|
14355 | 342 |
if is_forced || voting.votes.iter().all(|(id, _)| client_id != *id) { |
13532 | 343 |
actions.push(server_chat("Your vote has been counted.".to_string()) |
344 |
.send_self().action()); |
|
13483 | 345 |
voting.votes.push((client_id, vote)); |
346 |
let i = voting.votes.iter(); |
|
347 |
let pro = i.clone().filter(|(_, v)| *v).count(); |
|
348 |
let contra = i.filter(|(_, v)| !*v).count(); |
|
349 |
let success_quota = voting.voters.len() / 2 + 1; |
|
350 |
if is_forced && vote || pro >= success_quota { |
|
351 |
result = Some(true); |
|
352 |
} else if is_forced && !vote || contra > voting.voters.len() - success_quota { |
|
353 |
result = Some(false); |
|
354 |
} |
|
355 |
} else { |
|
13532 | 356 |
actions.push(server_chat("You already have voted.".to_string()) |
357 |
.send_self().action()); |
|
13483 | 358 |
} |
359 |
} else { |
|
13532 | 360 |
actions.push(server_chat("There's no voting going on.".to_string()) |
361 |
.send_self().action()); |
|
13483 | 362 |
} |
363 |
||
364 |
if let Some(res) = result { |
|
13532 | 365 |
actions.push(server_chat("Voting closed.".to_string()) |
13483 | 366 |
.send_all().in_room(r.id).action()); |
367 |
let voting = replace(&mut r.voting, None).unwrap(); |
|
368 |
if res { |
|
369 |
actions.push(ApplyVoting(voting.kind, r.id)); |
|
370 |
} |
|
371 |
} |
|
372 |
} |
|
373 |
||
374 |
server.react(client_id, actions); |
|
375 |
} |
|
376 |
ApplyVoting(kind, room_id) => { |
|
377 |
let mut actions = Vec::new(); |
|
378 |
let mut id = client_id; |
|
379 |
match kind { |
|
380 |
VoteType::Kick(nick) => { |
|
381 |
if let Some(c) = server.find_client(&nick) { |
|
382 |
if c.room_id == Some(room_id) { |
|
383 |
id = c.id; |
|
384 |
actions.push(Kicked.send_self().action()); |
|
385 |
actions.push(MoveToLobby("kicked".to_string())); |
|
386 |
} |
|
387 |
} |
|
388 |
}, |
|
13532 | 389 |
VoteType::Map(None) => (), |
390 |
VoteType::Map(Some(name)) => { |
|
391 |
if let Some(location) = server.rooms[room_id].load_config(&name) { |
|
392 |
actions.push(server_chat(location.to_string()) |
|
393 |
.send_all().in_room(room_id).action()); |
|
394 |
actions.push(SendRoomUpdate(None)); |
|
395 |
for (_, c) in server.clients.iter() { |
|
396 |
if c.room_id == Some(room_id) { |
|
397 |
actions.push(SendRoomData{ |
|
398 |
to: c.id, teams: false, |
|
399 |
config: true, flags: false}) |
|
400 |
} |
|
401 |
} |
|
402 |
} |
|
13483 | 403 |
}, |
404 |
VoteType::Pause => { |
|
13485 | 405 |
if let Some(ref mut info) = server.rooms[room_id].game_info { |
13483 | 406 |
info.is_paused = !info.is_paused; |
13532 | 407 |
actions.push(server_chat("Pause toggled.".to_string()) |
13483 | 408 |
.send_all().in_room(room_id).action()); |
409 |
actions.push(ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
|
410 |
.send_all().in_room(room_id).action()); |
|
411 |
} |
|
412 |
}, |
|
413 |
VoteType::NewSeed => { |
|
13485 | 414 |
let seed = thread_rng().gen_range(0, 1_000_000_000).to_string(); |
415 |
let cfg = GameCfg::Seed(seed); |
|
416 |
actions.push(cfg.to_server_msg().send_all().in_room(room_id).action()); |
|
417 |
server.rooms[room_id].set_config(cfg); |
|
13483 | 418 |
}, |
419 |
VoteType::HedgehogsPerTeam(number) => { |
|
420 |
let r = &mut server.rooms[room_id]; |
|
421 |
let nicks = r.set_hedgehogs_number(number); |
|
422 |
actions.extend(nicks.into_iter().map(|n| |
|
423 |
HedgehogsNumber(n, number).send_all().in_room(room_id).action() |
|
424 |
)); |
|
425 |
}, |
|
426 |
} |
|
427 |
server.react(id, actions); |
|
428 |
} |
|
13421 | 429 |
MoveToLobby(msg) => { |
430 |
let mut actions = Vec::new(); |
|
431 |
let lobby_id = server.lobby_id; |
|
13424 | 432 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13421 | 433 |
r.players_number -= 1; |
13525 | 434 |
if c.is_ready() && r.ready_players_number > 0 { |
13421 | 435 |
r.ready_players_number -= 1; |
436 |
} |
|
13528 | 437 |
if c.is_master() && (r.players_number > 0 || r.is_fixed()) { |
13421 | 438 |
actions.push(ChangeMaster(r.id, None)); |
439 |
} |
|
13424 | 440 |
actions.push(ClientFlags("-i".to_string(), vec![c.nick.clone()]) |
441 |
.send_all().action()); |
|
13421 | 442 |
} |
13424 | 443 |
server.react(client_id, actions); |
13421 | 444 |
actions = Vec::new(); |
445 |
||
13424 | 446 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13421 | 447 |
c.room_id = Some(lobby_id); |
13528 | 448 |
if r.players_number == 0 && !r.is_fixed() { |
13421 | 449 |
actions.push(RemoveRoom(r.id)); |
13482 | 450 |
} else { |
451 |
actions.push(RemoveClientTeams); |
|
452 |
actions.push(RoomLeft(c.nick.clone(), msg) |
|
453 |
.send_all().in_room(r.id).but_self().action()); |
|
454 |
actions.push(SendRoomUpdate(Some(r.name.clone()))); |
|
13421 | 455 |
} |
456 |
} |
|
13424 | 457 |
server.react(client_id, actions) |
13421 | 458 |
} |
459 |
ChangeMaster(room_id, new_id) => { |
|
460 |
let mut actions = Vec::new(); |
|
461 |
let room_client_ids = server.room_clients(room_id); |
|
13528 | 462 |
let new_id = if server.room(client_id).map(|r| r.is_fixed()).unwrap_or(false) { |
13482 | 463 |
new_id |
464 |
} else { |
|
465 |
new_id.or_else(|| |
|
13529 | 466 |
room_client_ids.iter().find(|id| **id != client_id).cloned()) |
13482 | 467 |
}; |
13421 | 468 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
469 |
||
13424 | 470 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
471 |
match r.master_id { |
|
472 |
Some(id) if id == c.id => { |
|
13525 | 473 |
c.set_is_master(false); |
13424 | 474 |
r.master_id = None; |
475 |
actions.push(ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
|
476 |
.send_all().in_room(r.id).action()); |
|
477 |
} |
|
478 |
Some(_) => unreachable!(), |
|
479 |
None => {} |
|
13421 | 480 |
} |
481 |
r.master_id = new_id; |
|
13806 | 482 |
if !r.is_fixed() && c.protocol_number < 42 { |
483 |
r.name.replace_range(.., new_nick.as_ref().map_or("[]", String::as_str)); |
|
484 |
} |
|
13528 | 485 |
r.set_join_restriction(false); |
486 |
r.set_team_add_restriction(false); |
|
487 |
let is_fixed = r.is_fixed(); |
|
488 |
r.set_unregistered_players_restriction(is_fixed); |
|
13421 | 489 |
if let Some(nick) = new_nick { |
13424 | 490 |
actions.push(ClientFlags("+h".to_string(), vec![nick]) |
491 |
.send_all().in_room(r.id).action()); |
|
13421 | 492 |
} |
493 |
} |
|
13671 | 494 |
if let Some(id) = new_id { |
495 |
server.clients[id].set_is_master(true) |
|
496 |
} |
|
13424 | 497 |
server.react(client_id, actions); |
498 |
} |
|
499 |
RemoveTeam(name) => { |
|
13431 | 500 |
let mut actions = Vec::new(); |
501 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13424 | 502 |
r.remove_team(&name); |
13431 | 503 |
if let Some(ref mut info) = r.game_info { |
504 |
info.left_teams.push(name.clone()); |
|
505 |
} |
|
506 |
actions.push(TeamRemove(name.clone()).send_all().in_room(r.id).action()); |
|
507 |
actions.push(SendRoomUpdate(None)); |
|
13525 | 508 |
if r.game_info.is_some() && c.is_in_game() { |
13431 | 509 |
actions.push(SendTeamRemovalMessage(name)); |
510 |
} |
|
511 |
} |
|
13424 | 512 |
server.react(client_id, actions); |
513 |
}, |
|
514 |
RemoveClientTeams => { |
|
13671 | 515 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
516 |
let actions = r.client_teams(c.id).map(|t| RemoveTeam(t.name.clone())).collect(); |
|
517 |
server.react(client_id, actions); |
|
518 |
} |
|
13421 | 519 |
} |
520 |
SendRoomUpdate(old_name) => { |
|
13671 | 521 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13424 | 522 |
let name = old_name.unwrap_or_else(|| r.name.clone()); |
13671 | 523 |
let actions = vec![RoomUpdated(name, r.info(Some(&c))) |
524 |
.send_all().with_protocol(r.protocol_number).action()]; |
|
525 |
server.react(client_id, actions); |
|
526 |
} |
|
13428 | 527 |
}, |
528 |
StartRoomGame(room_id) => { |
|
529 |
let actions = { |
|
530 |
let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server.clients.iter() |
|
531 |
.map(|(id, c)| (id, c.nick.clone())).unzip(); |
|
532 |
let room = &mut server.rooms[room_id]; |
|
533 |
||
534 |
if !room.has_multiple_clans() { |
|
535 |
vec![Warn("The game can't be started with less than two clans!".to_string())] |
|
13806 | 536 |
} else if room.protocol_number <= 43 && room.players_number != room.ready_players_number { |
537 |
vec![Warn("Not all players are ready".to_string())] |
|
13428 | 538 |
} else if room.game_info.is_some() { |
539 |
vec![Warn("The game is already in progress".to_string())] |
|
540 |
} else { |
|
13432 | 541 |
room.start_round(); |
13428 | 542 |
for id in room_clients { |
543 |
let c = &mut server.clients[id]; |
|
13525 | 544 |
c.set_is_in_game(false); |
13428 | 545 |
c.team_indices = room.client_team_indices(c.id); |
546 |
} |
|
547 |
vec![RunGame.send_all().in_room(room.id).action(), |
|
548 |
SendRoomUpdate(None), |
|
549 |
ClientFlags("+g".to_string(), room_nicks) |
|
550 |
.send_all().in_room(room.id).action()] |
|
551 |
} |
|
552 |
}; |
|
553 |
server.react(client_id, actions); |
|
13421 | 554 |
} |
13428 | 555 |
SendTeamRemovalMessage(team_name) => { |
556 |
let mut actions = Vec::new(); |
|
13532 | 557 |
if let Some(r) = server.room(client_id) { |
13428 | 558 |
if let Some(ref mut info) = r.game_info { |
559 |
let msg = once(b'F').chain(team_name.bytes()); |
|
13433 | 560 |
actions.push(ForwardEngineMessage(vec![to_engine_msg(msg)]). |
13428 | 561 |
send_all().in_room(r.id).but_self().action()); |
562 |
info.teams_in_game -= 1; |
|
563 |
if info.teams_in_game == 0 { |
|
564 |
actions.push(FinishRoomGame(r.id)); |
|
565 |
} |
|
13432 | 566 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
13448 | 567 |
if let Some(m) = &info.sync_msg { |
568 |
info.msg_log.push(m.clone()); |
|
13432 | 569 |
} |
13448 | 570 |
if info.sync_msg.is_some() { |
571 |
info.sync_msg = None |
|
572 |
} |
|
573 |
info.msg_log.push(remove_msg.clone()); |
|
13433 | 574 |
actions.push(ForwardEngineMessage(vec![remove_msg]) |
13432 | 575 |
.send_all().in_room(r.id).but_self().action()); |
13428 | 576 |
} |
577 |
} |
|
578 |
server.react(client_id, actions); |
|
579 |
} |
|
580 |
FinishRoomGame(room_id) => { |
|
13431 | 581 |
let mut actions = Vec::new(); |
582 |
||
13806 | 583 |
let r = &mut server.rooms[room_id]; |
584 |
r.ready_players_number = 1; |
|
585 |
actions.push(SendRoomUpdate(None)); |
|
586 |
actions.push(RoundFinished.send_all().in_room(r.id).action()); |
|
587 |
||
588 |
if let Some(info) = replace(&mut r.game_info, None) { |
|
13431 | 589 |
for (_, c) in server.clients.iter() { |
13525 | 590 |
if c.room_id == Some(room_id) && c.is_joined_mid_game() { |
13431 | 591 |
actions.push(SendRoomData{ |
592 |
to: c.id, teams: false, |
|
593 |
config: true, flags: false}); |
|
13529 | 594 |
for name in &info.left_teams { |
13431 | 595 |
actions.push(TeamRemove(name.clone()) |
596 |
.send(c.id).action()); |
|
597 |
} |
|
598 |
} |
|
599 |
} |
|
600 |
} |
|
601 |
||
602 |
let nicks: Vec<_> = server.clients.iter_mut() |
|
603 |
.filter(|(_, c)| c.room_id == Some(room_id)) |
|
604 |
.map(|(_, c)| { |
|
13671 | 605 |
c.set_is_ready(c.is_master()); |
13525 | 606 |
c.set_is_joined_mid_game(false); |
13431 | 607 |
c |
13525 | 608 |
}).filter_map(|c| if !c.is_master() { |
13431 | 609 |
Some(c.nick.clone()) |
610 |
} else { |
|
611 |
None |
|
612 |
}).collect(); |
|
13806 | 613 |
|
13431 | 614 |
if !nicks.is_empty() { |
13806 | 615 |
let msg = if r.protocol_number < 38 { |
616 |
LegacyReady(false, nicks) |
|
617 |
} else { |
|
618 |
ClientFlags("-r".to_string(), nicks) |
|
619 |
}; |
|
620 |
actions.push(msg.send_all().in_room(room_id).action()); |
|
13431 | 621 |
} |
13428 | 622 |
server.react(client_id, actions); |
623 |
} |
|
12152 | 624 |
Warn(msg) => { |
13424 | 625 |
run_action(server, client_id, Warning(msg).send_self().action()); |
12152 | 626 |
} |
13421 | 627 |
ProtocolError(msg) => { |
13424 | 628 |
run_action(server, client_id, Error(msg).send_self().action()) |
13421 | 629 |
} |
12149 | 630 |
} |
631 |
} |