author | alfadur <mail@none> |
Tue, 05 Feb 2019 17:46:43 +0300 | |
changeset 14704 | 932ff7683653 |
parent 14696 | dfe652c53470 |
child 14707 | 9f98086de1b6 |
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 |
} |
|
91 |
||
92 |
impl HWServerMessage { |
|
14478 | 93 |
pub fn send(self, client_id: ClientId) -> PendingMessage { |
94 |
PendingMessage::send(self, client_id) |
|
95 |
} |
|
96 |
pub fn send_self(self) -> PendingMessage { |
|
97 |
PendingMessage::send_self(self) |
|
98 |
} |
|
99 |
pub fn send_all(self) -> PendingMessage { |
|
100 |
PendingMessage::send_all(self) |
|
101 |
} |
|
13419 | 102 |
} |
103 |
||
12138 | 104 |
pub enum Action { |
13416 | 105 |
ChangeMaster(RoomId, Option<ClientId>), |
106 |
SendRoomUpdate(Option<String>), |
|
13423 | 107 |
StartRoomGame(RoomId), |
108 |
SendTeamRemovalMessage(String), |
|
109 |
FinishRoomGame(RoomId), |
|
14478 | 110 |
SendRoomData { |
111 |
to: ClientId, |
|
112 |
teams: bool, |
|
113 |
config: bool, |
|
114 |
flags: bool, |
|
115 |
}, |
|
116 |
AddVote { |
|
117 |
vote: bool, |
|
118 |
is_forced: bool, |
|
119 |
}, |
|
13450 | 120 |
ApplyVoting(VoteType, RoomId), |
12138 | 121 |
} |
12144 | 122 |
|
123 |
use self::Action::*; |
|
124 |
||
13419 | 125 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12144 | 126 |
match action { |
14478 | 127 |
SendRoomData { |
128 |
to, |
|
129 |
teams, |
|
130 |
config, |
|
131 |
flags, |
|
132 |
} => { |
|
13424 | 133 |
let mut actions = Vec::new(); |
134 |
let room_id = server.clients[client_id].room_id; |
|
135 |
if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) { |
|
136 |
if config { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
137 |
/* actions.push( |
14478 | 138 |
ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config()) |
139 |
.send(to) |
|
140 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
141 |
)*/ |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
142 |
; |
13500 | 143 |
for cfg in r.game_config() { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
144 |
//actions.push(cfg.to_server_msg().send(to).action()); |
13424 | 145 |
} |
146 |
} |
|
147 |
if teams { |
|
13427 | 148 |
let current_teams = match r.game_info { |
149 |
Some(ref info) => &info.teams_at_start, |
|
14478 | 150 |
None => &r.teams, |
13427 | 151 |
}; |
152 |
for (owner_id, team) in current_teams.iter() { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
153 |
/*actions.push( |
14478 | 154 |
TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team)) |
155 |
.send(to) |
|
156 |
.action(), |
|
157 |
); |
|
158 |
actions.push(TeamColor(team.name.clone(), team.color).send(to).action()); |
|
159 |
actions.push( |
|
160 |
HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
161 |
.send(to) |
|
162 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
163 |
);*/ |
13424 | 164 |
} |
165 |
} |
|
166 |
if flags { |
|
167 |
if let Some(id) = r.master_id { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
168 |
/* |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
169 |
actions.push( |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
170 |
ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()]) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
171 |
.send(to) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
172 |
.action(), |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
173 |
); |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
174 |
*/ |
13424 | 175 |
} |
14478 | 176 |
let nicks: Vec<_> = server |
177 |
.clients |
|
178 |
.iter() |
|
13486 | 179 |
.filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready()) |
14478 | 180 |
.map(|(_, c)| c.nick.clone()) |
181 |
.collect(); |
|
13424 | 182 |
if !nicks.is_empty() { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
183 |
/*actions.push(ClientFlags("+r".to_string(), nicks).send(to).action())*/ |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
184 |
; |
13424 | 185 |
} |
186 |
} |
|
187 |
} |
|
188 |
server.react(client_id, actions); |
|
189 |
} |
|
14478 | 190 |
AddVote { vote, is_forced } => { |
13450 | 191 |
let mut actions = Vec::new(); |
13521 | 192 |
if let Some(r) = server.room(client_id) { |
13450 | 193 |
let mut result = None; |
194 |
if let Some(ref mut voting) = r.voting { |
|
14371 | 195 |
if is_forced || voting.votes.iter().all(|(id, _)| client_id != *id) { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
196 |
/* actions.push( |
14478 | 197 |
server_chat("Your vote has been counted.".to_string()) |
198 |
.send_self() |
|
199 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
200 |
)*/ |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
201 |
; |
13450 | 202 |
voting.votes.push((client_id, vote)); |
203 |
let i = voting.votes.iter(); |
|
204 |
let pro = i.clone().filter(|(_, v)| *v).count(); |
|
205 |
let contra = i.filter(|(_, v)| !*v).count(); |
|
206 |
let success_quota = voting.voters.len() / 2 + 1; |
|
207 |
if is_forced && vote || pro >= success_quota { |
|
208 |
result = Some(true); |
|
14478 | 209 |
} else if is_forced && !vote || contra > voting.voters.len() - success_quota |
210 |
{ |
|
13450 | 211 |
result = Some(false); |
212 |
} |
|
213 |
} else { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
214 |
/* actions.push( |
14478 | 215 |
server_chat("You already have voted.".to_string()) |
216 |
.send_self() |
|
217 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
218 |
)*/ |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
219 |
; |
13450 | 220 |
} |
221 |
} else { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
222 |
/* actions.push( |
14478 | 223 |
server_chat("There's no voting going on.".to_string()) |
224 |
.send_self() |
|
225 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
226 |
)*/ |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
227 |
; |
13450 | 228 |
} |
229 |
||
230 |
if let Some(res) = result { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
231 |
/*actions.push( |
14478 | 232 |
server_chat("Voting closed.".to_string()) |
233 |
.send_all() |
|
234 |
.in_room(r.id) |
|
235 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
236 |
);*/ |
13450 | 237 |
let voting = replace(&mut r.voting, None).unwrap(); |
238 |
if res { |
|
239 |
actions.push(ApplyVoting(voting.kind, r.id)); |
|
240 |
} |
|
241 |
} |
|
242 |
} |
|
243 |
||
244 |
server.react(client_id, actions); |
|
245 |
} |
|
246 |
ApplyVoting(kind, room_id) => { |
|
247 |
let mut actions = Vec::new(); |
|
248 |
let mut id = client_id; |
|
249 |
match kind { |
|
250 |
VoteType::Kick(nick) => { |
|
251 |
if let Some(c) = server.find_client(&nick) { |
|
252 |
if c.room_id == Some(room_id) { |
|
253 |
id = c.id; |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
254 |
//actions.push(Kicked.send_self().action()); |
14696
dfe652c53470
Server action refactoring part 6 of N
alfadur <mail@none>
parents:
14694
diff
changeset
|
255 |
//actions.push(MoveToLobby("kicked".to_string())); |
13450 | 256 |
} |
257 |
} |
|
14478 | 258 |
} |
13521 | 259 |
VoteType::Map(None) => (), |
260 |
VoteType::Map(Some(name)) => { |
|
261 |
if let Some(location) = server.rooms[room_id].load_config(&name) { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
262 |
/*actions.push( |
14478 | 263 |
server_chat(location.to_string()) |
264 |
.send_all() |
|
265 |
.in_room(room_id) |
|
266 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
267 |
);*/ |
13521 | 268 |
actions.push(SendRoomUpdate(None)); |
269 |
for (_, c) in server.clients.iter() { |
|
270 |
if c.room_id == Some(room_id) { |
|
14478 | 271 |
actions.push(SendRoomData { |
272 |
to: c.id, |
|
273 |
teams: false, |
|
274 |
config: true, |
|
275 |
flags: false, |
|
276 |
}) |
|
13521 | 277 |
} |
278 |
} |
|
279 |
} |
|
14478 | 280 |
} |
13450 | 281 |
VoteType::Pause => { |
13460 | 282 |
if let Some(ref mut info) = server.rooms[room_id].game_info { |
13450 | 283 |
info.is_paused = !info.is_paused; |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
284 |
/*actions.push( |
14478 | 285 |
server_chat("Pause toggled.".to_string()) |
286 |
.send_all() |
|
287 |
.in_room(room_id) |
|
288 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
289 |
);*/ |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
290 |
/*actions.push( |
14478 | 291 |
ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
292 |
.send_all() |
|
293 |
.in_room(room_id) |
|
294 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
295 |
);*/ |
13450 | 296 |
} |
14478 | 297 |
} |
13450 | 298 |
VoteType::NewSeed => { |
13460 | 299 |
let seed = thread_rng().gen_range(0, 1_000_000_000).to_string(); |
300 |
let cfg = GameCfg::Seed(seed); |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
301 |
/*actions.push(cfg.to_server_msg().send_all().in_room(room_id).action());*/ |
13460 | 302 |
server.rooms[room_id].set_config(cfg); |
14478 | 303 |
} |
13450 | 304 |
VoteType::HedgehogsPerTeam(number) => { |
305 |
let r = &mut server.rooms[room_id]; |
|
306 |
let nicks = r.set_hedgehogs_number(number); |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
307 |
/*actions.extend(nicks.into_iter().map(|n| { |
14478 | 308 |
HedgehogsNumber(n, number) |
309 |
.send_all() |
|
310 |
.in_room(room_id) |
|
311 |
.action() |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
312 |
}));*/ |
14478 | 313 |
} |
13450 | 314 |
} |
315 |
server.react(id, actions); |
|
316 |
} |
|
13416 | 317 |
ChangeMaster(room_id, new_id) => { |
318 |
let mut actions = Vec::new(); |
|
319 |
let room_client_ids = server.room_clients(room_id); |
|
14478 | 320 |
let new_id = if server |
321 |
.room(client_id) |
|
322 |
.map(|r| r.is_fixed()) |
|
323 |
.unwrap_or(false) |
|
324 |
{ |
|
13447 | 325 |
new_id |
326 |
} else { |
|
14478 | 327 |
new_id.or_else(|| room_client_ids.iter().find(|id| **id != client_id).cloned()) |
13447 | 328 |
}; |
13416 | 329 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
330 |
||
13419 | 331 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
332 |
match r.master_id { |
|
333 |
Some(id) if id == c.id => { |
|
13486 | 334 |
c.set_is_master(false); |
13419 | 335 |
r.master_id = None; |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
336 |
/*actions.push( |
14478 | 337 |
ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
338 |
.send_all() |
|
339 |
.in_room(r.id) |
|
340 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
341 |
);*/ |
13419 | 342 |
} |
343 |
Some(_) => unreachable!(), |
|
344 |
None => {} |
|
13416 | 345 |
} |
346 |
r.master_id = new_id; |
|
13775 | 347 |
if !r.is_fixed() && c.protocol_number < 42 { |
14478 | 348 |
r.name |
349 |
.replace_range(.., new_nick.as_ref().map_or("[]", String::as_str)); |
|
13775 | 350 |
} |
13494 | 351 |
r.set_join_restriction(false); |
352 |
r.set_team_add_restriction(false); |
|
353 |
let is_fixed = r.is_fixed(); |
|
354 |
r.set_unregistered_players_restriction(is_fixed); |
|
13416 | 355 |
if let Some(nick) = new_nick { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
356 |
/*actions.push( |
14478 | 357 |
ClientFlags("+h".to_string(), vec![nick]) |
358 |
.send_all() |
|
359 |
.in_room(r.id) |
|
360 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
361 |
);*/ |
13416 | 362 |
} |
363 |
} |
|
13666 | 364 |
if let Some(id) = new_id { |
365 |
server.clients[id].set_is_master(true) |
|
366 |
} |
|
13419 | 367 |
server.react(client_id, actions); |
368 |
} |
|
13416 | 369 |
SendRoomUpdate(old_name) => { |
13666 | 370 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13419 | 371 |
let name = old_name.unwrap_or_else(|| r.name.clone()); |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
372 |
/*let actions = vec![RoomUpdated(name, r.info(Some(&c))) |
14478 | 373 |
.send_all() |
374 |
.with_protocol(r.protocol_number) |
|
375 |
.action()]; |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
376 |
server.react(client_id, actions);*/ |
13666 | 377 |
} |
14478 | 378 |
} |
13423 | 379 |
StartRoomGame(room_id) => { |
380 |
let actions = { |
|
14478 | 381 |
let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server |
382 |
.clients |
|
383 |
.iter() |
|
384 |
.map(|(id, c)| (id, c.nick.clone())) |
|
385 |
.unzip(); |
|
13423 | 386 |
let room = &mut server.rooms[room_id]; |
387 |
||
388 |
if !room.has_multiple_clans() { |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
389 |
vec![/*Warn( |
14478 | 390 |
"The game can't be started with less than two clans!".to_string(), |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
391 |
)*/] |
14478 | 392 |
} else if room.protocol_number <= 43 |
393 |
&& room.players_number != room.ready_players_number |
|
394 |
{ |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
395 |
vec![/*Warn("Not all players are ready".to_string())*/] |
13423 | 396 |
} else if room.game_info.is_some() { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
397 |
vec![/*Warn("The game is already in progress".to_string())*/] |
13423 | 398 |
} else { |
13427 | 399 |
room.start_round(); |
13423 | 400 |
for id in room_clients { |
401 |
let c = &mut server.clients[id]; |
|
13486 | 402 |
c.set_is_in_game(false); |
13423 | 403 |
c.team_indices = room.client_team_indices(c.id); |
404 |
} |
|
14478 | 405 |
vec![ |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
406 |
/*RunGame.send_all().in_room(room.id).action(),*/ |
14478 | 407 |
SendRoomUpdate(None), |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
408 |
/*ClientFlags("+g".to_string(), room_nicks) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
409 |
.send_all() |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
410 |
.in_room(room.id) |
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
411 |
.action(),*/ |
14478 | 412 |
] |
13423 | 413 |
} |
414 |
}; |
|
415 |
server.react(client_id, actions); |
|
13416 | 416 |
} |
13423 | 417 |
SendTeamRemovalMessage(team_name) => { |
418 |
let mut actions = Vec::new(); |
|
13521 | 419 |
if let Some(r) = server.room(client_id) { |
13423 | 420 |
if let Some(ref mut info) = r.game_info { |
421 |
let msg = once(b'F').chain(team_name.bytes()); |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
422 |
/*actions.push( |
14478 | 423 |
ForwardEngineMessage(vec![to_engine_msg(msg)]) |
424 |
.send_all() |
|
425 |
.in_room(r.id) |
|
426 |
.but_self() |
|
427 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
428 |
);*/ |
13423 | 429 |
info.teams_in_game -= 1; |
430 |
if info.teams_in_game == 0 { |
|
431 |
actions.push(FinishRoomGame(r.id)); |
|
432 |
} |
|
13427 | 433 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
13443 | 434 |
if let Some(m) = &info.sync_msg { |
435 |
info.msg_log.push(m.clone()); |
|
13427 | 436 |
} |
13443 | 437 |
if info.sync_msg.is_some() { |
438 |
info.sync_msg = None |
|
439 |
} |
|
440 |
info.msg_log.push(remove_msg.clone()); |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
441 |
/*actions.push( |
14478 | 442 |
ForwardEngineMessage(vec![remove_msg]) |
443 |
.send_all() |
|
444 |
.in_room(r.id) |
|
445 |
.but_self() |
|
446 |
.action(), |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
447 |
);*/ |
13423 | 448 |
} |
449 |
} |
|
450 |
server.react(client_id, actions); |
|
451 |
} |
|
452 |
FinishRoomGame(room_id) => { |
|
13426 | 453 |
let mut actions = Vec::new(); |
454 |
||
13775 | 455 |
let r = &mut server.rooms[room_id]; |
456 |
r.ready_players_number = 1; |
|
457 |
actions.push(SendRoomUpdate(None)); |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
458 |
//actions.push(RoundFinished.send_all().in_room(r.id).action()); |
13775 | 459 |
|
460 |
if let Some(info) = replace(&mut r.game_info, None) { |
|
13426 | 461 |
for (_, c) in server.clients.iter() { |
13486 | 462 |
if c.room_id == Some(room_id) && c.is_joined_mid_game() { |
14478 | 463 |
actions.push(SendRoomData { |
464 |
to: c.id, |
|
465 |
teams: false, |
|
466 |
config: true, |
|
467 |
flags: false, |
|
468 |
}); |
|
13500 | 469 |
for name in &info.left_teams { |
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
470 |
//actions.push(TeamRemove(name.clone()).send(c.id).action()); |
13426 | 471 |
} |
472 |
} |
|
473 |
} |
|
474 |
} |
|
475 |
||
14478 | 476 |
let nicks: Vec<_> = server |
477 |
.clients |
|
478 |
.iter_mut() |
|
13426 | 479 |
.filter(|(_, c)| c.room_id == Some(room_id)) |
480 |
.map(|(_, c)| { |
|
13666 | 481 |
c.set_is_ready(c.is_master()); |
13486 | 482 |
c.set_is_joined_mid_game(false); |
13426 | 483 |
c |
14478 | 484 |
}) |
485 |
.filter_map(|c| { |
|
486 |
if !c.is_master() { |
|
487 |
Some(c.nick.clone()) |
|
488 |
} else { |
|
489 |
None |
|
490 |
} |
|
491 |
}) |
|
492 |
.collect(); |
|
13775 | 493 |
|
13426 | 494 |
if !nicks.is_empty() { |
13775 | 495 |
let msg = if r.protocol_number < 38 { |
496 |
LegacyReady(false, nicks) |
|
497 |
} else { |
|
498 |
ClientFlags("-r".to_string(), nicks) |
|
499 |
}; |
|
14704
932ff7683653
Server action refactoring part 8 of N
alfadur <mail@none>
parents:
14696
diff
changeset
|
500 |
//actions.push(msg.send_all().in_room(room_id).action()); |
13426 | 501 |
} |
13423 | 502 |
server.react(client_id, actions); |
503 |
} |
|
12144 | 504 |
} |
505 |
} |