author | alfadur |
Wed, 04 Jul 2018 04:42:16 +0300 | |
changeset 13439 | c4f917c6be51 |
parent 13428 | c8425fbcf1d9 |
child 13443 | 2501428303a2 |
permissions | -rw-r--r-- |
13416 | 1 |
use std::{ |
13423 | 2 |
io, io::Write, |
13426 | 3 |
iter::once, |
4 |
mem::swap |
|
13416 | 5 |
}; |
6 |
use super::{ |
|
7 |
server::HWServer, |
|
13423 | 8 |
room::{RoomId, GameInfo}, |
13419 | 9 |
client::{ClientId, HWClient}, |
13416 | 10 |
room::HWRoom, |
11 |
handlers |
|
12 |
}; |
|
13 |
use protocol::messages::{ |
|
14 |
HWProtocolMessage, |
|
15 |
HWServerMessage, |
|
16 |
HWServerMessage::* |
|
17 |
}; |
|
13423 | 18 |
use utils::to_engine_msg; |
12138 | 19 |
|
13419 | 20 |
pub enum Destination { |
13426 | 21 |
ToId(ClientId), |
13419 | 22 |
ToSelf, |
13423 | 23 |
ToAll { |
13419 | 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 { |
|
13426 | 36 |
pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage { |
37 |
PendingMessage{ destination: Destination::ToId(client_id), message} |
|
38 |
} |
|
39 |
||
13419 | 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 { |
|
13426 | 82 |
pub fn send(self, client_id: ClientId) -> PendingMessage { PendingMessage::send(self, client_id) } |
13419 | 83 |
pub fn send_self(self) -> PendingMessage { PendingMessage::send_self(self) } |
84 |
pub fn send_all(self) -> PendingMessage { PendingMessage::send_all(self) } |
|
85 |
} |
|
86 |
||
12138 | 87 |
pub enum Action { |
13419 | 88 |
Send(PendingMessage), |
12139 | 89 |
RemoveClient, |
90 |
ByeClient(String), |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
91 |
ReactProtocolMessage(HWProtocolMessage), |
12145 | 92 |
CheckRegistered, |
93 |
JoinLobby, |
|
12147 | 94 |
AddRoom(String, Option<String>), |
13416 | 95 |
RemoveRoom(RoomId), |
96 |
MoveToRoom(RoomId), |
|
97 |
MoveToLobby(String), |
|
98 |
ChangeMaster(RoomId, Option<ClientId>), |
|
13419 | 99 |
RemoveTeam(String), |
100 |
RemoveClientTeams, |
|
13416 | 101 |
SendRoomUpdate(Option<String>), |
13423 | 102 |
StartRoomGame(RoomId), |
103 |
SendTeamRemovalMessage(String), |
|
104 |
FinishRoomGame(RoomId), |
|
13426 | 105 |
SendRoomData{to: ClientId, teams: bool, config: bool, flags: bool}, |
12147 | 106 |
Warn(String), |
13416 | 107 |
ProtocolError(String) |
12138 | 108 |
} |
12144 | 109 |
|
110 |
use self::Action::*; |
|
111 |
||
13419 | 112 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12144 | 113 |
match action { |
13419 | 114 |
Send(msg) => server.send(client_id, msg.destination, msg.message), |
12144 | 115 |
ByeClient(msg) => { |
13416 | 116 |
let room_id; |
117 |
let nick; |
|
118 |
{ |
|
13419 | 119 |
let c = &server.clients[client_id]; |
13416 | 120 |
room_id = c.room_id; |
121 |
nick = c.nick.clone(); |
|
122 |
} |
|
123 |
||
13419 | 124 |
room_id.map (|id| { |
125 |
if id != server.lobby_id { |
|
126 |
server.react(client_id, vec![ |
|
127 |
MoveToLobby(format!("quit: {}", msg.clone()))]); |
|
13416 | 128 |
} |
129 |
}); |
|
130 |
||
13419 | 131 |
server.react(client_id, vec![ |
132 |
LobbyLeft(nick, msg.clone()).send_all().action(), |
|
133 |
Bye(msg).send_self().action(), |
|
13416 | 134 |
RemoveClient]); |
12144 | 135 |
}, |
136 |
RemoveClient => { |
|
13419 | 137 |
server.removed_clients.push(client_id); |
138 |
if server.clients.contains(client_id) { |
|
139 |
server.clients.remove(client_id); |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
140 |
} |
12144 | 141 |
}, |
142 |
ReactProtocolMessage(msg) => |
|
13419 | 143 |
handlers::handle(server, client_id, msg), |
12145 | 144 |
CheckRegistered => |
13419 | 145 |
if server.clients[client_id].protocol_number > 0 && server.clients[client_id].nick != "" { |
146 |
server.react(client_id, vec![ |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
147 |
JoinLobby, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
148 |
]); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
149 |
}, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
150 |
JoinLobby => { |
13419 | 151 |
server.clients[client_id].room_id = Some(server.lobby_id); |
12147 | 152 |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
153 |
let joined_msg; |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
154 |
{ |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
155 |
let mut lobby_nicks = Vec::new(); |
12852 | 156 |
for (_, c) in server.clients.iter() { |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
157 |
if c.room_id.is_some() { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
158 |
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
|
159 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
160 |
} |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
161 |
joined_msg = LobbyJoined(lobby_nicks); |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
162 |
} |
13419 | 163 |
let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]); |
13416 | 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()); |
|
13419 | 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(), |
|
12145 | 182 |
]); |
183 |
}, |
|
12147 | 184 |
AddRoom(name, password) => { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
185 |
let room_id = server.add_room();; |
13419 | 186 |
let actions = { |
12148 | 187 |
let r = &mut server.rooms[room_id]; |
13419 | 188 |
let c = &mut server.clients[client_id]; |
13416 | 189 |
r.master_id = Some(c.id); |
12148 | 190 |
r.name = name; |
191 |
r.password = password; |
|
192 |
r.protocol_number = c.protocol_number; |
|
13416 | 193 |
|
13419 | 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); |
|
13416 | 200 |
}, |
201 |
RemoveRoom(room_id) => { |
|
13419 | 202 |
let actions = { |
13416 | 203 |
let r = &mut server.rooms[room_id]; |
13419 | 204 |
vec![RoomRemove(r.name.clone()).send_all() |
205 |
.with_protocol(r.protocol_number).action()] |
|
206 |
}; |
|
13416 | 207 |
server.rooms.remove(room_id); |
13419 | 208 |
server.react(client_id, actions); |
13416 | 209 |
} |
210 |
MoveToRoom(room_id) => { |
|
13419 | 211 |
let actions = { |
13416 | 212 |
let r = &mut server.rooms[room_id]; |
13419 | 213 |
let c = &mut server.clients[client_id]; |
13416 | 214 |
r.players_number += 1; |
12148 | 215 |
c.room_id = Some(room_id); |
13416 | 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 |
} |
|
13419 | 225 |
|
13422 | 226 |
let mut v = vec![ |
227 |
RoomJoined(vec![c.nick.clone()]).send_all().in_room(room_id).action(), |
|
13427 | 228 |
ClientFlags("+i".to_string(), vec![c.nick.clone()]).send_all().action(), |
13422 | 229 |
SendRoomUpdate(None)]; |
230 |
if !c.is_master { |
|
13427 | 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 |
||
13426 | 255 |
v.push(SendRoomData{ to: client_id, teams: true, config: true, flags: true}); |
13427 | 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( |
|
13428 | 262 |
vec![to_engine_msg("e$spectate 1".bytes())]) |
263 |
.send_self().action()); |
|
264 |
v.push(ForwardEngineMessage(info.msg_log.clone()) |
|
13427 | 265 |
.send_self().action()); |
266 |
||
267 |
for name in team_names.iter() { |
|
268 |
v.push(ForwardEngineMessage( |
|
13428 | 269 |
vec![to_engine_msg(once(b'G').chain(name.bytes()))]) |
13427 | 270 |
.send_all().in_room(r.id).action()); |
271 |
} |
|
272 |
if info.is_paused { |
|
13428 | 273 |
v.push(ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
13427 | 274 |
.send_all().in_room(r.id).action()) |
275 |
} |
|
276 |
} |
|
13422 | 277 |
} |
278 |
v |
|
13419 | 279 |
}; |
280 |
server.react(client_id, actions); |
|
13424 | 281 |
} |
13426 | 282 |
SendRoomData {to, teams, config, flags} => { |
13424 | 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()) |
|
13426 | 288 |
.send(to).action()); |
13424 | 289 |
for cfg in r.game_config().into_iter() { |
13439 | 290 |
actions.push(cfg.to_server_msg().send(to).action()); |
13424 | 291 |
} |
292 |
} |
|
293 |
if teams { |
|
13427 | 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() { |
|
13424 | 299 |
actions.push(TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team)) |
13426 | 300 |
.send(to).action()); |
13427 | 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()); |
|
13424 | 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()]) |
|
13426 | 310 |
.send(to).action()); |
13424 | 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) |
|
13426 | 317 |
.send(to).action()); |
13424 | 318 |
} |
319 |
} |
|
320 |
} |
|
321 |
server.react(client_id, actions); |
|
322 |
} |
|
13416 | 323 |
MoveToLobby(msg) => { |
324 |
let mut actions = Vec::new(); |
|
325 |
let lobby_id = server.lobby_id; |
|
13419 | 326 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13416 | 327 |
r.players_number -= 1; |
13424 | 328 |
if c.is_ready && r.ready_players_number > 0 { |
13416 | 329 |
r.ready_players_number -= 1; |
330 |
} |
|
331 |
if r.players_number > 0 && c.is_master { |
|
332 |
actions.push(ChangeMaster(r.id, None)); |
|
333 |
} |
|
13419 | 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()); |
|
13416 | 339 |
actions.push(SendRoomUpdate(Some(r.name.clone()))); |
340 |
} |
|
13419 | 341 |
server.react(client_id, actions); |
13416 | 342 |
actions = Vec::new(); |
343 |
||
13419 | 344 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13416 | 345 |
c.room_id = Some(lobby_id); |
346 |
if r.players_number == 0 { |
|
347 |
actions.push(RemoveRoom(r.id)); |
|
348 |
} |
|
349 |
} |
|
13419 | 350 |
server.react(client_id, actions) |
13416 | 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(|| |
|
13419 | 356 |
room_client_ids.iter().find(|id| **id != client_id).map(|id| *id)); |
13416 | 357 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
358 |
||
13419 | 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 => {} |
|
13416 | 369 |
} |
370 |
r.master_id = new_id; |
|
371 |
if let Some(nick) = new_nick { |
|
13419 | 372 |
actions.push(ClientFlags("+h".to_string(), vec![nick]) |
373 |
.send_all().in_room(r.id).action()); |
|
13416 | 374 |
} |
375 |
} |
|
376 |
new_id.map(|id| server.clients[id].is_master = true); |
|
13419 | 377 |
server.react(client_id, actions); |
378 |
} |
|
379 |
RemoveTeam(name) => { |
|
13426 | 380 |
let mut actions = Vec::new(); |
381 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13419 | 382 |
r.remove_team(&name); |
13426 | 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 |
} |
|
13419 | 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); |
|
13416 | 401 |
} |
402 |
SendRoomUpdate(old_name) => { |
|
13419 | 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); |
|
13423 | 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 { |
|
13427 | 423 |
room.start_round(); |
13423 | 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); |
|
13416 | 436 |
} |
13423 | 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()); |
|
13428 | 442 |
actions.push(ForwardEngineMessage(vec![to_engine_msg(msg)]). |
13423 | 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 |
} |
|
13427 | 448 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
449 |
match &info.last_msg { |
|
13428 | 450 |
Some(m) => info.msg_log.push(m.clone()), |
451 |
None => info.msg_log.push(remove_msg.clone()) |
|
13427 | 452 |
} |
13428 | 453 |
actions.push(ForwardEngineMessage(vec![remove_msg]) |
13427 | 454 |
.send_all().in_room(r.id).but_self().action()); |
13423 | 455 |
} |
456 |
} |
|
457 |
server.react(client_id, actions); |
|
458 |
} |
|
459 |
FinishRoomGame(room_id) => { |
|
13426 | 460 |
let mut actions = Vec::new(); |
461 |
let mut old_info = None; |
|
462 |
{ |
|
13423 | 463 |
let r = &mut server.rooms[room_id]; |
13426 | 464 |
swap(&mut old_info, &mut r.game_info); |
13423 | 465 |
r.game_info = None; |
13426 | 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 |
} |
|
13423 | 500 |
server.react(client_id, actions); |
501 |
} |
|
12147 | 502 |
Warn(msg) => { |
13419 | 503 |
run_action(server, client_id, Warning(msg).send_self().action()); |
12147 | 504 |
} |
13416 | 505 |
ProtocolError(msg) => { |
13419 | 506 |
run_action(server, client_id, Error(msg).send_self().action()) |
13416 | 507 |
} |
12144 | 508 |
} |
509 |
} |