author | alfadur |
Wed, 27 Jun 2018 04:54:41 +0300 | |
changeset 13424 | d8354cb98b98 |
parent 13423 | 87a6cad20c90 |
child 13426 | f091f69d59e4 |
permissions | -rw-r--r-- |
13416 | 1 |
use std::{ |
13423 | 2 |
io, io::Write, |
3 |
iter::once |
|
13416 | 4 |
}; |
5 |
use super::{ |
|
6 |
server::HWServer, |
|
13423 | 7 |
room::{RoomId, GameInfo}, |
13419 | 8 |
client::{ClientId, HWClient}, |
13416 | 9 |
room::HWRoom, |
10 |
handlers |
|
11 |
}; |
|
12 |
use protocol::messages::{ |
|
13 |
HWProtocolMessage, |
|
14 |
HWServerMessage, |
|
15 |
HWServerMessage::* |
|
16 |
}; |
|
13423 | 17 |
use utils::to_engine_msg; |
12138 | 18 |
|
13419 | 19 |
pub enum Destination { |
20 |
ToSelf, |
|
13423 | 21 |
ToAll { |
13419 | 22 |
room_id: Option<RoomId>, |
23 |
protocol: Option<u32>, |
|
24 |
skip_self: bool |
|
25 |
} |
|
26 |
} |
|
27 |
||
28 |
pub struct PendingMessage { |
|
29 |
pub destination: Destination, |
|
30 |
pub message: HWServerMessage |
|
31 |
} |
|
32 |
||
33 |
impl PendingMessage { |
|
34 |
pub fn send_self(message: HWServerMessage) -> PendingMessage { |
|
35 |
PendingMessage{ destination: Destination::ToSelf, message } |
|
36 |
} |
|
37 |
||
38 |
pub fn send_all(message: HWServerMessage) -> PendingMessage { |
|
39 |
let destination = Destination::ToAll { |
|
40 |
room_id: None, |
|
41 |
protocol: None, |
|
42 |
skip_self: false, |
|
43 |
}; |
|
44 |
PendingMessage{ destination, message } |
|
45 |
} |
|
46 |
||
47 |
pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage { |
|
48 |
if let Destination::ToAll {ref mut room_id, ..} = self.destination { |
|
49 |
*room_id = Some(clients_room_id) |
|
50 |
} |
|
51 |
self |
|
52 |
} |
|
53 |
||
54 |
pub fn with_protocol(mut self, protocol_number: u32) -> PendingMessage { |
|
55 |
if let Destination::ToAll {ref mut protocol, ..} = self.destination { |
|
56 |
*protocol = Some(protocol_number) |
|
57 |
} |
|
58 |
self |
|
59 |
} |
|
60 |
||
61 |
pub fn but_self(mut self) -> PendingMessage { |
|
62 |
if let Destination::ToAll {ref mut skip_self, ..} = self.destination { |
|
63 |
*skip_self = true |
|
64 |
} |
|
65 |
self |
|
66 |
} |
|
67 |
||
68 |
pub fn action(self) -> Action { Send(self) } |
|
69 |
} |
|
70 |
||
71 |
impl Into<Action> for PendingMessage { |
|
72 |
fn into(self) -> Action { self.action() } |
|
73 |
} |
|
74 |
||
75 |
impl HWServerMessage { |
|
76 |
pub fn send_self(self) -> PendingMessage { PendingMessage::send_self(self) } |
|
77 |
pub fn send_all(self) -> PendingMessage { PendingMessage::send_all(self) } |
|
78 |
} |
|
79 |
||
12138 | 80 |
pub enum Action { |
13419 | 81 |
Send(PendingMessage), |
12139 | 82 |
RemoveClient, |
83 |
ByeClient(String), |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
84 |
ReactProtocolMessage(HWProtocolMessage), |
12145 | 85 |
CheckRegistered, |
86 |
JoinLobby, |
|
12147 | 87 |
AddRoom(String, Option<String>), |
13416 | 88 |
RemoveRoom(RoomId), |
89 |
MoveToRoom(RoomId), |
|
90 |
MoveToLobby(String), |
|
91 |
ChangeMaster(RoomId, Option<ClientId>), |
|
13419 | 92 |
RemoveTeam(String), |
93 |
RemoveClientTeams, |
|
13416 | 94 |
SendRoomUpdate(Option<String>), |
13423 | 95 |
StartRoomGame(RoomId), |
96 |
SendTeamRemovalMessage(String), |
|
97 |
FinishRoomGame(RoomId), |
|
13424 | 98 |
SendRoomData{teams: bool, config: bool, flags: bool}, |
12147 | 99 |
Warn(String), |
13416 | 100 |
ProtocolError(String) |
12138 | 101 |
} |
12144 | 102 |
|
103 |
use self::Action::*; |
|
104 |
||
13419 | 105 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12144 | 106 |
match action { |
13419 | 107 |
Send(msg) => server.send(client_id, msg.destination, msg.message), |
12144 | 108 |
ByeClient(msg) => { |
13416 | 109 |
let room_id; |
110 |
let nick; |
|
111 |
{ |
|
13419 | 112 |
let c = &server.clients[client_id]; |
13416 | 113 |
room_id = c.room_id; |
114 |
nick = c.nick.clone(); |
|
115 |
} |
|
116 |
||
13419 | 117 |
room_id.map (|id| { |
118 |
if id != server.lobby_id { |
|
119 |
server.react(client_id, vec![ |
|
120 |
MoveToLobby(format!("quit: {}", msg.clone()))]); |
|
13416 | 121 |
} |
122 |
}); |
|
123 |
||
13419 | 124 |
server.react(client_id, vec![ |
125 |
LobbyLeft(nick, msg.clone()).send_all().action(), |
|
126 |
Bye(msg).send_self().action(), |
|
13416 | 127 |
RemoveClient]); |
12144 | 128 |
}, |
129 |
RemoveClient => { |
|
13419 | 130 |
server.removed_clients.push(client_id); |
131 |
if server.clients.contains(client_id) { |
|
132 |
server.clients.remove(client_id); |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
133 |
} |
12144 | 134 |
}, |
135 |
ReactProtocolMessage(msg) => |
|
13419 | 136 |
handlers::handle(server, client_id, msg), |
12145 | 137 |
CheckRegistered => |
13419 | 138 |
if server.clients[client_id].protocol_number > 0 && server.clients[client_id].nick != "" { |
139 |
server.react(client_id, vec![ |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
140 |
JoinLobby, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
141 |
]); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
142 |
}, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
143 |
JoinLobby => { |
13419 | 144 |
server.clients[client_id].room_id = Some(server.lobby_id); |
12147 | 145 |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
146 |
let joined_msg; |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
147 |
{ |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
148 |
let mut lobby_nicks = Vec::new(); |
12852 | 149 |
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
|
150 |
if c.room_id.is_some() { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
151 |
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
|
152 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
153 |
} |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
154 |
joined_msg = LobbyJoined(lobby_nicks); |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
155 |
} |
13419 | 156 |
let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]); |
13416 | 157 |
let flags_msg = ClientFlags( |
158 |
"+i".to_string(), |
|
159 |
server.clients.iter() |
|
160 |
.filter(|(_, c)| c.room_id.is_some()) |
|
161 |
.map(|(_, c)| c.nick.clone()) |
|
162 |
.collect()); |
|
163 |
let server_msg = ServerMessage("\u{1f994} is watching".to_string()); |
|
164 |
let rooms_msg = Rooms(server.rooms.iter() |
|
165 |
.filter(|(id, _)| *id != server.lobby_id) |
|
166 |
.flat_map(|(_, r)| |
|
167 |
r.info(r.master_id.map(|id| &server.clients[id]))) |
|
168 |
.collect()); |
|
13419 | 169 |
server.react(client_id, vec![ |
170 |
everyone_msg.send_all().but_self().action(), |
|
171 |
joined_msg.send_self().action(), |
|
172 |
flags_msg.send_self().action(), |
|
173 |
server_msg.send_self().action(), |
|
174 |
rooms_msg.send_self().action(), |
|
12145 | 175 |
]); |
176 |
}, |
|
12147 | 177 |
AddRoom(name, password) => { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
178 |
let room_id = server.add_room();; |
13419 | 179 |
let actions = { |
12148 | 180 |
let r = &mut server.rooms[room_id]; |
13419 | 181 |
let c = &mut server.clients[client_id]; |
13416 | 182 |
r.master_id = Some(c.id); |
12148 | 183 |
r.name = name; |
184 |
r.password = password; |
|
185 |
r.protocol_number = c.protocol_number; |
|
13416 | 186 |
|
13419 | 187 |
vec![ |
188 |
RoomAdd(r.info(Some(&c))).send_all() |
|
189 |
.with_protocol(r.protocol_number).action(), |
|
190 |
MoveToRoom(room_id)] |
|
191 |
}; |
|
192 |
server.react(client_id, actions); |
|
13416 | 193 |
}, |
194 |
RemoveRoom(room_id) => { |
|
13419 | 195 |
let actions = { |
13416 | 196 |
let r = &mut server.rooms[room_id]; |
13419 | 197 |
vec![RoomRemove(r.name.clone()).send_all() |
198 |
.with_protocol(r.protocol_number).action()] |
|
199 |
}; |
|
13416 | 200 |
server.rooms.remove(room_id); |
13419 | 201 |
server.react(client_id, actions); |
13416 | 202 |
} |
203 |
MoveToRoom(room_id) => { |
|
13419 | 204 |
let actions = { |
13416 | 205 |
let r = &mut server.rooms[room_id]; |
13419 | 206 |
let c = &mut server.clients[client_id]; |
13416 | 207 |
r.players_number += 1; |
12148 | 208 |
c.room_id = Some(room_id); |
13416 | 209 |
c.is_joined_mid_game = false; |
210 |
if r.master_id == Some(c.id) { |
|
211 |
r.ready_players_number += 1; |
|
212 |
c.is_master = true; |
|
213 |
c.is_ready = true; |
|
214 |
} else { |
|
215 |
c.is_ready = false; |
|
216 |
c.is_master = false; |
|
217 |
} |
|
13419 | 218 |
let flags_msg = ClientFlags("+i".to_string(), vec![c.nick.clone()]); |
219 |
||
13422 | 220 |
let mut v = vec![ |
221 |
RoomJoined(vec![c.nick.clone()]).send_all().in_room(room_id).action(), |
|
222 |
flags_msg.send_all().action(), |
|
223 |
SendRoomUpdate(None)]; |
|
224 |
if !c.is_master { |
|
13424 | 225 |
v.push(SendRoomData{ teams: true, config: true, flags: true}); |
13422 | 226 |
} |
227 |
v |
|
13419 | 228 |
}; |
229 |
server.react(client_id, actions); |
|
13424 | 230 |
} |
231 |
SendRoomData {teams, config, flags} => { |
|
232 |
let mut actions = Vec::new(); |
|
233 |
let room_id = server.clients[client_id].room_id; |
|
234 |
if let Some(r) = room_id.and_then(|id| server.rooms.get(id)) { |
|
235 |
if config { |
|
236 |
actions.push(ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config()) |
|
237 |
.send_self().action()); |
|
238 |
for cfg in r.game_config().into_iter() { |
|
239 |
actions.push(cfg.into_server_msg().send_self().action()); |
|
240 |
} |
|
241 |
} |
|
242 |
if teams { |
|
243 |
for (owner_id, team) in r.teams.iter() { |
|
244 |
actions.push(TeamAdd(HWRoom::team_info(&server.clients[*owner_id], &team)) |
|
245 |
.send_self().action()); |
|
246 |
} |
|
247 |
} |
|
248 |
if flags { |
|
249 |
if let Some(id) = r.master_id { |
|
250 |
actions.push(ClientFlags("+h".to_string(), vec![server.clients[id].nick.clone()]) |
|
251 |
.send_self().action()); |
|
252 |
} |
|
253 |
let nicks: Vec<_> = server.clients.iter() |
|
254 |
.filter(|(_, c)| c.room_id == Some(r.id) && c.is_ready) |
|
255 |
.map(|(_, c)| c.nick.clone()).collect(); |
|
256 |
if !nicks.is_empty() { |
|
257 |
actions.push(ClientFlags("+r".to_string(), nicks) |
|
258 |
.send_self().action()); |
|
259 |
} |
|
260 |
} |
|
261 |
} |
|
262 |
server.react(client_id, actions); |
|
263 |
} |
|
13416 | 264 |
MoveToLobby(msg) => { |
265 |
let mut actions = Vec::new(); |
|
266 |
let lobby_id = server.lobby_id; |
|
13419 | 267 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13416 | 268 |
r.players_number -= 1; |
13424 | 269 |
if c.is_ready && r.ready_players_number > 0 { |
13416 | 270 |
r.ready_players_number -= 1; |
271 |
} |
|
272 |
if r.players_number > 0 && c.is_master { |
|
273 |
actions.push(ChangeMaster(r.id, None)); |
|
274 |
} |
|
13419 | 275 |
actions.push(RemoveClientTeams); |
276 |
actions.push(RoomLeft(c.nick.clone(), msg) |
|
277 |
.send_all().in_room(r.id).but_self().action()); |
|
278 |
actions.push(ClientFlags("-i".to_string(), vec![c.nick.clone()]) |
|
279 |
.send_all().action()); |
|
13416 | 280 |
actions.push(SendRoomUpdate(Some(r.name.clone()))); |
281 |
} |
|
13419 | 282 |
server.react(client_id, actions); |
13416 | 283 |
actions = Vec::new(); |
284 |
||
13419 | 285 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13416 | 286 |
c.room_id = Some(lobby_id); |
287 |
if r.players_number == 0 { |
|
288 |
actions.push(RemoveRoom(r.id)); |
|
289 |
} |
|
290 |
} |
|
13419 | 291 |
server.react(client_id, actions) |
13416 | 292 |
} |
293 |
ChangeMaster(room_id, new_id) => { |
|
294 |
let mut actions = Vec::new(); |
|
295 |
let room_client_ids = server.room_clients(room_id); |
|
296 |
let new_id = new_id.or_else(|| |
|
13419 | 297 |
room_client_ids.iter().find(|id| **id != client_id).map(|id| *id)); |
13416 | 298 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
299 |
||
13419 | 300 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
301 |
match r.master_id { |
|
302 |
Some(id) if id == c.id => { |
|
303 |
c.is_master = false; |
|
304 |
r.master_id = None; |
|
305 |
actions.push(ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
|
306 |
.send_all().in_room(r.id).action()); |
|
307 |
} |
|
308 |
Some(_) => unreachable!(), |
|
309 |
None => {} |
|
13416 | 310 |
} |
311 |
r.master_id = new_id; |
|
312 |
if let Some(nick) = new_nick { |
|
13419 | 313 |
actions.push(ClientFlags("+h".to_string(), vec![nick]) |
314 |
.send_all().in_room(r.id).action()); |
|
13416 | 315 |
} |
316 |
} |
|
317 |
new_id.map(|id| server.clients[id].is_master = true); |
|
13419 | 318 |
server.react(client_id, actions); |
319 |
} |
|
320 |
RemoveTeam(name) => { |
|
13422 | 321 |
let actions = if let (_, Some(r)) = server.client_and_room(client_id) { |
13419 | 322 |
r.remove_team(&name); |
323 |
vec![TeamRemove(name).send_all().in_room(r.id).action(), |
|
324 |
SendRoomUpdate(None)] |
|
325 |
} else { |
|
326 |
Vec::new() |
|
327 |
}; |
|
328 |
server.react(client_id, actions); |
|
329 |
}, |
|
330 |
RemoveClientTeams => { |
|
331 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
332 |
r.client_teams(c.id).map(|t| RemoveTeam(t.name.clone())).collect() |
|
333 |
} else { |
|
334 |
Vec::new() |
|
335 |
}; |
|
336 |
server.react(client_id, actions); |
|
13416 | 337 |
} |
338 |
SendRoomUpdate(old_name) => { |
|
13419 | 339 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
340 |
let name = old_name.unwrap_or_else(|| r.name.clone()); |
|
341 |
vec![RoomUpdated(name, r.info(Some(&c))) |
|
342 |
.send_all().with_protocol(r.protocol_number).action()] |
|
343 |
} else { |
|
344 |
Vec::new() |
|
345 |
}; |
|
346 |
server.react(client_id, actions); |
|
13423 | 347 |
}, |
348 |
StartRoomGame(room_id) => { |
|
349 |
let actions = { |
|
350 |
let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server.clients.iter() |
|
351 |
.map(|(id, c)| (id, c.nick.clone())).unzip(); |
|
352 |
let room = &mut server.rooms[room_id]; |
|
353 |
||
354 |
if !room.has_multiple_clans() { |
|
355 |
vec![Warn("The game can't be started with less than two clans!".to_string())] |
|
356 |
} else if room.game_info.is_some() { |
|
357 |
vec![Warn("The game is already in progress".to_string())] |
|
358 |
} else { |
|
359 |
room.game_info = Some(GameInfo { |
|
360 |
teams_in_game: room.teams.len() as u8 |
|
361 |
}); |
|
362 |
for id in room_clients { |
|
363 |
let c = &mut server.clients[id]; |
|
364 |
c.is_in_game = true; |
|
365 |
c.team_indices = room.client_team_indices(c.id); |
|
366 |
} |
|
367 |
vec![RunGame.send_all().in_room(room.id).action(), |
|
368 |
SendRoomUpdate(None), |
|
369 |
ClientFlags("+g".to_string(), room_nicks) |
|
370 |
.send_all().in_room(room.id).action()] |
|
371 |
} |
|
372 |
}; |
|
373 |
server.react(client_id, actions); |
|
13416 | 374 |
} |
13423 | 375 |
SendTeamRemovalMessage(team_name) => { |
376 |
let mut actions = Vec::new(); |
|
377 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
378 |
if let Some(ref mut info) = r.game_info { |
|
379 |
let msg = once(b'F').chain(team_name.bytes()); |
|
380 |
actions.push(ForwardEngineMessage(to_engine_msg(msg)). |
|
381 |
send_all().in_room(r.id).but_self().action()); |
|
382 |
info.teams_in_game -= 1; |
|
383 |
if info.teams_in_game == 0 { |
|
384 |
actions.push(FinishRoomGame(r.id)); |
|
385 |
} |
|
386 |
} |
|
387 |
} |
|
388 |
server.react(client_id, actions); |
|
389 |
} |
|
390 |
FinishRoomGame(room_id) => { |
|
391 |
let actions = { |
|
392 |
let r = &mut server.rooms[room_id]; |
|
393 |
r.game_info = None; |
|
394 |
r.ready_players_number = 0; |
|
395 |
vec![SendRoomUpdate(None), |
|
396 |
RoundFinished.send_all().in_room(r.id).action()] |
|
397 |
}; |
|
398 |
server.react(client_id, actions); |
|
399 |
} |
|
12147 | 400 |
Warn(msg) => { |
13419 | 401 |
run_action(server, client_id, Warning(msg).send_self().action()); |
12147 | 402 |
} |
13416 | 403 |
ProtocolError(msg) => { |
13419 | 404 |
run_action(server, client_id, Error(msg).send_self().action()) |
13416 | 405 |
} |
12144 | 406 |
} |
407 |
} |