author | alfadur |
Fri, 20 Jul 2018 00:02:52 +0300 | |
changeset 13532 | e3ae9eea0689 |
parent 13529 | 5359ff75da3a |
child 13671 | 09f4a30e50cc |
permissions | -rw-r--r-- |
13421 | 1 |
use std::{ |
13428 | 2 |
io, io::Write, |
13431 | 3 |
iter::once, |
13483 | 4 |
mem::replace |
13421 | 5 |
}; |
6 |
use super::{ |
|
7 |
server::HWServer, |
|
13528 | 8 |
room::{GameInfo, RoomFlags}, |
13483 | 9 |
client::HWClient, |
13485 | 10 |
coretypes::{ClientId, RoomId, GameCfg, VoteType}, |
13421 | 11 |
room::HWRoom, |
12 |
handlers |
|
13 |
}; |
|
14 |
use protocol::messages::{ |
|
15 |
HWProtocolMessage, |
|
16 |
HWServerMessage, |
|
13483 | 17 |
HWServerMessage::*, |
18 |
server_chat |
|
13421 | 19 |
}; |
13428 | 20 |
use utils::to_engine_msg; |
13485 | 21 |
use rand::{thread_rng, Rng, distributions::Uniform}; |
12143 | 22 |
|
13424 | 23 |
pub enum Destination { |
13431 | 24 |
ToId(ClientId), |
13424 | 25 |
ToSelf, |
13428 | 26 |
ToAll { |
13424 | 27 |
room_id: Option<RoomId>, |
13525 | 28 |
protocol: Option<u16>, |
13424 | 29 |
skip_self: bool |
30 |
} |
|
31 |
} |
|
32 |
||
33 |
pub struct PendingMessage { |
|
34 |
pub destination: Destination, |
|
35 |
pub message: HWServerMessage |
|
36 |
} |
|
37 |
||
38 |
impl PendingMessage { |
|
13431 | 39 |
pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage { |
40 |
PendingMessage{ destination: Destination::ToId(client_id), message} |
|
41 |
} |
|
42 |
||
13424 | 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 |
||
13525 | 63 |
pub fn with_protocol(mut self, protocol_number: u16) -> PendingMessage { |
13424 | 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 { |
|
13431 | 85 |
pub fn send(self, client_id: ClientId) -> PendingMessage { PendingMessage::send(self, client_id) } |
13424 | 86 |
pub fn send_self(self) -> PendingMessage { PendingMessage::send_self(self) } |
87 |
pub fn send_all(self) -> PendingMessage { PendingMessage::send_all(self) } |
|
88 |
} |
|
89 |
||
12143 | 90 |
pub enum Action { |
13424 | 91 |
Send(PendingMessage), |
12144 | 92 |
RemoveClient, |
93 |
ByeClient(String), |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
94 |
ReactProtocolMessage(HWProtocolMessage), |
12150 | 95 |
CheckRegistered, |
96 |
JoinLobby, |
|
12152 | 97 |
AddRoom(String, Option<String>), |
13421 | 98 |
RemoveRoom(RoomId), |
99 |
MoveToRoom(RoomId), |
|
100 |
MoveToLobby(String), |
|
101 |
ChangeMaster(RoomId, Option<ClientId>), |
|
13424 | 102 |
RemoveTeam(String), |
103 |
RemoveClientTeams, |
|
13421 | 104 |
SendRoomUpdate(Option<String>), |
13428 | 105 |
StartRoomGame(RoomId), |
106 |
SendTeamRemovalMessage(String), |
|
107 |
FinishRoomGame(RoomId), |
|
13431 | 108 |
SendRoomData{to: ClientId, teams: bool, config: bool, flags: bool}, |
13483 | 109 |
AddVote{vote: bool, is_forced: bool}, |
110 |
ApplyVoting(VoteType, RoomId), |
|
12152 | 111 |
Warn(String), |
13421 | 112 |
ProtocolError(String) |
12143 | 113 |
} |
12149 | 114 |
|
115 |
use self::Action::*; |
|
116 |
||
13424 | 117 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12149 | 118 |
match action { |
13529 | 119 |
Send(msg) => server.send(client_id, &msg.destination, msg.message), |
12149 | 120 |
ByeClient(msg) => { |
13421 | 121 |
let room_id; |
122 |
let nick; |
|
123 |
{ |
|
13424 | 124 |
let c = &server.clients[client_id]; |
13421 | 125 |
room_id = c.room_id; |
126 |
nick = c.nick.clone(); |
|
127 |
} |
|
128 |
||
13529 | 129 |
if let Some(id) = room_id{ |
13424 | 130 |
if id != server.lobby_id { |
131 |
server.react(client_id, vec![ |
|
132 |
MoveToLobby(format!("quit: {}", msg.clone()))]); |
|
13421 | 133 |
} |
13529 | 134 |
} |
13421 | 135 |
|
13424 | 136 |
server.react(client_id, vec![ |
137 |
LobbyLeft(nick, msg.clone()).send_all().action(), |
|
138 |
Bye(msg).send_self().action(), |
|
13421 | 139 |
RemoveClient]); |
12149 | 140 |
}, |
141 |
RemoveClient => { |
|
13424 | 142 |
server.removed_clients.push(client_id); |
143 |
if server.clients.contains(client_id) { |
|
144 |
server.clients.remove(client_id); |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
145 |
} |
12149 | 146 |
}, |
147 |
ReactProtocolMessage(msg) => |
|
13424 | 148 |
handlers::handle(server, client_id, msg), |
12150 | 149 |
CheckRegistered => |
13424 | 150 |
if server.clients[client_id].protocol_number > 0 && server.clients[client_id].nick != "" { |
151 |
server.react(client_id, vec![ |
|
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
152 |
JoinLobby, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
153 |
]); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
154 |
}, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
155 |
JoinLobby => { |
13424 | 156 |
server.clients[client_id].room_id = Some(server.lobby_id); |
12152 | 157 |
|
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
158 |
let joined_msg; |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
159 |
{ |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
160 |
let mut lobby_nicks = Vec::new(); |
12857 | 161 |
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
|
162 |
if c.room_id.is_some() { |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
163 |
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
|
164 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
165 |
} |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
166 |
joined_msg = LobbyJoined(lobby_nicks); |
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
167 |
} |
13424 | 168 |
let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]); |
13421 | 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()); |
|
13424 | 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(), |
|
12150 | 187 |
]); |
188 |
}, |
|
12152 | 189 |
AddRoom(name, password) => { |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
190 |
let room_id = server.add_room();; |
13424 | 191 |
let actions = { |
12153 | 192 |
let r = &mut server.rooms[room_id]; |
13424 | 193 |
let c = &mut server.clients[client_id]; |
13421 | 194 |
r.master_id = Some(c.id); |
12153 | 195 |
r.name = name; |
196 |
r.password = password; |
|
197 |
r.protocol_number = c.protocol_number; |
|
13421 | 198 |
|
13424 | 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); |
|
13421 | 205 |
}, |
206 |
RemoveRoom(room_id) => { |
|
13424 | 207 |
let actions = { |
13421 | 208 |
let r = &mut server.rooms[room_id]; |
13424 | 209 |
vec![RoomRemove(r.name.clone()).send_all() |
210 |
.with_protocol(r.protocol_number).action()] |
|
211 |
}; |
|
13421 | 212 |
server.rooms.remove(room_id); |
13424 | 213 |
server.react(client_id, actions); |
13421 | 214 |
} |
215 |
MoveToRoom(room_id) => { |
|
13424 | 216 |
let actions = { |
13421 | 217 |
let r = &mut server.rooms[room_id]; |
13424 | 218 |
let c = &mut server.clients[client_id]; |
13421 | 219 |
r.players_number += 1; |
12153 | 220 |
c.room_id = Some(room_id); |
13525 | 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 { |
|
13421 | 228 |
r.ready_players_number += 1; |
229 |
} |
|
13424 | 230 |
|
13427 | 231 |
let mut v = vec![ |
232 |
RoomJoined(vec![c.nick.clone()]).send_all().in_room(room_id).action(), |
|
13432 | 233 |
ClientFlags("+i".to_string(), vec![c.nick.clone()]).send_all().action(), |
13427 | 234 |
SendRoomUpdate(None)]; |
13482 | 235 |
if !r.greeting.is_empty() { |
236 |
v.push(ChatMsg {nick: "[greeting]".to_string(), msg: r.greeting.clone()} |
|
237 |
.send_self().action()); |
|
238 |
} |
|
13525 | 239 |
if !c.is_master() { |
13432 | 240 |
let team_names: Vec<_>; |
241 |
if let Some(ref mut info) = r.game_info { |
|
13525 | 242 |
c.set_is_in_game(true); |
243 |
c.set_is_joined_mid_game(true); |
|
13432 | 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 |
||
13431 | 264 |
v.push(SendRoomData{ to: client_id, teams: true, config: true, flags: true}); |
13432 | 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( |
|
13433 | 271 |
vec![to_engine_msg("e$spectate 1".bytes())]) |
272 |
.send_self().action()); |
|
273 |
v.push(ForwardEngineMessage(info.msg_log.clone()) |
|
13432 | 274 |
.send_self().action()); |
275 |
||
13529 | 276 |
for name in &team_names { |
13432 | 277 |
v.push(ForwardEngineMessage( |
13433 | 278 |
vec![to_engine_msg(once(b'G').chain(name.bytes()))]) |
13432 | 279 |
.send_all().in_room(r.id).action()); |
280 |
} |
|
281 |
if info.is_paused { |
|
13433 | 282 |
v.push(ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
13432 | 283 |
.send_all().in_room(r.id).action()) |
284 |
} |
|
285 |
} |
|
13427 | 286 |
} |
287 |
v |
|
13424 | 288 |
}; |
289 |
server.react(client_id, actions); |
|
13429 | 290 |
} |
13431 | 291 |
SendRoomData {to, teams, config, flags} => { |
13429 | 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()) |
|
13431 | 297 |
.send(to).action()); |
13529 | 298 |
for cfg in r.game_config() { |
13444 | 299 |
actions.push(cfg.to_server_msg().send(to).action()); |
13429 | 300 |
} |
301 |
} |
|
302 |
if teams { |
|
13432 | 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() { |
|
13429 | 308 |
actions.push(TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team)) |
13431 | 309 |
.send(to).action()); |
13432 | 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()); |
|
13429 | 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()]) |
|
13431 | 319 |
.send(to).action()); |
13429 | 320 |
} |
321 |
let nicks: Vec<_> = server.clients.iter() |
|
13525 | 322 |
.filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready()) |
13429 | 323 |
.map(|(_, c)| c.nick.clone()).collect(); |
324 |
if !nicks.is_empty() { |
|
325 |
actions.push(ClientFlags("+r".to_string(), nicks) |
|
13431 | 326 |
.send(to).action()); |
13429 | 327 |
} |
328 |
} |
|
329 |
} |
|
330 |
server.react(client_id, actions); |
|
331 |
} |
|
13483 | 332 |
AddVote{vote, is_forced} => { |
333 |
let mut actions = Vec::new(); |
|
13532 | 334 |
if let Some(r) = server.room(client_id) { |
13483 | 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() { |
|
13532 | 338 |
actions.push(server_chat("Your vote has been counted.".to_string()) |
339 |
.send_self().action()); |
|
13483 | 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 { |
|
13532 | 351 |
actions.push(server_chat("You already have voted.".to_string()) |
352 |
.send_self().action()); |
|
13483 | 353 |
} |
354 |
} else { |
|
13532 | 355 |
actions.push(server_chat("There's no voting going on.".to_string()) |
356 |
.send_self().action()); |
|
13483 | 357 |
} |
358 |
||
359 |
if let Some(res) = result { |
|
13532 | 360 |
actions.push(server_chat("Voting closed.".to_string()) |
13483 | 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 |
}, |
|
13532 | 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 |
} |
|
13483 | 398 |
}, |
399 |
VoteType::Pause => { |
|
13485 | 400 |
if let Some(ref mut info) = server.rooms[room_id].game_info { |
13483 | 401 |
info.is_paused = !info.is_paused; |
13532 | 402 |
actions.push(server_chat("Pause toggled.".to_string()) |
13483 | 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 => { |
|
13485 | 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); |
|
13483 | 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 |
} |
|
13421 | 424 |
MoveToLobby(msg) => { |
425 |
let mut actions = Vec::new(); |
|
426 |
let lobby_id = server.lobby_id; |
|
13424 | 427 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13421 | 428 |
r.players_number -= 1; |
13525 | 429 |
if c.is_ready() && r.ready_players_number > 0 { |
13421 | 430 |
r.ready_players_number -= 1; |
431 |
} |
|
13528 | 432 |
if c.is_master() && (r.players_number > 0 || r.is_fixed()) { |
13421 | 433 |
actions.push(ChangeMaster(r.id, None)); |
434 |
} |
|
13424 | 435 |
actions.push(ClientFlags("-i".to_string(), vec![c.nick.clone()]) |
436 |
.send_all().action()); |
|
13421 | 437 |
} |
13424 | 438 |
server.react(client_id, actions); |
13421 | 439 |
actions = Vec::new(); |
440 |
||
13424 | 441 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13421 | 442 |
c.room_id = Some(lobby_id); |
13528 | 443 |
if r.players_number == 0 && !r.is_fixed() { |
13421 | 444 |
actions.push(RemoveRoom(r.id)); |
13482 | 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()))); |
|
13421 | 450 |
} |
451 |
} |
|
13424 | 452 |
server.react(client_id, actions) |
13421 | 453 |
} |
454 |
ChangeMaster(room_id, new_id) => { |
|
455 |
let mut actions = Vec::new(); |
|
456 |
let room_client_ids = server.room_clients(room_id); |
|
13528 | 457 |
let new_id = if server.room(client_id).map(|r| r.is_fixed()).unwrap_or(false) { |
13482 | 458 |
new_id |
459 |
} else { |
|
460 |
new_id.or_else(|| |
|
13529 | 461 |
room_client_ids.iter().find(|id| **id != client_id).cloned()) |
13482 | 462 |
}; |
13421 | 463 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
464 |
||
13424 | 465 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
466 |
match r.master_id { |
|
467 |
Some(id) if id == c.id => { |
|
13525 | 468 |
c.set_is_master(false); |
13424 | 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 => {} |
|
13421 | 475 |
} |
476 |
r.master_id = new_id; |
|
13528 | 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); |
|
13421 | 481 |
if let Some(nick) = new_nick { |
13424 | 482 |
actions.push(ClientFlags("+h".to_string(), vec![nick]) |
483 |
.send_all().in_room(r.id).action()); |
|
13421 | 484 |
} |
485 |
} |
|
13529 | 486 |
if let Some(id) = new_id { server.clients[id].set_is_master(true) } |
13424 | 487 |
server.react(client_id, actions); |
488 |
} |
|
489 |
RemoveTeam(name) => { |
|
13431 | 490 |
let mut actions = Vec::new(); |
491 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13424 | 492 |
r.remove_team(&name); |
13431 | 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)); |
|
13525 | 498 |
if r.game_info.is_some() && c.is_in_game() { |
13431 | 499 |
actions.push(SendTeamRemovalMessage(name)); |
500 |
} |
|
501 |
} |
|
13424 | 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); |
|
13421 | 511 |
} |
512 |
SendRoomUpdate(old_name) => { |
|
13424 | 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); |
|
13428 | 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 { |
|
13432 | 533 |
room.start_round(); |
13428 | 534 |
for id in room_clients { |
535 |
let c = &mut server.clients[id]; |
|
13525 | 536 |
c.set_is_in_game(false); |
13428 | 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); |
|
13421 | 546 |
} |
13428 | 547 |
SendTeamRemovalMessage(team_name) => { |
548 |
let mut actions = Vec::new(); |
|
13532 | 549 |
if let Some(r) = server.room(client_id) { |
13428 | 550 |
if let Some(ref mut info) = r.game_info { |
551 |
let msg = once(b'F').chain(team_name.bytes()); |
|
13433 | 552 |
actions.push(ForwardEngineMessage(vec![to_engine_msg(msg)]). |
13428 | 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 |
} |
|
13432 | 558 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
13448 | 559 |
if let Some(m) = &info.sync_msg { |
560 |
info.msg_log.push(m.clone()); |
|
13432 | 561 |
} |
13448 | 562 |
if info.sync_msg.is_some() { |
563 |
info.sync_msg = None |
|
564 |
} |
|
565 |
info.msg_log.push(remove_msg.clone()); |
|
13433 | 566 |
actions.push(ForwardEngineMessage(vec![remove_msg]) |
13432 | 567 |
.send_all().in_room(r.id).but_self().action()); |
13428 | 568 |
} |
569 |
} |
|
570 |
server.react(client_id, actions); |
|
571 |
} |
|
572 |
FinishRoomGame(room_id) => { |
|
13431 | 573 |
let mut actions = Vec::new(); |
13483 | 574 |
let old_info; |
13431 | 575 |
{ |
13428 | 576 |
let r = &mut server.rooms[room_id]; |
13483 | 577 |
old_info = replace(&mut r.game_info, None); |
13428 | 578 |
r.game_info = None; |
13431 | 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() { |
|
13525 | 586 |
if c.room_id == Some(room_id) && c.is_joined_mid_game() { |
13431 | 587 |
actions.push(SendRoomData{ |
588 |
to: c.id, teams: false, |
|
589 |
config: true, flags: false}); |
|
13529 | 590 |
for name in &info.left_teams { |
13431 | 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)| { |
|
13525 | 601 |
let is_master = c.is_master(); |
602 |
c.set_is_ready(is_master); |
|
603 |
c.set_is_joined_mid_game(false); |
|
13431 | 604 |
c |
13525 | 605 |
}).filter_map(|c| if !c.is_master() { |
13431 | 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 |
} |
|
13428 | 614 |
server.react(client_id, actions); |
615 |
} |
|
12152 | 616 |
Warn(msg) => { |
13424 | 617 |
run_action(server, client_id, Warning(msg).send_self().action()); |
12152 | 618 |
} |
13421 | 619 |
ProtocolError(msg) => { |
13424 | 620 |
run_action(server, client_id, Error(msg).send_self().action()) |
13421 | 621 |
} |
12149 | 622 |
} |
623 |
} |