naia_shared/world/delegation/
host_auth_handler.rs1use std::{collections::HashMap, hash::Hash};
2
3use crate::{
4 world::delegation::{
5 auth_channel::{EntityAuthAccessor, EntityAuthChannel, EntityAuthMutator},
6 entity_auth_status::{EntityAuthStatus, HostEntityAuthStatus},
7 },
8 HostType,
9};
10
11pub struct HostAuthHandler<E: Copy + Eq + Hash> {
12 auth_channels: HashMap<E, (EntityAuthMutator, EntityAuthAccessor)>,
13}
14
15impl<E: Copy + Eq + Hash> HostAuthHandler<E> {
16 pub fn new() -> Self {
17 Self {
18 auth_channels: HashMap::new(),
19 }
20 }
21
22 pub fn register_entity(&mut self, host_type: HostType, entity: &E) -> EntityAuthAccessor {
23 if self.auth_channels.contains_key(&entity) {
24 panic!("Entity cannot register with Server more than once!");
25 }
26
27 let (mutator, accessor) = EntityAuthChannel::new_channel(host_type);
28
29 self.auth_channels
30 .insert(*entity, (mutator, accessor.clone()));
31
32 accessor
33 }
34
35 pub fn deregister_entity(&mut self, entity: &E) {
36 self.auth_channels.remove(&entity);
37 }
38
39 pub fn get_accessor(&self, entity: &E) -> EntityAuthAccessor {
40 let (_, receiver) = self
41 .auth_channels
42 .get(&entity)
43 .expect("Entity must be registered with Server before it can receive messages!");
44
45 receiver.clone()
46 }
47
48 pub fn auth_status(&self, entity: &E) -> Option<HostEntityAuthStatus> {
49 if let Some((_, receiver)) = self.auth_channels.get(&entity) {
50 return Some(receiver.auth_status());
51 }
52
53 return None;
54 }
55
56 pub fn set_auth_status(&self, entity: &E, auth_status: EntityAuthStatus) {
57 let (sender, _) = self
58 .auth_channels
59 .get(&entity)
60 .expect("Entity must be registered with Server before it can be mutated!");
61
62 sender.set_auth_status(auth_status);
63 }
64}