author | alfadur |
Wed, 04 Jul 2018 04:42:16 +0300 | |
changeset 13444 | c4f917c6be51 |
parent 13433 | c8425fbcf1d9 |
child 13448 | 2501428303a2 |
permissions | -rw-r--r-- |
13421 | 1 |
use std::{ |
13428 | 2 |
io, io::Write, |
13431 | 3 |
iter::once, |
4 |
mem::swap |
|
13421 | 5 |
}; |
6 |
use super::{ |
|
7 |
server::HWServer, |
|
13428 | 8 |
room::{RoomId, GameInfo}, |
13424 | 9 |
client::{ClientId, HWClient}, |
13421 | 10 |
room::HWRoom, |
11 |
handlers |
|
12 |
}; |
|
13 |
use protocol::messages::{ |
|
14 |
HWProtocolMessage, |
|
15 |
HWServerMessage, |
|
16 |
HWServerMessage::* |
|
17 |
}; |
|
13428 | 18 |
use utils::to_engine_msg; |
12143 | 19 |
|
13424 | 20 |
pub enum Destination { |
13431 | 21 |
ToId(ClientId), |
13424 | 22 |
ToSelf, |
13428 | 23 |
ToAll { |
13424 | 24 |
room_id: Option<RoomId>, |
25 |
protocol: Option<u32>, |
|
26 |
skip_self: bool |
|
27 |
} |
|
28 |
} |
|
29 |
||
30 |
pub struct PendingMessage { |
|
31 |
pub destination: Destination, |
|
32 |
pub message: HWServerMessage |
|
33 |
} |
|
34 |
||
35 |
impl PendingMessage { |
|
13431 | 36 |
pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage { |
37 |
PendingMessage{ destination: Destination::ToId(client_id), message} |
|
38 |
} |
|
39 |
||
13424 | 40 |
pub fn send_self(message: HWServerMessage) -> PendingMessage { |
41 |
PendingMessage{ destination: Destination::ToSelf, message } |
|
42 |
} |
|
43 |
||
44 |
pub fn send_all(message: HWServerMessage) -> PendingMessage { |
|
45 |
let destination = Destination::ToAll { |
|
46 |
room_id: None, |
|
47 |
protocol: None, |
|
48 |
skip_self: false, |
|
49 |
}; |
|
50 |
PendingMessage{ destination, message } |
|
51 |
} |
|
52 |
||
53 |
pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage { |
|
54 |
if let Destination::ToAll {ref mut room_id, ..} = self.destination { |
|
55 |
*room_id = Some(clients_room_id) |
|
56 |
} |
|
57 |
self |
|
58 |
} |
|
59 |
||
60 |
pub fn with_protocol(mut self, protocol_number: u32) -> PendingMessage { |
|
61 |
if let Destination::ToAll {ref mut protocol, ..} = self.destination { |
|
62 |
*protocol = Some(protocol_number) |
|
63 |
} |
|
64 |
self |
|
65 |
} |
|
66 |
||
67 |
pub fn but_self(mut self) -> PendingMessage { |
|
68 |
if let Destination::ToAll {ref mut skip_self, ..} = self.destination { |
|
69 |
*skip_self = true |
|
70 |
} |
|
71 |
self |
|
72 |
} |
|
73 |
||
74 |
pub fn action(self) -> Action { Send(self) } |
|
75 |
} |
|
76 |
||
77 |
impl Into<Action> for PendingMessage { |
|
78 |
fn into(self) -> Action { self.action() } |
|
79 |
} |
|
80 |
||
81 |
impl HWServerMessage { |
|
13431 | 82 |
pub fn send(self, client_id: ClientId) -> PendingMessage { PendingMessage::send(self, client_id) } |
13424 | 83 |
pub fn send_self(self) -> PendingMessage { PendingMessage::send_self(self) } |
84 |
pub fn send_all(self) -> PendingMessage { PendingMessage::send_all(self) } |
|
85 |
} |
|
86 |
||
12143 | 87 |
pub enum Action { |
13424 | 88 |
Send(PendingMessage), |
12144 | 89 |
RemoveClient, |
90 |
ByeClient(String), |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
91 |
ReactProtocolMessage(HWProtocolMessage), |
12150 | 92 |
CheckRegistered, |
93 |
JoinLobby, |
|
12152 | 94 |
AddRoom(String, Option<String>), |
13421 | 95 |
RemoveRoom(RoomId), |
96 |
MoveToRoom(RoomId), |
|
97 |
MoveToLobby(String), |
|
98 |
ChangeMaster(RoomId, Option<ClientId>), |
|
13424 | 99 |
RemoveTeam(String), |
100 |
RemoveClientTeams, |
|
13421 | 101 |
SendRoomUpdate(Option<String>), |
13428 | 102 |
StartRoomGame(RoomId), |
103 |
SendTeamRemovalMessage(String), |
|
104 |
FinishRoomGame(RoomId), |
|
13431 | 105 |
SendRoomData{to: ClientId, teams: bool, config: bool, flags: bool}, |
12152 | 106 |
Warn(String), |
13421 | 107 |
ProtocolError(String) |
12143 | 108 |
} |
12149 | 109 |
|
110 |
use self::Action::*; |
|
111 |
||
13424 | 112 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12149 | 113 |
match action { |
13424 | 114 |
Send(msg) => server.send(client_id, msg.destination, msg.message), |
12149 | 115 |
ByeClient(msg) => { |
13421 | 116 |
let room_id; |
117 |
let nick; |
|
118 |
{ |
|
13424 | 119 |
let c = &server.clients[client_id]; |
13421 | 120 |
room_id = c.room_id; |
121 |
nick = c.nick.clone(); |
|
122 |
} |
|
123 |
||
13424 | 124 |
room_id.map (|id| { |
125 |
if id != server.lobby_id { |
|
126 |
server.react(client_id, vec![ |
|
127 |
MoveToLobby(format!("quit: {}", msg.clone()))]); |
|
13421 | 128 |
} |
129 |
}); |
|
130 |
||
13424 | 131 |
server.react(client_id, vec![ |
132 |
LobbyLeft(nick, msg.clone()).send_all().action(), |
|
133 |
Bye(msg).send_self().action(), |
|
13421 | 134 |
RemoveClient]); |
12149 | 135 |
}, |
136 |
RemoveClient => { |
|
13424 | 137 |
server.removed_clients.push(client_id); |
138 |
if server.clients.contains(client_id) { |
|
139 |
server.clients.remove(client_id); |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
140 |
} |
12149 | 141 |
}, |
142 |
ReactProtocolMessage(msg) => |
|
13424 | 143 |
handlers::handle(server, client_id, msg), |
12150 | 144 |
CheckRegistered => |
13424 | 145 |
if server.clients[client_id].protocol_number > 0 && server.clients[client_id].nick != "" { |
146 |
server.react(client_id, vec![ |
|
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
147 |
JoinLobby, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
148 |
]); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
149 |
}, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
150 |
JoinLobby => { |
13424 | 151 |
server.clients[client_id].room_id = Some(server.lobby_id); |
12152 | 152 |
|
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
153 |
let joined_msg; |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
154 |
{ |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
155 |
let mut lobby_nicks = Vec::new(); |
12857 | 156 |
for (_, c) in server.clients.iter() { |
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
157 |
if c.room_id.is_some() { |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
158 |
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
|
159 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
160 |
} |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
161 |
joined_msg = LobbyJoined(lobby_nicks); |
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
162 |
} |
13424 | 163 |
let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]); |
13421 | 164 |
let flags_msg = ClientFlags( |
165 |
"+i".to_string(), |
|
166 |
server.clients.iter() |
|
167 |
.filter(|(_, c)| c.room_id.is_some()) |
|
168 |
.map(|(_, c)| c.nick.clone()) |
|
169 |
.collect()); |
|
170 |
let server_msg = ServerMessage("\u{1f994} is watching".to_string()); |
|
171 |
let rooms_msg = Rooms(server.rooms.iter() |
|
172 |
.filter(|(id, _)| *id != server.lobby_id) |
|
173 |
.flat_map(|(_, r)| |
|
174 |
r.info(r.master_id.map(|id| &server.clients[id]))) |
|
175 |
.collect()); |
|
13424 | 176 |
server.react(client_id, vec![ |
177 |
everyone_msg.send_all().but_self().action(), |
|
178 |
joined_msg.send_self().action(), |
|
179 |
flags_msg.send_self().action(), |
|
180 |
server_msg.send_self().action(), |
|
181 |
rooms_msg.send_self().action(), |
|
12150 | 182 |
]); |
183 |
}, |
|
12152 | 184 |
AddRoom(name, password) => { |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
185 |
let room_id = server.add_room();; |
13424 | 186 |
let actions = { |
12153 | 187 |
let r = &mut server.rooms[room_id]; |
13424 | 188 |
let c = &mut server.clients[client_id]; |
13421 | 189 |
r.master_id = Some(c.id); |
12153 | 190 |
r.name = name; |
191 |
r.password = password; |
|
192 |
r.protocol_number = c.protocol_number; |
|
13421 | 193 |
|
13424 | 194 |
vec![ |
195 |
RoomAdd(r.info(Some(&c))).send_all() |
|
196 |
.with_protocol(r.protocol_number).action(), |
|
197 |
MoveToRoom(room_id)] |
|
198 |
}; |
|
199 |
server.react(client_id, actions); |
|
13421 | 200 |
}, |
201 |
RemoveRoom(room_id) => { |
|
13424 | 202 |
let actions = { |
13421 | 203 |
let r = &mut server.rooms[room_id]; |
13424 | 204 |
vec![RoomRemove(r.name.clone()).send_all() |
205 |
.with_protocol(r.protocol_number).action()] |
|
206 |
}; |
|
13421 | 207 |
server.rooms.remove(room_id); |
13424 | 208 |
server.react(client_id, actions); |
13421 | 209 |
} |
210 |
MoveToRoom(room_id) => { |
|
13424 | 211 |
let actions = { |
13421 | 212 |
let r = &mut server.rooms[room_id]; |
13424 | 213 |
let c = &mut server.clients[client_id]; |
13421 | 214 |
r.players_number += 1; |
12153 | 215 |
c.room_id = Some(room_id); |
13421 | 216 |
c.is_joined_mid_game = false; |
217 |
if r.master_id == Some(c.id) { |
|
218 |
r.ready_players_number += 1; |
|
219 |
c.is_master = true; |
|
220 |
c.is_ready = true; |
|
221 |
} else { |
|
222 |
c.is_ready = false; |
|
223 |
c.is_master = false; |
|
224 |
} |
|
13424 | 225 |
|
13427 | 226 |
let mut v = vec![ |
227 |
RoomJoined(vec![c.nick.clone()]).send_all().in_room(room_id).action(), |
|
13432 | 228 |
ClientFlags("+i".to_string(), vec![c.nick.clone()]).send_all().action(), |
13427 | 229 |
SendRoomUpdate(None)]; |
230 |
if !c.is_master { |
|
13432 | 231 |
let team_names: Vec<_>; |
232 |
if let Some(ref mut info) = r.game_info { |
|
233 |
c.is_in_game = true; |
|
234 |
c.is_joined_mid_game = true; |
|
235 |
||
236 |
{ |
|
237 |
let teams = info.client_teams(c.id); |
|
238 |
c.teams_in_game = teams.clone().count() as u8; |
|
239 |
c.clan = teams.clone().next().map(|t| t.color); |
|
240 |
team_names = teams.map(|t| t.name.clone()).collect(); |
|
241 |
} |
|
242 |
||
243 |
if !team_names.is_empty() { |
|
244 |
info.left_teams.retain(|name| |
|
245 |
!team_names.contains(&name)); |
|
246 |
info.teams_in_game += team_names.len() as u8; |
|
247 |
r.teams = info.teams_at_start.iter() |
|
248 |
.filter(|(_, t)| !team_names.contains(&t.name)) |
|
249 |
.cloned().collect(); |
|
250 |
} |
|
251 |
} else { |
|
252 |
team_names = Vec::new(); |
|
253 |
} |
|
254 |
||
13431 | 255 |
v.push(SendRoomData{ to: client_id, teams: true, config: true, flags: true}); |
13432 | 256 |
|
257 |
if let Some(ref info) = r.game_info { |
|
258 |
v.push(RunGame.send_self().action()); |
|
259 |
v.push(ClientFlags("+g".to_string(), vec![c.nick.clone()]) |
|
260 |
.send_all().in_room(r.id).action()); |
|
261 |
v.push(ForwardEngineMessage( |
|
13433 | 262 |
vec![to_engine_msg("e$spectate 1".bytes())]) |
263 |
.send_self().action()); |
|
264 |
v.push(ForwardEngineMessage(info.msg_log.clone()) |
|
13432 | 265 |
.send_self().action()); |
266 |
||
267 |
for name in team_names.iter() { |
|
268 |
v.push(ForwardEngineMessage( |
|
13433 | 269 |
vec![to_engine_msg(once(b'G').chain(name.bytes()))]) |
13432 | 270 |
.send_all().in_room(r.id).action()); |
271 |
} |
|
272 |
if info.is_paused { |
|
13433 | 273 |
v.push(ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
13432 | 274 |
.send_all().in_room(r.id).action()) |
275 |
} |
|
276 |
} |
|
13427 | 277 |
} |
278 |
v |
|
13424 | 279 |
}; |
280 |
server.react(client_id, actions); |
|
13429 | 281 |
} |
13431 | 282 |
SendRoomData {to, teams, config, flags} => { |
13429 | 283 |
let mut actions = Vec::new(); |
284 |
let room_id = server.clients[client_id].room_id; |
|
285 |
if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) { |
|
286 |
if config { |
|
287 |
actions.push(ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config()) |
|
13431 | 288 |
.send(to).action()); |
13429 | 289 |
for cfg in r.game_config().into_iter() { |
13444 | 290 |
actions.push(cfg.to_server_msg().send(to).action()); |
13429 | 291 |
} |
292 |
} |
|
293 |
if teams { |
|
13432 | 294 |
let current_teams = match r.game_info { |
295 |
Some(ref info) => &info.teams_at_start, |
|
296 |
None => &r.teams |
|
297 |
}; |
|
298 |
for (owner_id, team) in current_teams.iter() { |
|
13429 | 299 |
actions.push(TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team)) |
13431 | 300 |
.send(to).action()); |
13432 | 301 |
actions.push(TeamColor(team.name.clone(), team.color) |
302 |
.send(to).action()); |
|
303 |
actions.push(HedgehogsNumber(team.name.clone(), team.hedgehogs_number) |
|
304 |
.send(to).action()); |
|
13429 | 305 |
} |
306 |
} |
|
307 |
if flags { |
|
308 |
if let Some(id) = r.master_id { |
|
309 |
actions.push(ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()]) |
|
13431 | 310 |
.send(to).action()); |
13429 | 311 |
} |
312 |
let nicks: Vec<_> = server.clients.iter() |
|
313 |
.filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready) |
|
314 |
.map(|(_, c)| c.nick.clone()).collect(); |
|
315 |
if !nicks.is_empty() { |
|
316 |
actions.push(ClientFlags("+r".to_string(), nicks) |
|
13431 | 317 |
.send(to).action()); |
13429 | 318 |
} |
319 |
} |
|
320 |
} |
|
321 |
server.react(client_id, actions); |
|
322 |
} |
|
13421 | 323 |
MoveToLobby(msg) => { |
324 |
let mut actions = Vec::new(); |
|
325 |
let lobby_id = server.lobby_id; |
|
13424 | 326 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13421 | 327 |
r.players_number -= 1; |
13429 | 328 |
if c.is_ready && r.ready_players_number > 0 { |
13421 | 329 |
r.ready_players_number -= 1; |
330 |
} |
|
331 |
if r.players_number > 0 && c.is_master { |
|
332 |
actions.push(ChangeMaster(r.id, None)); |
|
333 |
} |
|
13424 | 334 |
actions.push(RemoveClientTeams); |
335 |
actions.push(RoomLeft(c.nick.clone(), msg) |
|
336 |
.send_all().in_room(r.id).but_self().action()); |
|
337 |
actions.push(ClientFlags("-i".to_string(), vec![c.nick.clone()]) |
|
338 |
.send_all().action()); |
|
13421 | 339 |
actions.push(SendRoomUpdate(Some(r.name.clone()))); |
340 |
} |
|
13424 | 341 |
server.react(client_id, actions); |
13421 | 342 |
actions = Vec::new(); |
343 |
||
13424 | 344 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13421 | 345 |
c.room_id = Some(lobby_id); |
346 |
if r.players_number == 0 { |
|
347 |
actions.push(RemoveRoom(r.id)); |
|
348 |
} |
|
349 |
} |
|
13424 | 350 |
server.react(client_id, actions) |
13421 | 351 |
} |
352 |
ChangeMaster(room_id, new_id) => { |
|
353 |
let mut actions = Vec::new(); |
|
354 |
let room_client_ids = server.room_clients(room_id); |
|
355 |
let new_id = new_id.or_else(|| |
|
13424 | 356 |
room_client_ids.iter().find(|id| **id != client_id).map(|id| *id)); |
13421 | 357 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
358 |
||
13424 | 359 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
360 |
match r.master_id { |
|
361 |
Some(id) if id == c.id => { |
|
362 |
c.is_master = false; |
|
363 |
r.master_id = None; |
|
364 |
actions.push(ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
|
365 |
.send_all().in_room(r.id).action()); |
|
366 |
} |
|
367 |
Some(_) => unreachable!(), |
|
368 |
None => {} |
|
13421 | 369 |
} |
370 |
r.master_id = new_id; |
|
371 |
if let Some(nick) = new_nick { |
|
13424 | 372 |
actions.push(ClientFlags("+h".to_string(), vec![nick]) |
373 |
.send_all().in_room(r.id).action()); |
|
13421 | 374 |
} |
375 |
} |
|
376 |
new_id.map(|id| server.clients[id].is_master = true); |
|
13424 | 377 |
server.react(client_id, actions); |
378 |
} |
|
379 |
RemoveTeam(name) => { |
|
13431 | 380 |
let mut actions = Vec::new(); |
381 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13424 | 382 |
r.remove_team(&name); |
13431 | 383 |
if let Some(ref mut info) = r.game_info { |
384 |
info.left_teams.push(name.clone()); |
|
385 |
} |
|
386 |
actions.push(TeamRemove(name.clone()).send_all().in_room(r.id).action()); |
|
387 |
actions.push(SendRoomUpdate(None)); |
|
388 |
if r.game_info.is_some() && c.is_in_game { |
|
389 |
actions.push(SendTeamRemovalMessage(name)); |
|
390 |
} |
|
391 |
} |
|
13424 | 392 |
server.react(client_id, actions); |
393 |
}, |
|
394 |
RemoveClientTeams => { |
|
395 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
396 |
r.client_teams(c.id).map(|t| RemoveTeam(t.name.clone())).collect() |
|
397 |
} else { |
|
398 |
Vec::new() |
|
399 |
}; |
|
400 |
server.react(client_id, actions); |
|
13421 | 401 |
} |
402 |
SendRoomUpdate(old_name) => { |
|
13424 | 403 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
404 |
let name = old_name.unwrap_or_else(|| r.name.clone()); |
|
405 |
vec![RoomUpdated(name, r.info(Some(&c))) |
|
406 |
.send_all().with_protocol(r.protocol_number).action()] |
|
407 |
} else { |
|
408 |
Vec::new() |
|
409 |
}; |
|
410 |
server.react(client_id, actions); |
|
13428 | 411 |
}, |
412 |
StartRoomGame(room_id) => { |
|
413 |
let actions = { |
|
414 |
let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server.clients.iter() |
|
415 |
.map(|(id, c)| (id, c.nick.clone())).unzip(); |
|
416 |
let room = &mut server.rooms[room_id]; |
|
417 |
||
418 |
if !room.has_multiple_clans() { |
|
419 |
vec![Warn("The game can't be started with less than two clans!".to_string())] |
|
420 |
} else if room.game_info.is_some() { |
|
421 |
vec![Warn("The game is already in progress".to_string())] |
|
422 |
} else { |
|
13432 | 423 |
room.start_round(); |
13428 | 424 |
for id in room_clients { |
425 |
let c = &mut server.clients[id]; |
|
426 |
c.is_in_game = true; |
|
427 |
c.team_indices = room.client_team_indices(c.id); |
|
428 |
} |
|
429 |
vec![RunGame.send_all().in_room(room.id).action(), |
|
430 |
SendRoomUpdate(None), |
|
431 |
ClientFlags("+g".to_string(), room_nicks) |
|
432 |
.send_all().in_room(room.id).action()] |
|
433 |
} |
|
434 |
}; |
|
435 |
server.react(client_id, actions); |
|
13421 | 436 |
} |
13428 | 437 |
SendTeamRemovalMessage(team_name) => { |
438 |
let mut actions = Vec::new(); |
|
439 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
440 |
if let Some(ref mut info) = r.game_info { |
|
441 |
let msg = once(b'F').chain(team_name.bytes()); |
|
13433 | 442 |
actions.push(ForwardEngineMessage(vec![to_engine_msg(msg)]). |
13428 | 443 |
send_all().in_room(r.id).but_self().action()); |
444 |
info.teams_in_game -= 1; |
|
445 |
if info.teams_in_game == 0 { |
|
446 |
actions.push(FinishRoomGame(r.id)); |
|
447 |
} |
|
13432 | 448 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
449 |
match &info.last_msg { |
|
13433 | 450 |
Some(m) => info.msg_log.push(m.clone()), |
451 |
None => info.msg_log.push(remove_msg.clone()) |
|
13432 | 452 |
} |
13433 | 453 |
actions.push(ForwardEngineMessage(vec![remove_msg]) |
13432 | 454 |
.send_all().in_room(r.id).but_self().action()); |
13428 | 455 |
} |
456 |
} |
|
457 |
server.react(client_id, actions); |
|
458 |
} |
|
459 |
FinishRoomGame(room_id) => { |
|
13431 | 460 |
let mut actions = Vec::new(); |
461 |
let mut old_info = None; |
|
462 |
{ |
|
13428 | 463 |
let r = &mut server.rooms[room_id]; |
13431 | 464 |
swap(&mut old_info, &mut r.game_info); |
13428 | 465 |
r.game_info = None; |
13431 | 466 |
r.ready_players_number = 1; |
467 |
actions.push(SendRoomUpdate(None)); |
|
468 |
actions.push(RoundFinished.send_all().in_room(r.id).action()); |
|
469 |
} |
|
470 |
||
471 |
if let Some(info) = old_info { |
|
472 |
for (_, c) in server.clients.iter() { |
|
473 |
if c.room_id == Some(room_id) && c.is_joined_mid_game { |
|
474 |
actions.push(SendRoomData{ |
|
475 |
to: c.id, teams: false, |
|
476 |
config: true, flags: false}); |
|
477 |
for name in info.left_teams.iter() { |
|
478 |
actions.push(TeamRemove(name.clone()) |
|
479 |
.send(c.id).action()); |
|
480 |
} |
|
481 |
} |
|
482 |
} |
|
483 |
} |
|
484 |
||
485 |
let nicks: Vec<_> = server.clients.iter_mut() |
|
486 |
.filter(|(_, c)| c.room_id == Some(room_id)) |
|
487 |
.map(|(_, c)| { |
|
488 |
c.is_ready = c.is_master; |
|
489 |
c.is_joined_mid_game = false; |
|
490 |
c |
|
491 |
}).filter_map(|c| if !c.is_master { |
|
492 |
Some(c.nick.clone()) |
|
493 |
} else { |
|
494 |
None |
|
495 |
}).collect(); |
|
496 |
if !nicks.is_empty() { |
|
497 |
actions.push(ClientFlags("-r".to_string(), nicks) |
|
498 |
.send_all().in_room(room_id).action()); |
|
499 |
} |
|
13428 | 500 |
server.react(client_id, actions); |
501 |
} |
|
12152 | 502 |
Warn(msg) => { |
13424 | 503 |
run_action(server, client_id, Warning(msg).send_self().action()); |
12152 | 504 |
} |
13421 | 505 |
ProtocolError(msg) => { |
13424 | 506 |
run_action(server, client_id, Error(msg).send_self().action()) |
13421 | 507 |
} |
12149 | 508 |
} |
509 |
} |