author | alfadur |
Sat, 07 Jul 2018 20:22:31 +0300 | |
changeset 13478 | d79795acaa73 |
parent 13477 | f748a72432f2 |
child 13480 | fb37745c5bca |
permissions | -rw-r--r-- |
13416 | 1 |
use std::{ |
13423 | 2 |
io, io::Write, |
13426 | 3 |
iter::once, |
13478 | 4 |
mem::replace |
13416 | 5 |
}; |
6 |
use super::{ |
|
7 |
server::HWServer, |
|
13478 | 8 |
room::{GameInfo}, |
9 |
client::HWClient, |
|
10 |
coretypes::{ClientId, RoomId, 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; |
12138 | 21 |
|
13419 | 22 |
pub enum Destination { |
13426 | 23 |
ToId(ClientId), |
13419 | 24 |
ToSelf, |
13423 | 25 |
ToAll { |
13419 | 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 { |
|
13426 | 38 |
pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage { |
39 |
PendingMessage{ destination: Destination::ToId(client_id), message} |
|
40 |
} |
|
41 |
||
13419 | 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 { |
|
13426 | 84 |
pub fn send(self, client_id: ClientId) -> PendingMessage { PendingMessage::send(self, client_id) } |
13419 | 85 |
pub fn send_self(self) -> PendingMessage { PendingMessage::send_self(self) } |
86 |
pub fn send_all(self) -> PendingMessage { PendingMessage::send_all(self) } |
|
87 |
} |
|
88 |
||
12138 | 89 |
pub enum Action { |
13419 | 90 |
Send(PendingMessage), |
12139 | 91 |
RemoveClient, |
92 |
ByeClient(String), |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
93 |
ReactProtocolMessage(HWProtocolMessage), |
12145 | 94 |
CheckRegistered, |
95 |
JoinLobby, |
|
12147 | 96 |
AddRoom(String, Option<String>), |
13416 | 97 |
RemoveRoom(RoomId), |
98 |
MoveToRoom(RoomId), |
|
99 |
MoveToLobby(String), |
|
100 |
ChangeMaster(RoomId, Option<ClientId>), |
|
13419 | 101 |
RemoveTeam(String), |
102 |
RemoveClientTeams, |
|
13416 | 103 |
SendRoomUpdate(Option<String>), |
13423 | 104 |
StartRoomGame(RoomId), |
105 |
SendTeamRemovalMessage(String), |
|
106 |
FinishRoomGame(RoomId), |
|
13426 | 107 |
SendRoomData{to: ClientId, teams: bool, config: bool, flags: bool}, |
13478 | 108 |
AddVote{vote: bool, is_forced: bool}, |
109 |
ApplyVoting(VoteType, RoomId), |
|
12147 | 110 |
Warn(String), |
13416 | 111 |
ProtocolError(String) |
12138 | 112 |
} |
12144 | 113 |
|
114 |
use self::Action::*; |
|
115 |
||
13419 | 116 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12144 | 117 |
match action { |
13419 | 118 |
Send(msg) => server.send(client_id, msg.destination, msg.message), |
12144 | 119 |
ByeClient(msg) => { |
13416 | 120 |
let room_id; |
121 |
let nick; |
|
122 |
{ |
|
13419 | 123 |
let c = &server.clients[client_id]; |
13416 | 124 |
room_id = c.room_id; |
125 |
nick = c.nick.clone(); |
|
126 |
} |
|
127 |
||
13419 | 128 |
room_id.map (|id| { |
129 |
if id != server.lobby_id { |
|
130 |
server.react(client_id, vec![ |
|
131 |
MoveToLobby(format!("quit: {}", msg.clone()))]); |
|
13416 | 132 |
} |
133 |
}); |
|
134 |
||
13419 | 135 |
server.react(client_id, vec![ |
136 |
LobbyLeft(nick, msg.clone()).send_all().action(), |
|
137 |
Bye(msg).send_self().action(), |
|
13416 | 138 |
RemoveClient]); |
12144 | 139 |
}, |
140 |
RemoveClient => { |
|
13419 | 141 |
server.removed_clients.push(client_id); |
142 |
if server.clients.contains(client_id) { |
|
143 |
server.clients.remove(client_id); |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
144 |
} |
12144 | 145 |
}, |
146 |
ReactProtocolMessage(msg) => |
|
13419 | 147 |
handlers::handle(server, client_id, msg), |
12145 | 148 |
CheckRegistered => |
13419 | 149 |
if server.clients[client_id].protocol_number > 0 && server.clients[client_id].nick != "" { |
150 |
server.react(client_id, vec![ |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
151 |
JoinLobby, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
152 |
]); |
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 |
JoinLobby => { |
13419 | 155 |
server.clients[client_id].room_id = Some(server.lobby_id); |
12147 | 156 |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
157 |
let joined_msg; |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
158 |
{ |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
159 |
let mut lobby_nicks = Vec::new(); |
12852 | 160 |
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
|
161 |
if c.room_id.is_some() { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
162 |
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
|
163 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
164 |
} |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
165 |
joined_msg = LobbyJoined(lobby_nicks); |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
166 |
} |
13419 | 167 |
let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]); |
13416 | 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()); |
|
13419 | 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(), |
|
12145 | 186 |
]); |
187 |
}, |
|
12147 | 188 |
AddRoom(name, password) => { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
189 |
let room_id = server.add_room();; |
13419 | 190 |
let actions = { |
12148 | 191 |
let r = &mut server.rooms[room_id]; |
13419 | 192 |
let c = &mut server.clients[client_id]; |
13416 | 193 |
r.master_id = Some(c.id); |
12148 | 194 |
r.name = name; |
195 |
r.password = password; |
|
196 |
r.protocol_number = c.protocol_number; |
|
13416 | 197 |
|
13419 | 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); |
|
13416 | 204 |
}, |
205 |
RemoveRoom(room_id) => { |
|
13419 | 206 |
let actions = { |
13416 | 207 |
let r = &mut server.rooms[room_id]; |
13419 | 208 |
vec![RoomRemove(r.name.clone()).send_all() |
209 |
.with_protocol(r.protocol_number).action()] |
|
210 |
}; |
|
13416 | 211 |
server.rooms.remove(room_id); |
13419 | 212 |
server.react(client_id, actions); |
13416 | 213 |
} |
214 |
MoveToRoom(room_id) => { |
|
13419 | 215 |
let actions = { |
13416 | 216 |
let r = &mut server.rooms[room_id]; |
13419 | 217 |
let c = &mut server.clients[client_id]; |
13416 | 218 |
r.players_number += 1; |
12148 | 219 |
c.room_id = Some(room_id); |
13416 | 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 |
} |
|
13419 | 229 |
|
13422 | 230 |
let mut v = vec![ |
231 |
RoomJoined(vec![c.nick.clone()]).send_all().in_room(room_id).action(), |
|
13427 | 232 |
ClientFlags("+i".to_string(), vec![c.nick.clone()]).send_all().action(), |
13422 | 233 |
SendRoomUpdate(None)]; |
13477 | 234 |
if !r.greeting.is_empty() { |
235 |
v.push(ChatMsg {nick: "[greeting]".to_string(), msg: r.greeting.clone()} |
|
236 |
.send_self().action()); |
|
237 |
} |
|
13422 | 238 |
if !c.is_master { |
13427 | 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 |
||
13426 | 263 |
v.push(SendRoomData{ to: client_id, teams: true, config: true, flags: true}); |
13427 | 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( |
|
13428 | 270 |
vec![to_engine_msg("e$spectate 1".bytes())]) |
271 |
.send_self().action()); |
|
272 |
v.push(ForwardEngineMessage(info.msg_log.clone()) |
|
13427 | 273 |
.send_self().action()); |
274 |
||
275 |
for name in team_names.iter() { |
|
276 |
v.push(ForwardEngineMessage( |
|
13428 | 277 |
vec![to_engine_msg(once(b'G').chain(name.bytes()))]) |
13427 | 278 |
.send_all().in_room(r.id).action()); |
279 |
} |
|
280 |
if info.is_paused { |
|
13428 | 281 |
v.push(ForwardEngineMessage(vec![to_engine_msg(once(b'I'))]) |
13427 | 282 |
.send_all().in_room(r.id).action()) |
283 |
} |
|
284 |
} |
|
13422 | 285 |
} |
286 |
v |
|
13419 | 287 |
}; |
288 |
server.react(client_id, actions); |
|
13424 | 289 |
} |
13426 | 290 |
SendRoomData {to, teams, config, flags} => { |
13424 | 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()) |
|
13426 | 296 |
.send(to).action()); |
13424 | 297 |
for cfg in r.game_config().into_iter() { |
13439 | 298 |
actions.push(cfg.to_server_msg().send(to).action()); |
13424 | 299 |
} |
300 |
} |
|
301 |
if teams { |
|
13427 | 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() { |
|
13424 | 307 |
actions.push(TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team)) |
13426 | 308 |
.send(to).action()); |
13427 | 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()); |
|
13424 | 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()]) |
|
13426 | 318 |
.send(to).action()); |
13424 | 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) |
|
13426 | 325 |
.send(to).action()); |
13424 | 326 |
} |
327 |
} |
|
328 |
} |
|
329 |
server.react(client_id, actions); |
|
330 |
} |
|
13478 | 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 |
} |
|
13416 | 405 |
MoveToLobby(msg) => { |
406 |
let mut actions = Vec::new(); |
|
407 |
let lobby_id = server.lobby_id; |
|
13419 | 408 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13416 | 409 |
r.players_number -= 1; |
13424 | 410 |
if c.is_ready && r.ready_players_number > 0 { |
13416 | 411 |
r.ready_players_number -= 1; |
412 |
} |
|
13477 | 413 |
if c.is_master && (r.players_number > 0 || r.is_fixed) { |
13416 | 414 |
actions.push(ChangeMaster(r.id, None)); |
415 |
} |
|
13419 | 416 |
actions.push(ClientFlags("-i".to_string(), vec![c.nick.clone()]) |
417 |
.send_all().action()); |
|
13416 | 418 |
} |
13419 | 419 |
server.react(client_id, actions); |
13416 | 420 |
actions = Vec::new(); |
421 |
||
13419 | 422 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13416 | 423 |
c.room_id = Some(lobby_id); |
13477 | 424 |
if r.players_number == 0 && !r.is_fixed { |
13416 | 425 |
actions.push(RemoveRoom(r.id)); |
13477 | 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()))); |
|
13416 | 431 |
} |
432 |
} |
|
13419 | 433 |
server.react(client_id, actions) |
13416 | 434 |
} |
435 |
ChangeMaster(room_id, new_id) => { |
|
436 |
let mut actions = Vec::new(); |
|
437 |
let room_client_ids = server.room_clients(room_id); |
|
13477 | 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 |
}; |
|
13416 | 444 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
445 |
||
13419 | 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 => {} |
|
13416 | 456 |
} |
457 |
r.master_id = new_id; |
|
458 |
if let Some(nick) = new_nick { |
|
13419 | 459 |
actions.push(ClientFlags("+h".to_string(), vec![nick]) |
460 |
.send_all().in_room(r.id).action()); |
|
13416 | 461 |
} |
462 |
} |
|
463 |
new_id.map(|id| server.clients[id].is_master = true); |
|
13419 | 464 |
server.react(client_id, actions); |
465 |
} |
|
466 |
RemoveTeam(name) => { |
|
13426 | 467 |
let mut actions = Vec::new(); |
468 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13419 | 469 |
r.remove_team(&name); |
13426 | 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 |
} |
|
13419 | 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); |
|
13416 | 488 |
} |
489 |
SendRoomUpdate(old_name) => { |
|
13419 | 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); |
|
13423 | 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 { |
|
13427 | 510 |
room.start_round(); |
13423 | 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); |
|
13416 | 523 |
} |
13423 | 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()); |
|
13428 | 529 |
actions.push(ForwardEngineMessage(vec![to_engine_msg(msg)]). |
13423 | 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 |
} |
|
13427 | 535 |
let remove_msg = to_engine_msg(once(b'F').chain(team_name.bytes())); |
13443 | 536 |
if let Some(m) = &info.sync_msg { |
537 |
info.msg_log.push(m.clone()); |
|
13427 | 538 |
} |
13443 | 539 |
if info.sync_msg.is_some() { |
540 |
info.sync_msg = None |
|
541 |
} |
|
542 |
info.msg_log.push(remove_msg.clone()); |
|
13428 | 543 |
actions.push(ForwardEngineMessage(vec![remove_msg]) |
13427 | 544 |
.send_all().in_room(r.id).but_self().action()); |
13423 | 545 |
} |
546 |
} |
|
547 |
server.react(client_id, actions); |
|
548 |
} |
|
549 |
FinishRoomGame(room_id) => { |
|
13426 | 550 |
let mut actions = Vec::new(); |
13478 | 551 |
let old_info; |
13426 | 552 |
{ |
13423 | 553 |
let r = &mut server.rooms[room_id]; |
13478 | 554 |
old_info = replace(&mut r.game_info, None); |
13423 | 555 |
r.game_info = None; |
13426 | 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 |
} |
|
13423 | 590 |
server.react(client_id, actions); |
591 |
} |
|
12147 | 592 |
Warn(msg) => { |
13419 | 593 |
run_action(server, client_id, Warning(msg).send_self().action()); |
12147 | 594 |
} |
13416 | 595 |
ProtocolError(msg) => { |
13419 | 596 |
run_action(server, client_id, Error(msg).send_self().action()) |
13416 | 597 |
} |
12144 | 598 |
} |
599 |
} |