author | alfadur |
Wed, 27 Jun 2018 17:58:33 +0300 | |
changeset 13431 | f091f69d59e4 |
parent 13429 | d8354cb98b98 |
child 13432 | 6f6a866c86a2 |
permissions | -rw-r--r-- |
13421 | 1 |
use std::{ |
13428 | 2 |
io, io::Write, |
13431 | 3 |
iter::once, |
4 |
mem::swap |
|
13421 | 5 |
}; |
6 |
use super::{ |
|
7 |
server::HWServer, |
|
13428 | 8 |
room::{RoomId, GameInfo}, |
13424 | 9 |
client::{ClientId, HWClient}, |
13421 | 10 |
room::HWRoom, |
11 |
handlers |
|
12 |
}; |
|
13 |
use protocol::messages::{ |
|
14 |
HWProtocolMessage, |
|
15 |
HWServerMessage, |
|
16 |
HWServerMessage::* |
|
17 |
}; |
|
13428 | 18 |
use utils::to_engine_msg; |
12143 | 19 |
|
13424 | 20 |
pub enum Destination { |
13431 | 21 |
ToId(ClientId), |
13424 | 22 |
ToSelf, |
13428 | 23 |
ToAll { |
13424 | 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 { |
|
13431 | 36 |
pub fn send(message: HWServerMessage, client_id: ClientId) -> PendingMessage { |
37 |
PendingMessage{ destination: Destination::ToId(client_id), message} |
|
38 |
} |
|
39 |
||
13424 | 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 { |
|
13431 | 82 |
pub fn send(self, client_id: ClientId) -> PendingMessage { PendingMessage::send(self, client_id) } |
13424 | 83 |
pub fn send_self(self) -> PendingMessage { PendingMessage::send_self(self) } |
84 |
pub fn send_all(self) -> PendingMessage { PendingMessage::send_all(self) } |
|
85 |
} |
|
86 |
||
12143 | 87 |
pub enum Action { |
13424 | 88 |
Send(PendingMessage), |
12144 | 89 |
RemoveClient, |
90 |
ByeClient(String), |
|
12147
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12146
diff
changeset
|
91 |
ReactProtocolMessage(HWProtocolMessage), |
12150 | 92 |
CheckRegistered, |
93 |
JoinLobby, |
|
12152 | 94 |
AddRoom(String, Option<String>), |
13421 | 95 |
RemoveRoom(RoomId), |
96 |
MoveToRoom(RoomId), |
|
97 |
MoveToLobby(String), |
|
98 |
ChangeMaster(RoomId, Option<ClientId>), |
|
13424 | 99 |
RemoveTeam(String), |
100 |
RemoveClientTeams, |
|
13421 | 101 |
SendRoomUpdate(Option<String>), |
13428 | 102 |
StartRoomGame(RoomId), |
103 |
SendTeamRemovalMessage(String), |
|
104 |
FinishRoomGame(RoomId), |
|
13431 | 105 |
SendRoomData{to: ClientId, teams: bool, config: bool, flags: bool}, |
12152 | 106 |
Warn(String), |
13421 | 107 |
ProtocolError(String) |
12143 | 108 |
} |
12149 | 109 |
|
110 |
use self::Action::*; |
|
111 |
||
13424 | 112 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12149 | 113 |
match action { |
13424 | 114 |
Send(msg) => server.send(client_id, msg.destination, msg.message), |
12149 | 115 |
ByeClient(msg) => { |
13421 | 116 |
let room_id; |
117 |
let nick; |
|
118 |
{ |
|
13424 | 119 |
let c = &server.clients[client_id]; |
13421 | 120 |
room_id = c.room_id; |
121 |
nick = c.nick.clone(); |
|
122 |
} |
|
123 |
||
13424 | 124 |
room_id.map (|id| { |
125 |
if id != server.lobby_id { |
|
126 |
server.react(client_id, vec![ |
|
127 |
MoveToLobby(format!("quit: {}", msg.clone()))]); |
|
13421 | 128 |
} |
129 |
}); |
|
130 |
||
13424 | 131 |
server.react(client_id, vec![ |
132 |
LobbyLeft(nick, msg.clone()).send_all().action(), |
|
133 |
Bye(msg).send_self().action(), |
|
13421 | 134 |
RemoveClient]); |
12149 | 135 |
}, |
136 |
RemoveClient => { |
|
13424 | 137 |
server.removed_clients.push(client_id); |
138 |
if server.clients.contains(client_id) { |
|
139 |
server.clients.remove(client_id); |
|
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
140 |
} |
12149 | 141 |
}, |
142 |
ReactProtocolMessage(msg) => |
|
13424 | 143 |
handlers::handle(server, client_id, msg), |
12150 | 144 |
CheckRegistered => |
13424 | 145 |
if server.clients[client_id].protocol_number > 0 && server.clients[client_id].nick != "" { |
146 |
server.react(client_id, vec![ |
|
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
147 |
JoinLobby, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
148 |
]); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
149 |
}, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
150 |
JoinLobby => { |
13424 | 151 |
server.clients[client_id].room_id = Some(server.lobby_id); |
12152 | 152 |
|
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
153 |
let joined_msg; |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
154 |
{ |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
155 |
let mut lobby_nicks = Vec::new(); |
12857 | 156 |
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
|
157 |
if c.room_id.is_some() { |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
158 |
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
|
159 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
160 |
} |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
161 |
joined_msg = LobbyJoined(lobby_nicks); |
12151
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12150
diff
changeset
|
162 |
} |
13424 | 163 |
let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]); |
13421 | 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()); |
|
13424 | 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(), |
|
12150 | 182 |
]); |
183 |
}, |
|
12152 | 184 |
AddRoom(name, password) => { |
13124
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12858
diff
changeset
|
185 |
let room_id = server.add_room();; |
13424 | 186 |
let actions = { |
12153 | 187 |
let r = &mut server.rooms[room_id]; |
13424 | 188 |
let c = &mut server.clients[client_id]; |
13421 | 189 |
r.master_id = Some(c.id); |
12153 | 190 |
r.name = name; |
191 |
r.password = password; |
|
192 |
r.protocol_number = c.protocol_number; |
|
13421 | 193 |
|
13424 | 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); |
|
13421 | 200 |
}, |
201 |
RemoveRoom(room_id) => { |
|
13424 | 202 |
let actions = { |
13421 | 203 |
let r = &mut server.rooms[room_id]; |
13424 | 204 |
vec![RoomRemove(r.name.clone()).send_all() |
205 |
.with_protocol(r.protocol_number).action()] |
|
206 |
}; |
|
13421 | 207 |
server.rooms.remove(room_id); |
13424 | 208 |
server.react(client_id, actions); |
13421 | 209 |
} |
210 |
MoveToRoom(room_id) => { |
|
13424 | 211 |
let actions = { |
13421 | 212 |
let r = &mut server.rooms[room_id]; |
13424 | 213 |
let c = &mut server.clients[client_id]; |
13421 | 214 |
r.players_number += 1; |
12153 | 215 |
c.room_id = Some(room_id); |
13421 | 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 |
} |
|
13424 | 225 |
let flags_msg = ClientFlags("+i".to_string(), vec![c.nick.clone()]); |
226 |
||
13427 | 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 { |
|
13431 | 232 |
v.push(SendRoomData{ to: client_id, teams: true, config: true, flags: true}); |
13427 | 233 |
} |
234 |
v |
|
13424 | 235 |
}; |
236 |
server.react(client_id, actions); |
|
13429 | 237 |
} |
13431 | 238 |
SendRoomData {to, teams, config, flags} => { |
13429 | 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()) |
|
13431 | 244 |
.send(to).action()); |
13429 | 245 |
for cfg in r.game_config().into_iter() { |
13431 | 246 |
actions.push(cfg.into_server_msg().send(to).action()); |
13429 | 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)) |
|
13431 | 252 |
.send(to).action()); |
13429 | 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()]) |
|
13431 | 258 |
.send(to).action()); |
13429 | 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) |
|
13431 | 265 |
.send(to).action()); |
13429 | 266 |
} |
267 |
} |
|
268 |
} |
|
269 |
server.react(client_id, actions); |
|
270 |
} |
|
13421 | 271 |
MoveToLobby(msg) => { |
272 |
let mut actions = Vec::new(); |
|
273 |
let lobby_id = server.lobby_id; |
|
13424 | 274 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13421 | 275 |
r.players_number -= 1; |
13429 | 276 |
if c.is_ready && r.ready_players_number > 0 { |
13421 | 277 |
r.ready_players_number -= 1; |
278 |
} |
|
279 |
if r.players_number > 0 && c.is_master { |
|
280 |
actions.push(ChangeMaster(r.id, None)); |
|
281 |
} |
|
13424 | 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()); |
|
13421 | 287 |
actions.push(SendRoomUpdate(Some(r.name.clone()))); |
288 |
} |
|
13424 | 289 |
server.react(client_id, actions); |
13421 | 290 |
actions = Vec::new(); |
291 |
||
13424 | 292 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13421 | 293 |
c.room_id = Some(lobby_id); |
294 |
if r.players_number == 0 { |
|
295 |
actions.push(RemoveRoom(r.id)); |
|
296 |
} |
|
297 |
} |
|
13424 | 298 |
server.react(client_id, actions) |
13421 | 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(|| |
|
13424 | 304 |
room_client_ids.iter().find(|id| **id != client_id).map(|id| *id)); |
13421 | 305 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
306 |
||
13424 | 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 => {} |
|
13421 | 317 |
} |
318 |
r.master_id = new_id; |
|
319 |
if let Some(nick) = new_nick { |
|
13424 | 320 |
actions.push(ClientFlags("+h".to_string(), vec![nick]) |
321 |
.send_all().in_room(r.id).action()); |
|
13421 | 322 |
} |
323 |
} |
|
324 |
new_id.map(|id| server.clients[id].is_master = true); |
|
13424 | 325 |
server.react(client_id, actions); |
326 |
} |
|
327 |
RemoveTeam(name) => { |
|
13431 | 328 |
let mut actions = Vec::new(); |
329 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
13424 | 330 |
r.remove_team(&name); |
13431 | 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 |
} |
|
13424 | 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); |
|
13421 | 349 |
} |
350 |
SendRoomUpdate(old_name) => { |
|
13424 | 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); |
|
13428 | 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 { |
|
13431 | 371 |
room.game_info = Some(GameInfo::new(room.teams.len() as u8)); |
13428 | 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); |
|
13421 | 384 |
} |
13428 | 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) => { |
|
13431 | 401 |
let mut actions = Vec::new(); |
402 |
let mut old_info = None; |
|
403 |
{ |
|
13428 | 404 |
let r = &mut server.rooms[room_id]; |
13431 | 405 |
swap(&mut old_info, &mut r.game_info); |
13428 | 406 |
r.game_info = None; |
13431 | 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 |
} |
|
13428 | 441 |
server.react(client_id, actions); |
442 |
} |
|
12152 | 443 |
Warn(msg) => { |
13424 | 444 |
run_action(server, client_id, Warning(msg).send_self().action()); |
12152 | 445 |
} |
13421 | 446 |
ProtocolError(msg) => { |
13424 | 447 |
run_action(server, client_id, Error(msg).send_self().action()) |
13421 | 448 |
} |
12149 | 449 |
} |
450 |
} |