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