author | alfadur |
Thu, 21 Jun 2018 17:23:10 -0400 | |
changeset 13419 | 81e0ed105f5d |
parent 13416 | cdf69667593b |
child 13422 | 5fb27f94fc3b |
permissions | -rw-r--r-- |
13416 | 1 |
use std::{ |
2 |
io, io::Write |
|
3 |
}; |
|
4 |
use super::{ |
|
5 |
server::HWServer, |
|
13419 | 6 |
room::RoomId, |
7 |
client::{ClientId, HWClient}, |
|
13416 | 8 |
room::HWRoom, |
9 |
handlers |
|
10 |
}; |
|
11 |
use protocol::messages::{ |
|
12 |
HWProtocolMessage, |
|
13 |
HWServerMessage, |
|
14 |
HWServerMessage::* |
|
15 |
}; |
|
12138 | 16 |
|
13419 | 17 |
pub enum Destination { |
18 |
ToSelf, |
|
19 |
ToAll { |
|
20 |
room_id: Option<RoomId>, |
|
21 |
protocol: Option<u32>, |
|
22 |
skip_self: bool |
|
23 |
} |
|
24 |
} |
|
25 |
||
26 |
pub struct PendingMessage { |
|
27 |
pub destination: Destination, |
|
28 |
pub message: HWServerMessage |
|
29 |
} |
|
30 |
||
31 |
impl PendingMessage { |
|
32 |
pub fn send_self(message: HWServerMessage) -> PendingMessage { |
|
33 |
PendingMessage{ destination: Destination::ToSelf, message } |
|
34 |
} |
|
35 |
||
36 |
pub fn send_all(message: HWServerMessage) -> PendingMessage { |
|
37 |
let destination = Destination::ToAll { |
|
38 |
room_id: None, |
|
39 |
protocol: None, |
|
40 |
skip_self: false, |
|
41 |
}; |
|
42 |
PendingMessage{ destination, message } |
|
43 |
} |
|
44 |
||
45 |
pub fn in_room(mut self, clients_room_id: RoomId) -> PendingMessage { |
|
46 |
if let Destination::ToAll {ref mut room_id, ..} = self.destination { |
|
47 |
*room_id = Some(clients_room_id) |
|
48 |
} |
|
49 |
self |
|
50 |
} |
|
51 |
||
52 |
pub fn with_protocol(mut self, protocol_number: u32) -> PendingMessage { |
|
53 |
if let Destination::ToAll {ref mut protocol, ..} = self.destination { |
|
54 |
*protocol = Some(protocol_number) |
|
55 |
} |
|
56 |
self |
|
57 |
} |
|
58 |
||
59 |
pub fn but_self(mut self) -> PendingMessage { |
|
60 |
if let Destination::ToAll {ref mut skip_self, ..} = self.destination { |
|
61 |
*skip_self = true |
|
62 |
} |
|
63 |
self |
|
64 |
} |
|
65 |
||
66 |
pub fn action(self) -> Action { Send(self) } |
|
67 |
} |
|
68 |
||
69 |
impl Into<Action> for PendingMessage { |
|
70 |
fn into(self) -> Action { self.action() } |
|
71 |
} |
|
72 |
||
73 |
impl HWServerMessage { |
|
74 |
pub fn send_self(self) -> PendingMessage { PendingMessage::send_self(self) } |
|
75 |
pub fn send_all(self) -> PendingMessage { PendingMessage::send_all(self) } |
|
76 |
} |
|
77 |
||
12138 | 78 |
pub enum Action { |
13419 | 79 |
Send(PendingMessage), |
12139 | 80 |
RemoveClient, |
81 |
ByeClient(String), |
|
12142
4d7d41be1993
Start refactoring path from getting message from client to reacting to it
unc0rr
parents:
12141
diff
changeset
|
82 |
ReactProtocolMessage(HWProtocolMessage), |
12145 | 83 |
CheckRegistered, |
84 |
JoinLobby, |
|
12147 | 85 |
AddRoom(String, Option<String>), |
13416 | 86 |
RemoveRoom(RoomId), |
87 |
MoveToRoom(RoomId), |
|
88 |
MoveToLobby(String), |
|
89 |
ChangeMaster(RoomId, Option<ClientId>), |
|
13419 | 90 |
RemoveTeam(String), |
91 |
RemoveClientTeams, |
|
13416 | 92 |
SendRoomUpdate(Option<String>), |
12147 | 93 |
Warn(String), |
13416 | 94 |
ProtocolError(String) |
12138 | 95 |
} |
12144 | 96 |
|
97 |
use self::Action::*; |
|
98 |
||
13419 | 99 |
pub fn run_action(server: &mut HWServer, client_id: usize, action: Action) { |
12144 | 100 |
match action { |
13419 | 101 |
Send(msg) => server.send(client_id, msg.destination, msg.message), |
12144 | 102 |
ByeClient(msg) => { |
13416 | 103 |
let room_id; |
104 |
let nick; |
|
105 |
{ |
|
13419 | 106 |
let c = &server.clients[client_id]; |
13416 | 107 |
room_id = c.room_id; |
108 |
nick = c.nick.clone(); |
|
109 |
} |
|
110 |
||
13419 | 111 |
room_id.map (|id| { |
112 |
if id != server.lobby_id { |
|
113 |
server.react(client_id, vec![ |
|
114 |
MoveToLobby(format!("quit: {}", msg.clone()))]); |
|
13416 | 115 |
} |
116 |
}); |
|
117 |
||
13419 | 118 |
server.react(client_id, vec![ |
119 |
LobbyLeft(nick, msg.clone()).send_all().action(), |
|
120 |
Bye(msg).send_self().action(), |
|
13416 | 121 |
RemoveClient]); |
12144 | 122 |
}, |
123 |
RemoveClient => { |
|
13419 | 124 |
server.removed_clients.push(client_id); |
125 |
if server.clients.contains(client_id) { |
|
126 |
server.clients.remove(client_id); |
|
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
127 |
} |
12144 | 128 |
}, |
129 |
ReactProtocolMessage(msg) => |
|
13419 | 130 |
handlers::handle(server, client_id, msg), |
12145 | 131 |
CheckRegistered => |
13419 | 132 |
if server.clients[client_id].protocol_number > 0 && server.clients[client_id].nick != "" { |
133 |
server.react(client_id, vec![ |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
134 |
JoinLobby, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
135 |
]); |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
136 |
}, |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
137 |
JoinLobby => { |
13419 | 138 |
server.clients[client_id].room_id = Some(server.lobby_id); |
12147 | 139 |
|
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
140 |
let joined_msg; |
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
141 |
{ |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
142 |
let mut lobby_nicks = Vec::new(); |
12852 | 143 |
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
|
144 |
if c.room_id.is_some() { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
145 |
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
|
146 |
} |
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 |
joined_msg = LobbyJoined(lobby_nicks); |
12146
8d8fb85bc09c
SendAllButMe action, list all clients in lobby in LobbyJoined message to newcomers
unc0rr
parents:
12145
diff
changeset
|
149 |
} |
13419 | 150 |
let everyone_msg = LobbyJoined(vec![server.clients[client_id].nick.clone()]); |
13416 | 151 |
let flags_msg = ClientFlags( |
152 |
"+i".to_string(), |
|
153 |
server.clients.iter() |
|
154 |
.filter(|(_, c)| c.room_id.is_some()) |
|
155 |
.map(|(_, c)| c.nick.clone()) |
|
156 |
.collect()); |
|
157 |
let server_msg = ServerMessage("\u{1f994} is watching".to_string()); |
|
158 |
let rooms_msg = Rooms(server.rooms.iter() |
|
159 |
.filter(|(id, _)| *id != server.lobby_id) |
|
160 |
.flat_map(|(_, r)| |
|
161 |
r.info(r.master_id.map(|id| &server.clients[id]))) |
|
162 |
.collect()); |
|
13419 | 163 |
server.react(client_id, vec![ |
164 |
everyone_msg.send_all().but_self().action(), |
|
165 |
joined_msg.send_self().action(), |
|
166 |
flags_msg.send_self().action(), |
|
167 |
server_msg.send_self().action(), |
|
168 |
rooms_msg.send_self().action(), |
|
12145 | 169 |
]); |
170 |
}, |
|
12147 | 171 |
AddRoom(name, password) => { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12853
diff
changeset
|
172 |
let room_id = server.add_room();; |
13419 | 173 |
let actions = { |
12148 | 174 |
let r = &mut server.rooms[room_id]; |
13419 | 175 |
let c = &mut server.clients[client_id]; |
13416 | 176 |
r.master_id = Some(c.id); |
12148 | 177 |
r.name = name; |
178 |
r.password = password; |
|
179 |
r.protocol_number = c.protocol_number; |
|
13416 | 180 |
|
13419 | 181 |
vec![ |
182 |
RoomAdd(r.info(Some(&c))).send_all() |
|
183 |
.with_protocol(r.protocol_number).action(), |
|
184 |
MoveToRoom(room_id)] |
|
185 |
}; |
|
186 |
server.react(client_id, actions); |
|
13416 | 187 |
}, |
188 |
RemoveRoom(room_id) => { |
|
13419 | 189 |
let actions = { |
13416 | 190 |
let r = &mut server.rooms[room_id]; |
13419 | 191 |
vec![RoomRemove(r.name.clone()).send_all() |
192 |
.with_protocol(r.protocol_number).action()] |
|
193 |
}; |
|
13416 | 194 |
server.rooms.remove(room_id); |
13419 | 195 |
server.react(client_id, actions); |
13416 | 196 |
} |
197 |
MoveToRoom(room_id) => { |
|
13419 | 198 |
let actions = { |
13416 | 199 |
let r = &mut server.rooms[room_id]; |
13419 | 200 |
let c = &mut server.clients[client_id]; |
13416 | 201 |
r.players_number += 1; |
12148 | 202 |
c.room_id = Some(room_id); |
13416 | 203 |
c.is_joined_mid_game = false; |
204 |
if r.master_id == Some(c.id) { |
|
205 |
r.ready_players_number += 1; |
|
206 |
c.is_master = true; |
|
207 |
c.is_ready = true; |
|
208 |
} else { |
|
209 |
c.is_ready = false; |
|
210 |
c.is_master = false; |
|
211 |
} |
|
13419 | 212 |
let flags_msg = ClientFlags("+i".to_string(), vec![c.nick.clone()]); |
213 |
||
214 |
vec![RoomJoined(vec![c.nick.clone()]).send_all().in_room(room_id).action(), |
|
215 |
flags_msg.send_all().action(), |
|
216 |
SendRoomUpdate(None)] |
|
217 |
}; |
|
218 |
server.react(client_id, actions); |
|
12147 | 219 |
}, |
13416 | 220 |
MoveToLobby(msg) => { |
221 |
let mut actions = Vec::new(); |
|
222 |
let lobby_id = server.lobby_id; |
|
13419 | 223 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13416 | 224 |
r.players_number -= 1; |
225 |
if c.is_ready { |
|
226 |
r.ready_players_number -= 1; |
|
227 |
} |
|
228 |
if r.players_number > 0 && c.is_master { |
|
229 |
actions.push(ChangeMaster(r.id, None)); |
|
230 |
} |
|
13419 | 231 |
actions.push(RemoveClientTeams); |
232 |
actions.push(RoomLeft(c.nick.clone(), msg) |
|
233 |
.send_all().in_room(r.id).but_self().action()); |
|
234 |
actions.push(ClientFlags("-i".to_string(), vec![c.nick.clone()]) |
|
235 |
.send_all().action()); |
|
13416 | 236 |
actions.push(SendRoomUpdate(Some(r.name.clone()))); |
237 |
} |
|
13419 | 238 |
server.react(client_id, actions); |
13416 | 239 |
actions = Vec::new(); |
240 |
||
13419 | 241 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
13416 | 242 |
c.room_id = Some(lobby_id); |
243 |
if r.players_number == 0 { |
|
244 |
actions.push(RemoveRoom(r.id)); |
|
245 |
} |
|
246 |
} |
|
13419 | 247 |
server.react(client_id, actions) |
13416 | 248 |
} |
249 |
ChangeMaster(room_id, new_id) => { |
|
250 |
let mut actions = Vec::new(); |
|
251 |
let room_client_ids = server.room_clients(room_id); |
|
252 |
let new_id = new_id.or_else(|| |
|
13419 | 253 |
room_client_ids.iter().find(|id| **id != client_id).map(|id| *id)); |
13416 | 254 |
let new_nick = new_id.map(|id| server.clients[id].nick.clone()); |
255 |
||
13419 | 256 |
if let (c, Some(r)) = server.client_and_room(client_id) { |
257 |
match r.master_id { |
|
258 |
Some(id) if id == c.id => { |
|
259 |
c.is_master = false; |
|
260 |
r.master_id = None; |
|
261 |
actions.push(ClientFlags("-h".to_string(), vec![c.nick.clone()]) |
|
262 |
.send_all().in_room(r.id).action()); |
|
263 |
} |
|
264 |
Some(_) => unreachable!(), |
|
265 |
None => {} |
|
13416 | 266 |
} |
267 |
r.master_id = new_id; |
|
268 |
if let Some(nick) = new_nick { |
|
13419 | 269 |
actions.push(ClientFlags("+h".to_string(), vec![nick]) |
270 |
.send_all().in_room(r.id).action()); |
|
13416 | 271 |
} |
272 |
} |
|
273 |
new_id.map(|id| server.clients[id].is_master = true); |
|
13419 | 274 |
server.react(client_id, actions); |
275 |
} |
|
276 |
RemoveTeam(name) => { |
|
277 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
278 |
r.remove_team(&name); |
|
279 |
vec![TeamRemove(name).send_all().in_room(r.id).action(), |
|
280 |
SendRoomUpdate(None)] |
|
281 |
} else { |
|
282 |
Vec::new() |
|
283 |
}; |
|
284 |
server.react(client_id, actions); |
|
285 |
}, |
|
286 |
RemoveClientTeams => { |
|
287 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
|
288 |
r.client_teams(c.id).map(|t| RemoveTeam(t.name.clone())).collect() |
|
289 |
} else { |
|
290 |
Vec::new() |
|
291 |
}; |
|
292 |
server.react(client_id, actions); |
|
13416 | 293 |
} |
294 |
SendRoomUpdate(old_name) => { |
|
13419 | 295 |
let actions = if let (c, Some(r)) = server.client_and_room(client_id) { |
296 |
let name = old_name.unwrap_or_else(|| r.name.clone()); |
|
297 |
vec![RoomUpdated(name, r.info(Some(&c))) |
|
298 |
.send_all().with_protocol(r.protocol_number).action()] |
|
299 |
} else { |
|
300 |
Vec::new() |
|
301 |
}; |
|
302 |
server.react(client_id, actions); |
|
13416 | 303 |
} |
304 |
||
12147 | 305 |
Warn(msg) => { |
13419 | 306 |
run_action(server, client_id, Warning(msg).send_self().action()); |
12147 | 307 |
} |
13416 | 308 |
ProtocolError(msg) => { |
13419 | 309 |
run_action(server, client_id, Error(msg).send_self().action()) |
13416 | 310 |
} |
12144 | 311 |
} |
312 |
} |