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