author | alfadur |
Fri, 20 Jul 2018 00:02:52 +0300 | |
changeset 13527 | e3ae9eea0689 |
parent 13524 | 5359ff75da3a |
child 13666 | 09f4a30e50cc |
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, |
|
13523 | 8 |
room::{GameInfo, RoomFlags}, |
13478 | 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>, |
13520 | 28 |
protocol: Option<u16>, |
13419 | 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 |
||
13520 | 63 |
pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage { |
13419 | 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 { |
13524 | 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 |
||
13524 | 129 |
if let Some(id) = room_id{ |
13419 | 130 |
if id != server.lobby_id { |
131 |
server.react(client_id, vec![ |
|
132 |
MoveToLobby(format!("quit: {}", msg.clone()))]); |
|
13416 | 133 |
} |
13524 | 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), |
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); |
13520 | 221 |
|
222 |
let is_master = r.master_id == Some(c.id); |
|
223 |
c.set_is_master(is_master); |
|
224 |
c.set_is_ready(is_master); |
|
225 |
c.set_is_joined_mid_game(false); |
|
226 |
||
227 |
if is_master { |
|
13416 | 228 |
r.ready_players_number += 1; |
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 |
} |
|
13520 | 239 |
if !c.is_master() { |
13427 | 240 |
let team_names: Vec<_>; |
241 |
if let Some(ref mut info) = r.game_info { |
|
13520 | 242 |
c.set_is_in_game(true); |
243 |
c.set_is_joined_mid_game(true); |
|
13427 | 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 |
||
13524 | 276 |
for name in &team_names { |
13427 | 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()); |
13524 | 298 |
for cfg in r.game_config() { |
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() |
|
13520 | 322 |
.filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready()) |
13424 | 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(); |
|
13527 | 334 |
if let Some(r) = server.room(client_id) { |
13478 | 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() { |
|
13527 | 338 |
actions.push(server_chat("Your vote has been counted.".to_string()) |
339 |
.send_self().action()); |
|
13478 | 340 |
voting.votes.push((client_id, vote)); |
341 |
let i = voting.votes.iter(); |
|
342 |
let pro = i.clone().filter(|(_, v)| *v).count(); |
|
343 |
let contra = i.filter(|(_, v)| !*v).count(); |
|
344 |
let success_quota = voting.voters.len() / 2 + 1; |
|
345 |
if is_forced && vote || pro >= success_quota { |
|
346 |
result = Some(true); |
|
347 |
} else if is_forced && !vote || contra > voting.voters.len() - success_quota { |
|
348 |
result = Some(false); |
|
349 |
} |
|
350 |
} else { |
|
13527 | 351 |
actions.push(server_chat("You already have voted.".to_string()) |
352 |
.send_self().action()); |
|
13478 | 353 |
} |
354 |
} else { |
|
13527 | 355 |
actions.push(server_chat("There's no voting going on.".to_string()) |
356 |
.send_self().action()); |
|
13478 | 357 |
} |
358 |
||
359 |
if let Some(res) = result { |
|
13527 | 360 |
actions.push(server_chat("Voting closed.".to_string()) |
13478 | 361 |
.send_all().in_room(r.id).action()); |
362 |
let voting = replace(&mut r.voting, None).unwrap(); |
|
363 |
if res { |
|
364 |
actions.push(ApplyVoting(voting.kind, r.id)); |
|
365 |
} |
|
366 |
} |
|
367 |
} |
|
368 |
||
369 |
server.react(client_id, actions); |
|
370 |
} |
|
371 |
ApplyVoting(kind, room_id) => { |
|
372 |
let mut actions = Vec::new(); |
|
373 |
let mut id = client_id; |
|
374 |
match kind { |
|
375 |
VoteType::Kick(nick) => { |
|
376 |
if let Some(c) = server.find_client(&nick) { |
|
377 |
if c.room_id == Some(room_id) { |
|
378 |
id = c.id; |
|
379 |
actions.push(Kicked.send_self().action()); |
|
380 |
actions.push(MoveToLobby("kicked".to_string())); |
|
381 |
} |
|
382 |
} |
|
383 |
}, |
|
13527 | 384 |
VoteType::Map(None) => (), |
385 |
VoteType::Map(Some(name)) => { |
|
386 |
if let Some(location) = server.rooms[room_id].load_config(&name) { |
|
387 |
actions.push(server_chat(location.to_string()) |
|
388 |
.send_all().in_room(room_id).action()); |
|
389 |
actions.push(SendRoomUpdate(None)); |
|
390 |
for (_, c) in server.clients.iter() { |
|
391 |
if c.room_id == Some(room_id) { |
|
392 |
actions.push(SendRoomData{ |
|
393 |
to: c.id, teams: false, |
|
394 |
config: true, flags: false}) |
|
395 |
} |
|
396 |
} |
|
397 |
} |
|
13478 | 398 |
}, |
399 |
VoteType::Pause => { |
|
13480 | 400 |
if let Some(ref mut info) = server.rooms[room_id].game_info { |
13478 | 401 |
info.is_paused = !info.is_paused; |
13527 | 402 |
actions.push(server_chat("Pause toggled.".to_string()) |
13478 | 403 |
.send_all().in_room(room_id).action()); |
404 |
actions.push(ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
|
405 |
.send_all().in_room(room_id).action()); |
|
406 |
} |
|
407 |
}, |
|
408 |
VoteType::NewSeed => { |
|
13480 | 409 |
let seed = thread_rng().gen_range(0, 1_000_000_000).to_string(); |
410 |
let cfg = GameCfg::Seed(seed); |
|
411 |
actions.push(cfg.to_server_msg().send_all().in_room(room_id).action()); |
|
412 |
server.rooms[room_id].set_config(cfg); |
|
13478 | 413 |
}, |
414 |
VoteType::HedgehogsPerTeam(number) => { |
|
415 |
let r = &mut server.rooms[room_id]; |
|
416 |
let nicks = r.set_hedgehogs_number(number); |
|
417 |
actions.extend(nicks.into_iter().map(|n| |
|
418 |
HedgehogsNumber(n, number).send_all().in_room(room_id).action() |
|
419 |
)); |
|
420 |
}, |
|
421 |
} |
|
422 |
server.react(id, actions); |
|
423 |
} |
|
13416 | 424 |
MoveToLobby(msg) => { |
425 |
let mut actions = Vec::new(); |
|
426 |
let lobby_id = server.lobby_id; |
|
13419 | 427 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13416 | 428 |
r.players_number -= 1; |
13520 | 429 |
if c.is_ready() && r.ready_players_number > 0 { |
13416 | 430 |
r.ready_players_number -= 1; |
431 |
} |
|
13523 | 432 |
if c.is_master() && (r.players_number > 0 || r.is_fixed()) { |
13416 | 433 |
actions.push(ChangeMaster(r.id, None)); |
434 |
} |
|
13419 | 435 |
actions.push(ClientFlags("-i".to_string(), vec![c.nick.clone()]) |
436 |
.send_all().action()); |
|
13416 | 437 |
} |
13419 | 438 |
server.react(client_id, actions); |
13416 | 439 |
actions = Vec::new(); |
440 |
||
13419 | 441 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13416 | 442 |
c.room_id = Some(lobby_id); |
13523 | 443 |
if r.players_number == 0 && !r.is_fixed() { |
13416 | 444 |
actions.push(RemoveRoom(r.id)); |
13477 | 445 |
} else { |
446 |
actions.push(RemoveClientTeams); |
|
447 |
actions.push(RoomLeft(c.nick.clone(), msg) |
|
448 |
.send_all().in_room(r.id).but_self().action()); |
|
449 |
actions.push(SendRoomUpdate(Some(r.name.clone()))); |
|
13416 | 450 |
} |
451 |
} |
|
13419 | 452 |
server.react(client_id, actions) |
13416 | 453 |
} |
454 |
ChangeMaster(room_id, new_id) => { |
|
455 |
let mut actions = Vec::new(); |
|
456 |
let room_client_ids = server.room_clients(room_id); |
|
13523 | 457 |
let new_id = if server.room(client_id).map(|r| r.is_fixed()).unwrap_or(false) { |
13477 | 458 |
new_id |
459 |
} else { |
|
460 |
new_id.or_else(|| |
|
13524 | 461 |
room_client_ids.iter().find(|id| **id != client_id).cloned()) |
13477 | 462 |
}; |
13416 | 463 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
464 |
||
13419 | 465 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
466 |
match r.master_id { |
|
467 |
Some(id) if id == c.id => { |
|
13520 | 468 |
c.set_is_master(false); |
13419 | 469 |
r.master_id = None; |
470 |
actions.push(ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
|
471 |
.send_all().in_room(r.id).action()); |
|
472 |
} |
|
473 |
Some(_) => unreachable!(), |
|
474 |
None => {} |
|
13416 | 475 |
} |
476 |
r.master_id = new_id; |
|
13523 | 477 |
r.set_join_restriction(false); |
478 |
r.set_team_add_restriction(false); |
|
479 |
let is_fixed = r.is_fixed(); |
|
480 |
r.set_unregistered_players_restriction(is_fixed); |
|
13416 | 481 |
if let Some(nick) = new_nick { |
13419 | 482 |
actions.push(ClientFlags("+h".to_string(), vec![nick]) |
483 |
.send_all().in_room(r.id).action()); |
|
13416 | 484 |
} |
485 |
} |
|
13524 | 486 |
if let Some(id) = new_id { server.clients[id].set_is_master(true) } |
13419 | 487 |
server.react(client_id, actions); |
488 |
} |
|
489 |
RemoveTeam(name) => { |
|
13426 | 490 |
let mut actions = Vec::new(); |
491 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13419 | 492 |
r.remove_team(&name); |
13426 | 493 |
if let Some(ref mut info) = r.game_info { |
494 |
info.left_teams.push(name.clone()); |
|
495 |
} |
|
496 |
actions.push(TeamRemove(name.clone()).send_all().in_room(r.id).action()); |
|
497 |
actions.push(SendRoomUpdate(None)); |
|
13520 | 498 |
if r.game_info.is_some() && c.is_in_game() { |
13426 | 499 |
actions.push(SendTeamRemovalMessage(name)); |
500 |
} |
|
501 |
} |
|
13419 | 502 |
server.react(client_id, actions); |
503 |
}, |
|
504 |
RemoveClientTeams => { |
|
505 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
506 |
r.client_teams(c.id).map(|t| RemoveTeam(t.name.clone())).collect() |
|
507 |
} else { |
|
508 |
Vec::new() |
|
509 |
}; |
|
510 |
server.react(client_id, actions); |
|
13416 | 511 |
} |
512 |
SendRoomUpdate(old_name) => { |
|
13419 | 513 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
514 |
let name = old_name.unwrap_or_else(|| r.name.clone()); |
|
515 |
vec![RoomUpdated(name, r.info(Some(&c))) |
|
516 |
.send_all().with_protocol(r.protocol_number).action()] |
|
517 |
} else { |
|
518 |
Vec::new() |
|
519 |
}; |
|
520 |
server.react(client_id, actions); |
|
13423 | 521 |
}, |
522 |
StartRoomGame(room_id) => { |
|
523 |
let actions = { |
|
524 |
let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server.clients.iter() |
|
525 |
.map(|(id, c)| (id, c.nick.clone())).unzip(); |
|
526 |
let room = &mut server.rooms[room_id]; |
|
527 |
||
528 |
if !room.has_multiple_clans() { |
|
529 |
vec![Warn("The game can't be started with less than two clans!".to_string())] |
|
530 |
} else if room.game_info.is_some() { |
|
531 |
vec![Warn("The game is already in progress".to_string())] |
|
532 |
} else { |
|
13427 | 533 |
room.start_round(); |
13423 | 534 |
for id in room_clients { |
535 |
let c = &mut server.clients[id]; |
|
13520 | 536 |
c.set_is_in_game(false); |
13423 | 537 |
c.team_indices = room.client_team_indices(c.id); |
538 |
} |
|
539 |
vec![RunGame.send_all().in_room(room.id).action(), |
|
540 |
SendRoomUpdate(None), |
|
541 |
ClientFlags("+g".to_string(), room_nicks) |
|
542 |
.send_all().in_room(room.id).action()] |
|
543 |
} |
|
544 |
}; |
|
545 |
server.react(client_id, actions); |
|
13416 | 546 |
} |
13423 | 547 |
SendTeamRemovalMessage(team_name) => { |
548 |
let mut actions = Vec::new(); |
|
13527 | 549 |
if let Some(r) = server.room(client_id) { |
13423 | 550 |
if let Some(ref mut info) = r.game_info { |
551 |
let msg = once(b'F').chain(team_name.bytes()); |
|
13428 | 552 |
actions.push(ForwardEngineMessage(vec![to_engine_msg(msg)]). |
13423 | 553 |
send_all().in_room(r.id).but_self().action()); |
554 |
info.teams_in_game -= 1; |
|
555 |
if info.teams_in_game == 0 { |
|
556 |
actions.push(FinishRoomGame(r.id)); |
|
557 |
} |
|
13427 | 558 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
13443 | 559 |
if let Some(m) = &info.sync_msg { |
560 |
info.msg_log.push(m.clone()); |
|
13427 | 561 |
} |
13443 | 562 |
if info.sync_msg.is_some() { |
563 |
info.sync_msg = None |
|
564 |
} |
|
565 |
info.msg_log.push(remove_msg.clone()); |
|
13428 | 566 |
actions.push(ForwardEngineMessage(vec![remove_msg]) |
13427 | 567 |
.send_all().in_room(r.id).but_self().action()); |
13423 | 568 |
} |
569 |
} |
|
570 |
server.react(client_id, actions); |
|
571 |
} |
|
572 |
FinishRoomGame(room_id) => { |
|
13426 | 573 |
let mut actions = Vec::new(); |
13478 | 574 |
let old_info; |
13426 | 575 |
{ |
13423 | 576 |
let r = &mut server.rooms[room_id]; |
13478 | 577 |
old_info = replace(&mut r.game_info, None); |
13423 | 578 |
r.game_info = None; |
13426 | 579 |
r.ready_players_number = 1; |
580 |
actions.push(SendRoomUpdate(None)); |
|
581 |
actions.push(RoundFinished.send_all().in_room(r.id).action()); |
|
582 |
} |
|
583 |
||
584 |
if let Some(info) = old_info { |
|
585 |
for (_, c) in server.clients.iter() { |
|
13520 | 586 |
if c.room_id == Some(room_id) && c.is_joined_mid_game() { |
13426 | 587 |
actions.push(SendRoomData{ |
588 |
to: c.id, teams: false, |
|
589 |
config: true, flags: false}); |
|
13524 | 590 |
for name in &info.left_teams { |
13426 | 591 |
actions.push(TeamRemove(name.clone()) |
592 |
.send(c.id).action()); |
|
593 |
} |
|
594 |
} |
|
595 |
} |
|
596 |
} |
|
597 |
||
598 |
let nicks: Vec<_> = server.clients.iter_mut() |
|
599 |
.filter(|(_, c)| c.room_id == Some(room_id)) |
|
600 |
.map(|(_, c)| { |
|
13520 | 601 |
let is_master = c.is_master(); |
602 |
c.set_is_ready(is_master); |
|
603 |
c.set_is_joined_mid_game(false); |
|
13426 | 604 |
c |
13520 | 605 |
}).filter_map(|c| if !c.is_master() { |
13426 | 606 |
Some(c.nick.clone()) |
607 |
} else { |
|
608 |
None |
|
609 |
}).collect(); |
|
610 |
if !nicks.is_empty() { |
|
611 |
actions.push(ClientFlags("-r".to_string(), nicks) |
|
612 |
.send_all().in_room(room_id).action()); |
|
613 |
} |
|
13423 | 614 |
server.react(client_id, actions); |
615 |
} |
|
12147 | 616 |
Warn(msg) => { |
13419 | 617 |
run_action(server, client_id, Warning(msg).send_self().action()); |
12147 | 618 |
} |
13416 | 619 |
ProtocolError(msg) => { |
13419 | 620 |
run_action(server, client_id, Error(msg).send_self().action()) |
13416 | 621 |
} |
12144 | 622 |
} |
623 |
} |