159 } |
159 } |
160 |
160 |
161 pub struct HwServer { |
161 pub struct HwServer { |
162 clients: IndexSlab<HwClient>, |
162 clients: IndexSlab<HwClient>, |
163 rooms: Slab<HwRoom>, |
163 rooms: Slab<HwRoom>, |
164 pub latest_protocol: u16, |
164 latest_protocol: u16, |
165 pub flags: ServerFlags, |
165 flags: ServerFlags, |
166 pub greetings: ServerGreetings, |
166 greetings: ServerGreetings, |
167 } |
167 } |
168 |
168 |
169 impl HwServer { |
169 impl HwServer { |
170 pub fn new(clients_limit: usize, rooms_limit: usize) -> Self { |
170 pub fn new(clients_limit: usize, rooms_limit: usize) -> Self { |
171 let rooms = Slab::with_capacity(rooms_limit); |
171 let rooms = Slab::with_capacity(rooms_limit); |
203 pub fn get_room(&self, room_id: RoomId) -> Option<&HwRoom> { |
203 pub fn get_room(&self, room_id: RoomId) -> Option<&HwRoom> { |
204 self.rooms.get(room_id) |
204 self.rooms.get(room_id) |
205 } |
205 } |
206 |
206 |
207 #[inline] |
207 #[inline] |
208 pub fn get_room_mut(&mut self, room_id: RoomId) -> Option<&mut HwRoom> { |
208 fn get_room_mut(&mut self, room_id: RoomId) -> Option<&mut HwRoom> { |
209 self.rooms.get_mut(room_id) |
209 self.rooms.get_mut(room_id) |
210 } |
210 } |
211 |
211 |
212 #[inline] |
212 #[inline] |
213 pub fn iter_rooms(&self) -> impl Iterator<Item = &HwRoom> { |
213 pub fn iter_rooms(&self) -> impl Iterator<Item = &HwRoom> { |
414 self.clients |
414 self.clients |
415 .iter() |
415 .iter() |
416 .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) |
416 .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) |
417 } |
417 } |
418 |
418 |
419 pub fn find_client_mut(&mut self, nick: &str) -> Option<&mut HwClient> { |
419 fn find_client_mut(&mut self, nick: &str) -> Option<&mut HwClient> { |
420 self.clients |
420 self.clients |
421 .iter_mut() |
421 .iter_mut() |
422 .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) |
422 .find_map(|(_, c)| Some(c).filter(|c| c.nick == nick)) |
423 } |
423 } |
424 |
424 |
425 pub fn all_clients(&self) -> impl Iterator<Item = ClientId> + '_ { |
425 pub fn iter_client_ids(&self) -> impl Iterator<Item = ClientId> + '_ { |
426 self.clients.iter().map(|(id, _)| id) |
426 self.clients.iter().map(|(id, _)| id) |
427 } |
427 } |
428 |
428 |
429 pub fn filter_clients<'a, F>(&'a self, f: F) -> impl Iterator<Item = ClientId> + 'a |
429 pub fn filter_clients<'a, F>(&'a self, f: F) -> impl Iterator<Item = ClientId> + 'a |
430 where |
430 where |
438 F: Fn(&(usize, &HwRoom)) -> bool + 'a, |
438 F: Fn(&(usize, &HwRoom)) -> bool + 'a, |
439 { |
439 { |
440 self.rooms.iter().filter(f).map(|(_, c)| c.id) |
440 self.rooms.iter().filter(f).map(|(_, c)| c.id) |
441 } |
441 } |
442 |
442 |
443 pub fn collect_clients<F>(&self, f: F) -> Vec<ClientId> |
443 pub fn collect_client_ids<F>(&self, f: F) -> Vec<ClientId> |
444 where |
444 where |
445 F: Fn(&(usize, &HwClient)) -> bool, |
445 F: Fn(&(usize, &HwClient)) -> bool, |
446 { |
446 { |
447 self.filter_clients(f).collect() |
447 self.filter_clients(f).collect() |
448 } |
448 } |
456 .filter(f) |
456 .filter(f) |
457 .map(|(_, c)| c.nick.clone()) |
457 .map(|(_, c)| c.nick.clone()) |
458 .collect() |
458 .collect() |
459 } |
459 } |
460 |
460 |
461 pub fn lobby_clients(&self) -> impl Iterator<Item = ClientId> + '_ { |
461 pub fn lobby_client_ids(&self) -> impl Iterator<Item = ClientId> + '_ { |
462 self.filter_clients(|(_, c)| c.room_id == None) |
462 self.filter_clients(|(_, c)| c.room_id == None) |
463 } |
463 } |
464 |
464 |
465 pub fn room_clients(&self, room_id: RoomId) -> impl Iterator<Item = ClientId> + '_ { |
465 pub fn room_client_ids(&self, room_id: RoomId) -> impl Iterator<Item = ClientId> + '_ { |
466 self.filter_clients(move |(_, c)| c.room_id == Some(room_id)) |
466 self.filter_clients(move |(_, c)| c.room_id == Some(room_id)) |
467 } |
467 } |
468 |
468 |
469 pub fn protocol_clients(&self, protocol: u16) -> impl Iterator<Item = ClientId> + '_ { |
469 pub fn protocol_client_ids(&self, protocol: u16) -> impl Iterator<Item = ClientId> + '_ { |
470 self.filter_clients(move |(_, c)| c.protocol_number == protocol) |
470 self.filter_clients(move |(_, c)| c.protocol_number == protocol) |
471 } |
471 } |
472 |
472 |
473 pub fn protocol_rooms(&self, protocol: u16) -> impl Iterator<Item = RoomId> + '_ { |
473 pub fn protocol_room_ids(&self, protocol: u16) -> impl Iterator<Item = RoomId> + '_ { |
474 self.filter_rooms(move |(_, r)| r.protocol_number == protocol) |
474 self.filter_rooms(move |(_, r)| r.protocol_number == protocol) |
475 } |
475 } |
476 |
476 |
477 pub fn other_clients_in_room(&self, self_id: ClientId) -> Vec<ClientId> { |
477 pub fn other_client_ids_in_room(&self, self_id: ClientId) -> Vec<ClientId> { |
478 let room_id = self.clients[self_id].room_id; |
478 let room_id = self.clients[self_id].room_id; |
479 self.collect_clients(|(id, c)| *id != self_id && c.room_id == room_id) |
479 self.collect_client_ids(|(id, c)| *id != self_id && c.room_id == room_id) |
480 } |
480 } |
481 |
481 |
482 pub fn is_registered_only(&self) -> bool { |
482 pub fn is_registered_only(&self) -> bool { |
483 self.flags.contains(ServerFlags::REGISTERED_ONLY) |
483 self.flags.contains(ServerFlags::REGISTERED_ONLY) |
484 } |
484 } |
485 |
485 |
486 pub fn set_is_registered_only(&mut self, value: bool) { |
486 pub fn set_is_registered_only(&mut self, value: bool) { |
487 self.flags.set(ServerFlags::REGISTERED_ONLY, value) |
487 self.flags.set(ServerFlags::REGISTERED_ONLY, value) |
|
488 } |
|
489 |
|
490 pub fn set_room_saves(&mut self, room_id: RoomId, text: &str) -> Result<(), serde_yaml::Error> { |
|
491 if let Some(room) = self.rooms.get_mut(room_id) { |
|
492 room.set_saves(text) |
|
493 } else { |
|
494 Ok(()) |
|
495 } |
488 } |
496 } |
489 } |
497 } |
490 |
498 |
491 pub struct HwRoomControl<'a> { |
499 pub struct HwRoomControl<'a> { |
492 server: &'a mut HwServer, |
500 server: &'a mut HwServer, |
588 if !is_fixed { |
596 if !is_fixed { |
589 if room.players_number == 0 { |
597 if room.players_number == 0 { |
590 self.server.rooms.remove(self.room_id); |
598 self.server.rooms.remove(self.room_id); |
591 } else if room.master_id == None { |
599 } else if room.master_id == None { |
592 let protocol_number = room.protocol_number; |
600 let protocol_number = room.protocol_number; |
593 let new_master_id = self.server.room_clients(self.room_id).next(); |
601 let new_master_id = self.server.room_client_ids(self.room_id).next(); |
594 |
602 |
595 if let Some(new_master_id) = new_master_id { |
603 if let Some(new_master_id) = new_master_id { |
596 let room = self.room_mut(); |
604 let room = self.room_mut(); |
597 room.master_id = Some(new_master_id); |
605 room.master_id = Some(new_master_id); |
598 let new_master = &mut self.server.clients[new_master_id]; |
606 let new_master = &mut self.server.clients[new_master_id]; |
670 pub fn start_vote(&mut self, kind: VoteType) -> Result<(), StartVoteError> { |
678 pub fn start_vote(&mut self, kind: VoteType) -> Result<(), StartVoteError> { |
671 use StartVoteError::*; |
679 use StartVoteError::*; |
672 match self.room().voting { |
680 match self.room().voting { |
673 Some(_) => Err(VotingInProgress), |
681 Some(_) => Err(VotingInProgress), |
674 None => { |
682 None => { |
675 let voting = Voting::new(kind, self.server.room_clients(self.room_id).collect()); |
683 let voting = Voting::new(kind, self.server.room_client_ids(self.room_id).collect()); |
676 self.room_mut().voting = Some(voting); |
684 self.room_mut().voting = Some(voting); |
677 Ok(()) |
685 Ok(()) |
678 } |
686 } |
679 } |
687 } |
680 } |
688 } |