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