author | Wuzzy <Wuzzy2@mail.ru> |
Thu, 14 Nov 2019 17:12:31 +0100 | |
changeset 15526 | 266ff128a65a |
parent 15514 | 395be40faa51 |
child 15538 | b907b9071ec5 |
permissions | -rw-r--r-- |
13416 | 1 |
use super::{ |
15096 | 2 |
client::HwClient, |
14714 | 3 |
indexslab::IndexSlab, |
15096 | 4 |
room::HwRoom, |
15463 | 5 |
types::{ClientId, RoomId, ServerVar}, |
13416 | 6 |
}; |
15096 | 7 |
use crate::{protocol::messages::HwProtocolMessage::Greeting, utils}; |
14714 | 8 |
|
15463 | 9 |
use crate::core::server::JoinRoomError::WrongProtocol; |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
10 |
use bitflags::*; |
13810 | 11 |
use log::*; |
14478 | 12 |
use slab; |
15463 | 13 |
use std::{borrow::BorrowMut, collections::HashSet, iter, num::NonZeroU16}; |
12126 | 14 |
|
12852 | 15 |
type Slab<T> = slab::Slab<T>; |
12127 | 16 |
|
15465 | 17 |
#[derive(Debug)] |
15463 | 18 |
pub enum CreateRoomError { |
19 |
InvalidName, |
|
20 |
AlreadyExists, |
|
21 |
} |
|
22 |
||
15465 | 23 |
#[derive(Debug)] |
15463 | 24 |
pub enum JoinRoomError { |
25 |
DoesntExist, |
|
26 |
WrongProtocol, |
|
27 |
Full, |
|
28 |
Restricted, |
|
29 |
} |
|
30 |
||
15504 | 31 |
pub enum LeaveRoomResult { |
32 |
RoomRemoved, |
|
33 |
RoomRemains { |
|
34 |
is_empty: bool, |
|
35 |
was_master: bool, |
|
36 |
was_in_game: bool, |
|
37 |
new_master: Option<ClientId>, |
|
38 |
removed_teams: Vec<String>, |
|
39 |
}, |
|
40 |
} |
|
41 |
||
42 |
#[derive(Debug)] |
|
43 |
pub enum LeaveRoomError { |
|
44 |
NoRoom, |
|
45 |
} |
|
46 |
||
15465 | 47 |
#[derive(Debug)] |
15509 | 48 |
pub struct ChangeMasterResult { |
49 |
pub old_master_id: Option<ClientId>, |
|
50 |
pub new_master_id: ClientId, |
|
51 |
} |
|
52 |
||
53 |
#[derive(Debug)] |
|
54 |
pub enum ChangeMasterError { |
|
55 |
NoAccess, |
|
56 |
AlreadyMaster, |
|
57 |
NoClient, |
|
58 |
ClientNotInRoom, |
|
59 |
} |
|
60 |
||
61 |
#[derive(Debug)] |
|
15514 | 62 |
pub enum StartGameError { |
63 |
NotEnoughClans, |
|
64 |
NotEnoughTeams, |
|
65 |
NotReady, |
|
66 |
AlreadyInGame, |
|
67 |
} |
|
68 |
||
69 |
#[derive(Debug)] |
|
15465 | 70 |
pub struct UninitializedError(); |
71 |
#[derive(Debug)] |
|
15463 | 72 |
pub struct AccessError(); |
73 |
||
15096 | 74 |
pub struct HwAnteClient { |
14714 | 75 |
pub nick: Option<String>, |
76 |
pub protocol_number: Option<NonZeroU16>, |
|
77 |
pub server_salt: String, |
|
14802 | 78 |
pub is_checker: bool, |
15197 | 79 |
pub is_local_admin: bool, |
15465 | 80 |
pub is_registered: bool, |
81 |
pub is_admin: bool, |
|
82 |
pub is_contributor: bool, |
|
14714 | 83 |
} |
84 |
||
15096 | 85 |
pub struct HwAnteroom { |
86 |
pub clients: IndexSlab<HwAnteClient>, |
|
14714 | 87 |
} |
88 |
||
15096 | 89 |
impl HwAnteroom { |
14714 | 90 |
pub fn new(clients_limit: usize) -> Self { |
91 |
let clients = IndexSlab::with_capacity(clients_limit); |
|
15096 | 92 |
HwAnteroom { clients } |
14714 | 93 |
} |
94 |
||
15197 | 95 |
pub fn add_client(&mut self, client_id: ClientId, salt: String, is_local_admin: bool) { |
15096 | 96 |
let client = HwAnteClient { |
14714 | 97 |
nick: None, |
98 |
protocol_number: None, |
|
99 |
server_salt: salt, |
|
14802 | 100 |
is_checker: false, |
15197 | 101 |
is_local_admin, |
15465 | 102 |
is_registered: false, |
103 |
is_admin: false, |
|
104 |
is_contributor: false, |
|
14714 | 105 |
}; |
106 |
self.clients.insert(client_id, client); |
|
107 |
} |
|
108 |
||
15096 | 109 |
pub fn remove_client(&mut self, client_id: ClientId) -> Option<HwAnteClient> { |
15463 | 110 |
let client = self.clients.remove(client_id); |
14714 | 111 |
client |
112 |
} |
|
113 |
} |
|
114 |
||
14804 | 115 |
pub struct ServerGreetings { |
116 |
pub for_latest_protocol: String, |
|
117 |
pub for_old_protocols: String, |
|
118 |
} |
|
119 |
||
120 |
impl ServerGreetings { |
|
121 |
fn new() -> Self { |
|
122 |
Self { |
|
123 |
for_latest_protocol: "\u{1f994} is watching".to_string(), |
|
124 |
for_old_protocols: "\u{1f994} is watching".to_string(), |
|
125 |
} |
|
126 |
} |
|
127 |
} |
|
128 |
||
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
129 |
bitflags! { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
130 |
pub struct ServerFlags: u8 { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
131 |
const REGISTERED_ONLY = 0b0000_1000; |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
132 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
133 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
134 |
|
15096 | 135 |
pub struct HwServer { |
136 |
pub clients: IndexSlab<HwClient>, |
|
137 |
pub rooms: Slab<HwRoom>, |
|
138 |
pub anteroom: HwAnteroom, |
|
14804 | 139 |
pub latest_protocol: u16, |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
140 |
pub flags: ServerFlags, |
14804 | 141 |
pub greetings: ServerGreetings, |
12126 | 142 |
} |
143 |
||
15096 | 144 |
impl HwServer { |
14801 | 145 |
pub fn new(clients_limit: usize, rooms_limit: usize) -> Self { |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
146 |
let rooms = Slab::with_capacity(rooms_limit); |
14714 | 147 |
let clients = IndexSlab::with_capacity(clients_limit); |
14715 | 148 |
Self { |
14478 | 149 |
clients, |
150 |
rooms, |
|
15096 | 151 |
anteroom: HwAnteroom::new(clients_limit), |
14804 | 152 |
greetings: ServerGreetings::new(), |
153 |
latest_protocol: 58, |
|
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
154 |
flags: ServerFlags::empty(), |
14715 | 155 |
} |
12126 | 156 |
} |
157 |
||
15465 | 158 |
#[inline] |
159 |
pub fn client(&self, client_id: ClientId) -> &HwClient { |
|
160 |
&self.clients[client_id] |
|
161 |
} |
|
162 |
||
163 |
#[inline] |
|
164 |
pub fn client_mut(&mut self, client_id: ClientId) -> &mut HwClient { |
|
165 |
&mut self.clients[client_id] |
|
166 |
} |
|
167 |
||
168 |
#[inline] |
|
169 |
pub fn room(&self, room_id: RoomId) -> &HwRoom { |
|
170 |
&self.rooms[room_id] |
|
171 |
} |
|
172 |
||
173 |
#[inline] |
|
174 |
pub fn room_mut(&mut self, room_id: RoomId) -> &mut HwRoom { |
|
175 |
&mut self.rooms[room_id] |
|
176 |
} |
|
177 |
||
178 |
#[inline] |
|
15504 | 179 |
pub fn client_and_room(&self, client_id: ClientId, room_id: RoomId) -> (&HwClient, &HwRoom) { |
180 |
(&self.clients[client_id], &self.rooms[room_id]) |
|
181 |
} |
|
182 |
||
183 |
#[inline] |
|
184 |
pub fn client_and_room_mut( |
|
185 |
&mut self, |
|
186 |
client_id: ClientId, |
|
187 |
room_id: RoomId, |
|
188 |
) -> (&mut HwClient, &mut HwRoom) { |
|
189 |
(&mut self.clients[client_id], &mut self.rooms[room_id]) |
|
190 |
} |
|
191 |
||
192 |
#[inline] |
|
15465 | 193 |
pub fn is_admin(&self, client_id: ClientId) -> bool { |
194 |
self.clients |
|
195 |
.get(client_id) |
|
196 |
.map(|c| c.is_admin()) |
|
197 |
.unwrap_or(false) |
|
198 |
} |
|
199 |
||
15096 | 200 |
pub fn add_client(&mut self, client_id: ClientId, data: HwAnteClient) { |
14714 | 201 |
if let (Some(protocol), Some(nick)) = (data.protocol_number, data.nick) { |
15096 | 202 |
let mut client = HwClient::new(client_id, protocol.get(), nick); |
14802 | 203 |
client.set_is_checker(data.is_checker); |
15197 | 204 |
#[cfg(not(feature = "official-server"))] |
205 |
client.set_is_admin(data.is_local_admin); |
|
206 |
||
15465 | 207 |
#[cfg(feature = "official-server")] |
208 |
{ |
|
209 |
client.set_is_registered(info.is_registered); |
|
210 |
client.set_is_admin(info.is_admin); |
|
211 |
client.set_is_contributor(info.is_contributor); |
|
212 |
} |
|
213 |
||
14714 | 214 |
self.clients.insert(client_id, client); |
13119
1e39b8749072
separated the server logic from all the async io mess.
alfadur
parents:
12852
diff
changeset
|
215 |
} |
12126 | 216 |
} |
12127 | 217 |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
218 |
pub fn remove_client(&mut self, client_id: ClientId) { |
14717 | 219 |
self.clients.remove(client_id); |
12127 | 220 |
} |
221 |
||
15465 | 222 |
pub fn get_greetings(&self, client: &HwClient) -> &str { |
223 |
if client.protocol_number < self.latest_protocol { |
|
14804 | 224 |
&self.greetings.for_old_protocols |
225 |
} else { |
|
226 |
&self.greetings.for_latest_protocol |
|
227 |
} |
|
228 |
} |
|
229 |
||
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
230 |
#[inline] |
15463 | 231 |
pub fn get_client_nick(&self, client_id: ClientId) -> &str { |
232 |
&self.clients[client_id].nick |
|
233 |
} |
|
234 |
||
235 |
#[inline] |
|
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
236 |
pub fn create_room( |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
237 |
&mut self, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
238 |
creator_id: ClientId, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
239 |
name: String, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
240 |
password: Option<String>, |
15463 | 241 |
) -> Result<(&HwClient, &HwRoom), CreateRoomError> { |
242 |
use CreateRoomError::*; |
|
243 |
if utils::is_name_illegal(&name) { |
|
244 |
Err(InvalidName) |
|
245 |
} else if self.has_room(&name) { |
|
246 |
Err(AlreadyExists) |
|
247 |
} else { |
|
248 |
Ok(create_room( |
|
249 |
&mut self.clients[creator_id], |
|
250 |
&mut self.rooms, |
|
251 |
name, |
|
252 |
password, |
|
253 |
)) |
|
254 |
} |
|
255 |
} |
|
256 |
||
257 |
pub fn join_room( |
|
258 |
&mut self, |
|
259 |
client_id: ClientId, |
|
260 |
room_id: RoomId, |
|
261 |
) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
|
262 |
use JoinRoomError::*; |
|
263 |
let room = &mut self.rooms[room_id]; |
|
264 |
let client = &mut self.clients[client_id]; |
|
265 |
||
266 |
if client.protocol_number != room.protocol_number { |
|
267 |
Err(WrongProtocol) |
|
268 |
} else if room.is_join_restricted() { |
|
269 |
Err(Restricted) |
|
270 |
} else if room.players_number == u8::max_value() { |
|
271 |
Err(Full) |
|
272 |
} else { |
|
273 |
move_to_room(client, room); |
|
274 |
let room_id = room.id; |
|
275 |
Ok(( |
|
276 |
&self.clients[client_id], |
|
277 |
&self.rooms[room_id], |
|
278 |
self.clients.iter().map(|(_, c)| c), |
|
279 |
)) |
|
280 |
} |
|
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
281 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
282 |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
283 |
#[inline] |
15463 | 284 |
pub fn join_room_by_name( |
285 |
&mut self, |
|
286 |
client_id: ClientId, |
|
287 |
room_name: &str, |
|
288 |
) -> Result<(&HwClient, &HwRoom, impl Iterator<Item = &HwClient> + Clone), JoinRoomError> { |
|
289 |
use JoinRoomError::*; |
|
290 |
let room = self.rooms.iter().find(|(_, r)| r.name == room_name); |
|
291 |
if let Some((_, room)) = room { |
|
292 |
let room_id = room.id; |
|
293 |
self.join_room(client_id, room_id) |
|
294 |
} else { |
|
295 |
Err(DoesntExist) |
|
296 |
} |
|
297 |
} |
|
298 |
||
15504 | 299 |
pub fn leave_room(&mut self, client_id: ClientId) -> Result<LeaveRoomResult, LeaveRoomError> { |
300 |
let client = &mut self.clients[client_id]; |
|
301 |
if let Some(room_id) = client.room_id { |
|
302 |
let room = &mut self.rooms[room_id]; |
|
303 |
||
304 |
room.players_number -= 1; |
|
305 |
client.room_id = None; |
|
306 |
||
307 |
let is_empty = room.players_number == 0; |
|
308 |
let is_fixed = room.is_fixed(); |
|
309 |
let was_master = room.master_id == Some(client_id); |
|
310 |
let was_in_game = client.is_in_game(); |
|
311 |
let mut removed_teams = vec![]; |
|
312 |
||
313 |
if is_empty && !is_fixed { |
|
314 |
if client.is_ready() && room.ready_players_number > 0 { |
|
315 |
room.ready_players_number -= 1; |
|
316 |
} |
|
317 |
||
318 |
removed_teams = room |
|
319 |
.client_teams(client.id) |
|
320 |
.map(|t| t.name.clone()) |
|
321 |
.collect(); |
|
322 |
||
323 |
for team_name in &removed_teams { |
|
324 |
room.remove_team(team_name); |
|
325 |
} |
|
326 |
||
327 |
if client.is_master() && !is_fixed { |
|
328 |
client.set_is_master(false); |
|
329 |
room.master_id = None; |
|
330 |
} |
|
331 |
} |
|
332 |
||
333 |
client.set_is_ready(false); |
|
334 |
client.set_is_in_game(false); |
|
335 |
||
336 |
if !is_fixed { |
|
337 |
if room.players_number == 0 { |
|
338 |
self.rooms.remove(room_id); |
|
339 |
} else if room.master_id == None { |
|
340 |
let new_master_id = self.room_clients(room_id).next(); |
|
341 |
if let Some(new_master_id) = new_master_id { |
|
342 |
let room = &mut self.rooms[room_id]; |
|
343 |
room.master_id = Some(new_master_id); |
|
344 |
let new_master = &mut self.clients[new_master_id]; |
|
345 |
new_master.set_is_master(true); |
|
346 |
||
347 |
if room.protocol_number < 42 { |
|
348 |
room.name = new_master.nick.clone(); |
|
349 |
} |
|
350 |
||
351 |
room.set_join_restriction(false); |
|
352 |
room.set_team_add_restriction(false); |
|
353 |
room.set_unregistered_players_restriction(true); |
|
354 |
} |
|
355 |
} |
|
356 |
} |
|
357 |
||
358 |
if is_empty && !is_fixed { |
|
359 |
Ok(LeaveRoomResult::RoomRemoved) |
|
360 |
} else { |
|
361 |
Ok(LeaveRoomResult::RoomRemains { |
|
362 |
is_empty, |
|
363 |
was_master, |
|
364 |
was_in_game, |
|
365 |
new_master: self.rooms[room_id].master_id, |
|
366 |
removed_teams, |
|
367 |
}) |
|
368 |
} |
|
369 |
} else { |
|
370 |
Err(LeaveRoomError::NoRoom) |
|
371 |
} |
|
372 |
} |
|
373 |
||
15509 | 374 |
pub fn change_master( |
375 |
&mut self, |
|
376 |
client_id: ClientId, |
|
377 |
room_id: RoomId, |
|
378 |
new_master_nick: String, |
|
379 |
) -> Result<ChangeMasterResult, ChangeMasterError> { |
|
380 |
let client = &mut self.clients[client_id]; |
|
381 |
let room = &mut self.rooms[room_id]; |
|
382 |
||
383 |
if client.is_admin() || room.master_id == Some(client_id) { |
|
384 |
let new_master_id = self |
|
385 |
.clients |
|
386 |
.iter() |
|
387 |
.find(|(_, c)| c.nick == new_master_nick) |
|
388 |
.map(|(id, _)| id); |
|
389 |
||
390 |
match new_master_id { |
|
391 |
Some(new_master_id) if new_master_id == client_id => { |
|
392 |
Err(ChangeMasterError::AlreadyMaster) |
|
393 |
} |
|
394 |
Some(new_master_id) => { |
|
395 |
let new_master = &mut self.clients[new_master_id]; |
|
396 |
if new_master.room_id == Some(room_id) { |
|
397 |
self.clients[new_master_id].set_is_master(true); |
|
398 |
let old_master_id = room.master_id; |
|
399 |
if let Some(master_id) = old_master_id { |
|
400 |
self.clients[master_id].set_is_master(false); |
|
401 |
} |
|
402 |
room.master_id = Some(new_master_id); |
|
403 |
Ok(ChangeMasterResult { |
|
404 |
old_master_id, |
|
405 |
new_master_id, |
|
406 |
}) |
|
407 |
} else { |
|
408 |
Err(ChangeMasterError::ClientNotInRoom) |
|
409 |
} |
|
410 |
} |
|
411 |
None => Err(ChangeMasterError::NoClient), |
|
412 |
} |
|
413 |
} else { |
|
414 |
Err(ChangeMasterError::NoAccess) |
|
415 |
} |
|
416 |
} |
|
417 |
||
15514 | 418 |
pub fn start_game(&mut self, room_id: RoomId) -> Result<Vec<String>, StartGameError> { |
419 |
let (room_clients, room_nicks): (Vec<_>, Vec<_>) = self |
|
420 |
.clients |
|
421 |
.iter() |
|
422 |
.map(|(id, c)| (id, c.nick.clone())) |
|
423 |
.unzip(); |
|
424 |
||
425 |
let room = &mut self.rooms[room_id]; |
|
426 |
||
427 |
if !room.has_multiple_clans() { |
|
428 |
Err(StartGameError::NotEnoughClans) |
|
429 |
} else if room.protocol_number <= 43 && room.players_number != room.ready_players_number { |
|
430 |
Err(StartGameError::NotReady) |
|
431 |
} else if room.game_info.is_some() { |
|
432 |
Err(StartGameError::AlreadyInGame) |
|
433 |
} else { |
|
434 |
room.start_round(); |
|
435 |
for id in room_clients { |
|
436 |
let c = &mut self.clients[id]; |
|
437 |
c.set_is_in_game(true); |
|
438 |
c.team_indices = room.client_team_indices(c.id); |
|
439 |
} |
|
440 |
Ok(room_nicks) |
|
441 |
} |
|
442 |
} |
|
443 |
||
15463 | 444 |
#[inline] |
445 |
pub fn set_var(&mut self, client_id: ClientId, var: ServerVar) -> Result<(), AccessError> { |
|
446 |
if self.clients[client_id].is_admin() { |
|
447 |
match var { |
|
448 |
ServerVar::MOTDNew(msg) => self.greetings.for_latest_protocol = msg, |
|
449 |
ServerVar::MOTDOld(msg) => self.greetings.for_old_protocols = msg, |
|
450 |
ServerVar::LatestProto(n) => self.latest_protocol = n, |
|
451 |
} |
|
452 |
Ok(()) |
|
453 |
} else { |
|
454 |
Err(AccessError()) |
|
455 |
} |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
456 |
} |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14525
diff
changeset
|
457 |
|
15463 | 458 |
#[inline] |
459 |
pub fn get_vars(&self, client_id: ClientId) -> Result<[ServerVar; 3], AccessError> { |
|
460 |
if self.clients[client_id].is_admin() { |
|
461 |
Ok([ |
|
462 |
ServerVar::MOTDNew(self.greetings.for_latest_protocol.clone()), |
|
463 |
ServerVar::MOTDOld(self.greetings.for_old_protocols.clone()), |
|
464 |
ServerVar::LatestProto(self.latest_protocol), |
|
465 |
]) |
|
466 |
} else { |
|
467 |
Err(AccessError()) |
|
468 |
} |
|
469 |
} |
|
470 |
||
471 |
pub fn get_used_protocols(&self, client_id: ClientId) -> Result<Vec<u16>, AccessError> { |
|
472 |
if self.clients[client_id].is_admin() { |
|
473 |
let mut protocols: HashSet<_> = self |
|
474 |
.clients |
|
475 |
.iter() |
|
476 |
.map(|(_, c)| c.protocol_number) |
|
477 |
.chain(self.rooms.iter().map(|(_, r)| r.protocol_number)) |
|
478 |
.collect(); |
|
479 |
let mut protocols: Vec<_> = protocols.drain().collect(); |
|
480 |
protocols.sort(); |
|
481 |
Ok(protocols) |
|
482 |
} else { |
|
483 |
Err(AccessError()) |
|
484 |
} |
|
485 |
} |
|
486 |
||
487 |
#[inline] |
|
13416 | 488 |
pub fn has_room(&self, name: &str) -> bool { |
14718 | 489 |
self.find_room(name).is_some() |
13416 | 490 |
} |
491 |
||
15463 | 492 |
#[inline] |
15096 | 493 |
pub fn find_room(&self, name: &str) -> Option<&HwRoom> { |
14478 | 494 |
self.rooms |
495 |
.iter() |
|
496 |
.find_map(|(_, r)| Some(r).filter(|r| r.name == name)) |
|
13416 | 497 |
} |
498 |
||
15096 | 499 |
pub fn find_room_mut(&mut self, name: &str) -> Option<&mut HwRoom> { |
14478 | 500 |
self.rooms |
501 |
.iter_mut() |
|
502 |
.find_map(|(_, r)| Some(r).filter(|r| r.name == name)) |
|
13416 | 503 |
} |
504 |
||
15096 | 505 |
pub fn find_client(&self, nick: &str) -> Option<&HwClient> { |
14478 | 506 |
self.clients |
507 |
.iter() |
|
508 |
.find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) |
|
13450 | 509 |
} |
510 |
||
15096 | 511 |
pub fn find_client_mut(&mut self, nick: &str) -> Option<&mut HwClient> { |
14478 | 512 |
self.clients |
513 |
.iter_mut() |
|
514 |
.find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) |
|
13450 | 515 |
} |
516 |
||
14810 | 517 |
pub fn all_clients(&self) -> impl Iterator<Item = ClientId> + '_ { |
518 |
self.clients.iter().map(|(id, _)| id) |
|
519 |
} |
|
520 |
||
521 |
pub fn filter_clients<'a, F>(&'a self, f: F) -> impl Iterator<Item = ClientId> + 'a |
|
522 |
where |
|
15096 | 523 |
F: Fn(&(usize, &HwClient)) -> bool + 'a, |
14810 | 524 |
{ |
525 |
self.clients.iter().filter(f).map(|(_, c)| c.id) |
|
526 |
} |
|
527 |
||
528 |
pub fn filter_rooms<'a, F>(&'a self, f: F) -> impl Iterator<Item = RoomId> + 'a |
|
529 |
where |
|
15096 | 530 |
F: Fn(&(usize, &HwRoom)) -> bool + 'a, |
14810 | 531 |
{ |
532 |
self.rooms.iter().filter(f).map(|(_, c)| c.id) |
|
533 |
} |
|
534 |
||
14802 | 535 |
pub fn collect_clients<F>(&self, f: F) -> Vec<ClientId> |
14478 | 536 |
where |
15096 | 537 |
F: Fn(&(usize, &HwClient)) -> bool, |
14478 | 538 |
{ |
14810 | 539 |
self.filter_clients(f).collect() |
13416 | 540 |
} |
541 |
||
14802 | 542 |
pub fn collect_nicks<F>(&self, f: F) -> Vec<String> |
543 |
where |
|
15096 | 544 |
F: Fn(&(usize, &HwClient)) -> bool, |
14802 | 545 |
{ |
546 |
self.clients |
|
547 |
.iter() |
|
548 |
.filter(f) |
|
549 |
.map(|(_, c)| c.nick.clone()) |
|
550 |
.collect() |
|
14715 | 551 |
} |
552 |
||
14810 | 553 |
pub fn lobby_clients(&self) -> impl Iterator<Item = ClientId> + '_ { |
554 |
self.filter_clients(|(_, c)| c.room_id == None) |
|
14802 | 555 |
} |
556 |
||
14810 | 557 |
pub fn room_clients(&self, room_id: RoomId) -> impl Iterator<Item = ClientId> + '_ { |
558 |
self.filter_clients(move |(_, c)| c.room_id == Some(room_id)) |
|
13416 | 559 |
} |
560 |
||
14810 | 561 |
pub fn protocol_clients(&self, protocol: u16) -> impl Iterator<Item = ClientId> + '_ { |
562 |
self.filter_clients(move |(_, c)| c.protocol_number == protocol) |
|
563 |
} |
|
564 |
||
565 |
pub fn protocol_rooms(&self, protocol: u16) -> impl Iterator<Item = RoomId> + '_ { |
|
566 |
self.filter_rooms(move |(_, r)| r.protocol_number == protocol) |
|
13416 | 567 |
} |
568 |
||
569 |
pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> { |
|
570 |
let room_id = self.clients[self_id].room_id; |
|
14802 | 571 |
self.collect_clients(|(id, c)| *id != self_id && c.room_id == room_id) |
13416 | 572 |
} |
14807
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
573 |
|
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
574 |
pub fn is_registered_only(&self) -> bool { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
575 |
self.flags.contains(ServerFlags::REGISTERED_ONLY) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
576 |
} |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
577 |
|
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
578 |
pub fn set_is_registered_only(&mut self, value: bool) { |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
579 |
self.flags.set(ServerFlags::REGISTERED_ONLY, value) |
8ecdb5c6bb2a
implement info, registered only & super power messages
alfadur
parents:
14804
diff
changeset
|
580 |
} |
13445
d3c86ade3d4d
Send the rnd reply to the room only.
Marcin Mielniczuk <marmistrz.dev@zoho.eu>
parents:
13442
diff
changeset
|
581 |
} |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
582 |
|
15096 | 583 |
fn allocate_room(rooms: &mut Slab<HwRoom>) -> &mut HwRoom { |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
584 |
let entry = rooms.vacant_entry(); |
15096 | 585 |
let room = HwRoom::new(entry.key()); |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
586 |
entry.insert(room) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
587 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
588 |
|
15463 | 589 |
fn create_room<'a, 'b>( |
590 |
client: &'a mut HwClient, |
|
591 |
rooms: &'b mut Slab<HwRoom>, |
|
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
592 |
name: String, |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
593 |
password: Option<String>, |
15463 | 594 |
) -> (&'a HwClient, &'b HwRoom) { |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
595 |
let room = allocate_room(rooms); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
596 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
597 |
room.master_id = Some(client.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
598 |
room.name = name; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
599 |
room.password = password; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
600 |
room.protocol_number = client.protocol_number; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
601 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
602 |
room.players_number = 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
603 |
room.ready_players_number = 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
604 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
605 |
client.room_id = Some(room.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
606 |
client.set_is_master(true); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
607 |
client.set_is_ready(true); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
608 |
client.set_is_joined_mid_game(false); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
609 |
|
15463 | 610 |
(client, room) |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
611 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
612 |
|
15096 | 613 |
fn move_to_room(client: &mut HwClient, room: &mut HwRoom) { |
14525
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
614 |
debug_assert!(client.room_id != Some(room.id)); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
615 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
616 |
room.players_number += 1; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
617 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
618 |
client.room_id = Some(room.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
619 |
client.set_is_joined_mid_game(room.game_info.is_some()); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
620 |
client.set_is_in_game(room.game_info.is_some()); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
621 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
622 |
if let Some(ref mut info) = room.game_info { |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
623 |
let teams = info.client_teams(client.id); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
624 |
client.teams_in_game = teams.clone().count() as u8; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
625 |
client.clan = teams.clone().next().map(|t| t.color); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
626 |
let team_names: Vec<_> = teams.map(|t| t.name.clone()).collect(); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
627 |
|
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
628 |
if !team_names.is_empty() { |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
629 |
info.left_teams.retain(|name| !team_names.contains(&name)); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
630 |
info.teams_in_game += team_names.len() as u8; |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
631 |
room.teams = info |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
632 |
.teams_at_start |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
633 |
.iter() |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
634 |
.filter(|(_, t)| !team_names.contains(&t.name)) |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
635 |
.cloned() |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
636 |
.collect(); |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
637 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
638 |
} |
6cc0fce249f9
Server action refactoring part 1 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
639 |
} |