author | alfadur |
Wed, 27 Jun 2018 02:34:46 +0300 | |
changeset 13423 | 87a6cad20c90 |
parent 13422 | 5fb27f94fc3b |
child 13424 | d8354cb98b98 |
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), |
|
12147 | 98 |
Warn(String), |
13416 | 99 |
ProtocolError(String) |
12138 | 100 |
} |
12144 | 101 |
|
102 |
use self::Action::*; |
|
103 |
||
13419 | 104 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12144 | 105 |
match action { |
13419 | 106 |
Send(msg) => server.send(client_id, msg.destination, msg.message), |
12144 | 107 |
ByeClient(msg) => { |
13416 | 108 |
let room_id; |
109 |
let nick; |
|
110 |
{ |
|
13419 | 111 |
let c = &server.clients[client_id]; |
13416 | 112 |
room_id = c.room_id; |
113 |
nick = c.nick.clone(); |
|
114 |
} |
|
115 |
||
13419 | 116 |
room_id.map (|id| { |
117 |
if id != server.lobby_id { |
|
118 |
server.react(client_id, vec![ |
|
119 |
MoveToLobby(format!("quit: {}", msg.clone()))]); |
|
13416 | 120 |
} |
121 |
}); |
|
122 |
||
13419 | 123 |
server.react(client_id, vec![ |
124 |
LobbyLeft(nick, msg.clone()).send_all().action(), |
|
125 |
Bye(msg).send_self().action(), |
|
13416 | 126 |
RemoveClient]); |
12144 | 127 |
}, |
128 |
RemoveClient => { |
|
13419 | 129 |
server.removed_clients.push(client_id); |
130 |
if server.clients.contains(client_id) { |
|
131 |
server.clients.remove(client_id); |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
132 |
} |
12144 | 133 |
}, |
134 |
ReactProtocolMessage(msg) => |
|
13419 | 135 |
handlers::handle(server, client_id, msg), |
12145 | 136 |
CheckRegistered => |
13419 | 137 |
if server.clients[client_id].protocol_number > 0 && server.clients[client_id].nick != "" { |
138 |
server.react(client_id, vec![ |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
139 |
JoinLobby, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
140 |
]); |
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 |
JoinLobby => { |
13419 | 143 |
server.clients[client_id].room_id = Some(server.lobby_id); |
12147 | 144 |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
145 |
let joined_msg; |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
146 |
{ |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
147 |
let mut lobby_nicks = Vec::new(); |
12852 | 148 |
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
|
149 |
if c.room_id.is_some() { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
150 |
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
|
151 |
} |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
152 |
} |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
153 |
joined_msg = LobbyJoined(lobby_nicks); |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
154 |
} |
13419 | 155 |
let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]); |
13416 | 156 |
let flags_msg = ClientFlags( |
157 |
"+i".to_string(), |
|
158 |
server.clients.iter() |
|
159 |
.filter(|(_, c)| c.room_id.is_some()) |
|
160 |
.map(|(_, c)| c.nick.clone()) |
|
161 |
.collect()); |
|
162 |
let server_msg = ServerMessage("\u{1f994} is watching".to_string()); |
|
163 |
let rooms_msg = Rooms(server.rooms.iter() |
|
164 |
.filter(|(id, _)| *id != server.lobby_id) |
|
165 |
.flat_map(|(_, r)| |
|
166 |
r.info(r.master_id.map(|id| &server.clients[id]))) |
|
167 |
.collect()); |
|
13419 | 168 |
server.react(client_id, vec![ |
169 |
everyone_msg.send_all().but_self().action(), |
|
170 |
joined_msg.send_self().action(), |
|
171 |
flags_msg.send_self().action(), |
|
172 |
server_msg.send_self().action(), |
|
173 |
rooms_msg.send_self().action(), |
|
12145 | 174 |
]); |
175 |
}, |
|
12147 | 176 |
AddRoom(name, password) => { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
177 |
let room_id = server.add_room();; |
13419 | 178 |
let actions = { |
12148 | 179 |
let r = &mut server.rooms[room_id]; |
13419 | 180 |
let c = &mut server.clients[client_id]; |
13416 | 181 |
r.master_id = Some(c.id); |
12148 | 182 |
r.name = name; |
183 |
r.password = password; |
|
184 |
r.protocol_number = c.protocol_number; |
|
13416 | 185 |
|
13419 | 186 |
vec![ |
187 |
RoomAdd(r.info(Some(&c))).send_all() |
|
188 |
.with_protocol(r.protocol_number).action(), |
|
189 |
MoveToRoom(room_id)] |
|
190 |
}; |
|
191 |
server.react(client_id, actions); |
|
13416 | 192 |
}, |
193 |
RemoveRoom(room_id) => { |
|
13419 | 194 |
let actions = { |
13416 | 195 |
let r = &mut server.rooms[room_id]; |
13419 | 196 |
vec![RoomRemove(r.name.clone()).send_all() |
197 |
.with_protocol(r.protocol_number).action()] |
|
198 |
}; |
|
13416 | 199 |
server.rooms.remove(room_id); |
13419 | 200 |
server.react(client_id, actions); |
13416 | 201 |
} |
202 |
MoveToRoom(room_id) => { |
|
13419 | 203 |
let actions = { |
13416 | 204 |
let r = &mut server.rooms[room_id]; |
13419 | 205 |
let c = &mut server.clients[client_id]; |
13416 | 206 |
r.players_number += 1; |
12148 | 207 |
c.room_id = Some(room_id); |
13416 | 208 |
c.is_joined_mid_game = false; |
209 |
if r.master_id == Some(c.id) { |
|
210 |
r.ready_players_number += 1; |
|
211 |
c.is_master = true; |
|
212 |
c.is_ready = true; |
|
213 |
} else { |
|
214 |
c.is_ready = false; |
|
215 |
c.is_master = false; |
|
216 |
} |
|
13419 | 217 |
let flags_msg = ClientFlags("+i".to_string(), vec![c.nick.clone()]); |
218 |
||
13422 | 219 |
let mut v = vec![ |
220 |
RoomJoined(vec![c.nick.clone()]).send_all().in_room(room_id).action(), |
|
221 |
flags_msg.send_all().action(), |
|
222 |
SendRoomUpdate(None)]; |
|
223 |
if !c.is_master { |
|
224 |
v.push(ConfigEntry("FULLMAPCONFIG".to_string(), r.map_config()) |
|
225 |
.send_self().action()); |
|
226 |
for cfg in r.game_config().into_iter() { |
|
227 |
v.push(cfg.into_server_msg().send_self().action()); |
|
228 |
} |
|
229 |
} |
|
230 |
v |
|
13419 | 231 |
}; |
232 |
server.react(client_id, actions); |
|
12147 | 233 |
}, |
13416 | 234 |
MoveToLobby(msg) => { |
235 |
let mut actions = Vec::new(); |
|
236 |
let lobby_id = server.lobby_id; |
|
13419 | 237 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13416 | 238 |
r.players_number -= 1; |
239 |
if c.is_ready { |
|
240 |
r.ready_players_number -= 1; |
|
241 |
} |
|
242 |
if r.players_number > 0 && c.is_master { |
|
243 |
actions.push(ChangeMaster(r.id, None)); |
|
244 |
} |
|
13419 | 245 |
actions.push(RemoveClientTeams); |
246 |
actions.push(RoomLeft(c.nick.clone(), msg) |
|
247 |
.send_all().in_room(r.id).but_self().action()); |
|
248 |
actions.push(ClientFlags("-i".to_string(), vec![c.nick.clone()]) |
|
249 |
.send_all().action()); |
|
13416 | 250 |
actions.push(SendRoomUpdate(Some(r.name.clone()))); |
251 |
} |
|
13419 | 252 |
server.react(client_id, actions); |
13416 | 253 |
actions = Vec::new(); |
254 |
||
13419 | 255 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13416 | 256 |
c.room_id = Some(lobby_id); |
257 |
if r.players_number == 0 { |
|
258 |
actions.push(RemoveRoom(r.id)); |
|
259 |
} |
|
260 |
} |
|
13419 | 261 |
server.react(client_id, actions) |
13416 | 262 |
} |
263 |
ChangeMaster(room_id, new_id) => { |
|
264 |
let mut actions = Vec::new(); |
|
265 |
let room_client_ids = server.room_clients(room_id); |
|
266 |
let new_id = new_id.or_else(|| |
|
13419 | 267 |
room_client_ids.iter().find(|id| **id != client_id).map(|id| *id)); |
13416 | 268 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
269 |
||
13419 | 270 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
271 |
match r.master_id { |
|
272 |
Some(id) if id == c.id => { |
|
273 |
c.is_master = false; |
|
274 |
r.master_id = None; |
|
275 |
actions.push(ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
|
276 |
.send_all().in_room(r.id).action()); |
|
277 |
} |
|
278 |
Some(_) => unreachable!(), |
|
279 |
None => {} |
|
13416 | 280 |
} |
281 |
r.master_id = new_id; |
|
282 |
if let Some(nick) = new_nick { |
|
13419 | 283 |
actions.push(ClientFlags("+h".to_string(), vec![nick]) |
284 |
.send_all().in_room(r.id).action()); |
|
13416 | 285 |
} |
286 |
} |
|
287 |
new_id.map(|id| server.clients[id].is_master = true); |
|
13419 | 288 |
server.react(client_id, actions); |
289 |
} |
|
290 |
RemoveTeam(name) => { |
|
13422 | 291 |
let actions = if let (_, Some(r)) = server.client_and_room(client_id) { |
13419 | 292 |
r.remove_team(&name); |
293 |
vec![TeamRemove(name).send_all().in_room(r.id).action(), |
|
294 |
SendRoomUpdate(None)] |
|
295 |
} else { |
|
296 |
Vec::new() |
|
297 |
}; |
|
298 |
server.react(client_id, actions); |
|
299 |
}, |
|
300 |
RemoveClientTeams => { |
|
301 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
302 |
r.client_teams(c.id).map(|t| RemoveTeam(t.name.clone())).collect() |
|
303 |
} else { |
|
304 |
Vec::new() |
|
305 |
}; |
|
306 |
server.react(client_id, actions); |
|
13416 | 307 |
} |
308 |
SendRoomUpdate(old_name) => { |
|
13419 | 309 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
310 |
let name = old_name.unwrap_or_else(|| r.name.clone()); |
|
311 |
vec![RoomUpdated(name, r.info(Some(&c))) |
|
312 |
.send_all().with_protocol(r.protocol_number).action()] |
|
313 |
} else { |
|
314 |
Vec::new() |
|
315 |
}; |
|
316 |
server.react(client_id, actions); |
|
13423 | 317 |
}, |
318 |
StartRoomGame(room_id) => { |
|
319 |
let actions = { |
|
320 |
let (room_clients, room_nicks): (Vec<_>, Vec<_>) = server.clients.iter() |
|
321 |
.map(|(id, c)| (id, c.nick.clone())).unzip(); |
|
322 |
let room = &mut server.rooms[room_id]; |
|
323 |
||
324 |
if !room.has_multiple_clans() { |
|
325 |
vec![Warn("The game can't be started with less than two clans!".to_string())] |
|
326 |
} else if room.game_info.is_some() { |
|
327 |
vec![Warn("The game is already in progress".to_string())] |
|
328 |
} else { |
|
329 |
room.game_info = Some(GameInfo { |
|
330 |
teams_in_game: room.teams.len() as u8 |
|
331 |
}); |
|
332 |
for id in room_clients { |
|
333 |
let c = &mut server.clients[id]; |
|
334 |
c.is_in_game = true; |
|
335 |
c.team_indices = room.client_team_indices(c.id); |
|
336 |
} |
|
337 |
vec![RunGame.send_all().in_room(room.id).action(), |
|
338 |
SendRoomUpdate(None), |
|
339 |
ClientFlags("+g".to_string(), room_nicks) |
|
340 |
.send_all().in_room(room.id).action()] |
|
341 |
} |
|
342 |
}; |
|
343 |
server.react(client_id, actions); |
|
13416 | 344 |
} |
13423 | 345 |
SendTeamRemovalMessage(team_name) => { |
346 |
let mut actions = Vec::new(); |
|
347 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
|
348 |
if let Some(ref mut info) = r.game_info { |
|
349 |
let msg = once(b'F').chain(team_name.bytes()); |
|
350 |
actions.push(ForwardEngineMessage(to_engine_msg(msg)). |
|
351 |
send_all().in_room(r.id).but_self().action()); |
|
352 |
info.teams_in_game -= 1; |
|
353 |
if info.teams_in_game == 0 { |
|
354 |
actions.push(FinishRoomGame(r.id)); |
|
355 |
} |
|
356 |
} |
|
357 |
} |
|
358 |
server.react(client_id, actions); |
|
359 |
} |
|
360 |
FinishRoomGame(room_id) => { |
|
361 |
let actions = { |
|
362 |
let r = &mut server.rooms[room_id]; |
|
363 |
r.game_info = None; |
|
364 |
r.ready_players_number = 0; |
|
365 |
vec![SendRoomUpdate(None), |
|
366 |
RoundFinished.send_all().in_room(r.id).action()] |
|
367 |
}; |
|
368 |
server.react(client_id, actions); |
|
369 |
} |
|
12147 | 370 |
Warn(msg) => { |
13419 | 371 |
run_action(server, client_id, Warning(msg).send_self().action()); |
12147 | 372 |
} |
13416 | 373 |
ProtocolError(msg) => { |
13419 | 374 |
run_action(server, client_id, Error(msg).send_self().action()) |
13416 | 375 |
} |
12144 | 376 |
} |
377 |
} |