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