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