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