naia_shared/world/entity/
entity_auth_event.rs

1use std::hash::Hash;
2
3use naia_derive::MessageInternal;
4use naia_serde::SerdeInternal;
5
6use crate::{
7    EntityAndGlobalEntityConverter, EntityAuthStatus, EntityProperty, EntityResponseEvent,
8    HostEntity, RemoteEntity,
9};
10
11#[derive(MessageInternal)]
12pub struct EntityEventMessage {
13    pub entity: EntityProperty,
14    pub action: EntityEventMessageAction,
15}
16
17#[derive(SerdeInternal, Clone, Debug, PartialEq)]
18pub enum EntityEventMessageAction {
19    Publish,
20    Unpublish,
21    EnableDelegation,
22    EnableDelegationResponse,
23    DisableDelegation,
24    RequestAuthority(u16),
25    ReleaseAuthority,
26    UpdateAuthority(EntityAuthStatus),
27    EntityMigrateResponse(u16), //u16 here is new Host Entity
28}
29
30impl EntityEventMessageAction {
31    pub fn to_response_event<E: Copy>(&self, entity: &E) -> EntityResponseEvent<E> {
32        match self {
33            EntityEventMessageAction::Publish => EntityResponseEvent::PublishEntity(*entity),
34            EntityEventMessageAction::Unpublish => EntityResponseEvent::UnpublishEntity(*entity),
35            EntityEventMessageAction::EnableDelegation => {
36                EntityResponseEvent::EnableDelegationEntity(*entity)
37            }
38            EntityEventMessageAction::EnableDelegationResponse => {
39                EntityResponseEvent::EnableDelegationEntityResponse(*entity)
40            }
41            EntityEventMessageAction::DisableDelegation => {
42                EntityResponseEvent::DisableDelegationEntity(*entity)
43            }
44            EntityEventMessageAction::RequestAuthority(remote_entity) => {
45                EntityResponseEvent::EntityRequestAuthority(
46                    *entity,
47                    RemoteEntity::new(*remote_entity),
48                )
49            }
50            EntityEventMessageAction::ReleaseAuthority => {
51                EntityResponseEvent::EntityReleaseAuthority(*entity)
52            }
53            EntityEventMessageAction::UpdateAuthority(new_auth_status) => {
54                EntityResponseEvent::EntityUpdateAuthority(*entity, *new_auth_status)
55            }
56            EntityEventMessageAction::EntityMigrateResponse(remote_entity) => {
57                EntityResponseEvent::EntityMigrateResponse(
58                    *entity,
59                    RemoteEntity::new(*remote_entity),
60                )
61            }
62        }
63    }
64}
65
66impl EntityEventMessage {
67    pub fn new_publish<E: Copy + Eq + Hash + Send + Sync>(
68        converter: &dyn EntityAndGlobalEntityConverter<E>,
69        entity: &E,
70    ) -> Self {
71        Self::new(converter, entity, EntityEventMessageAction::Publish)
72    }
73
74    pub fn new_unpublish<E: Copy + Eq + Hash + Send + Sync>(
75        converter: &dyn EntityAndGlobalEntityConverter<E>,
76        entity: &E,
77    ) -> Self {
78        Self::new(converter, entity, EntityEventMessageAction::Unpublish)
79    }
80
81    pub fn new_enable_delegation<E: Copy + Eq + Hash + Send + Sync>(
82        converter: &dyn EntityAndGlobalEntityConverter<E>,
83        entity: &E,
84    ) -> Self {
85        Self::new(
86            converter,
87            entity,
88            EntityEventMessageAction::EnableDelegation,
89        )
90    }
91
92    pub fn new_enable_delegation_response<E: Copy + Eq + Hash + Send + Sync>(
93        converter: &dyn EntityAndGlobalEntityConverter<E>,
94        entity: &E,
95    ) -> Self {
96        Self::new(
97            converter,
98            entity,
99            EntityEventMessageAction::EnableDelegationResponse,
100        )
101    }
102
103    pub fn new_disable_delegation<E: Copy + Eq + Hash + Send + Sync>(
104        converter: &dyn EntityAndGlobalEntityConverter<E>,
105        entity: &E,
106    ) -> Self {
107        Self::new(
108            converter,
109            entity,
110            EntityEventMessageAction::DisableDelegation,
111        )
112    }
113
114    pub fn new_request_authority<E: Copy + Eq + Hash + Send + Sync>(
115        converter: &dyn EntityAndGlobalEntityConverter<E>,
116        entity: &E,
117        host_entity: HostEntity,
118    ) -> Self {
119        Self::new(
120            converter,
121            entity,
122            EntityEventMessageAction::RequestAuthority(host_entity.value()),
123        )
124    }
125
126    pub fn new_release_authority<E: Copy + Eq + Hash + Send + Sync>(
127        converter: &dyn EntityAndGlobalEntityConverter<E>,
128        entity: &E,
129    ) -> Self {
130        Self::new(
131            converter,
132            entity,
133            EntityEventMessageAction::ReleaseAuthority,
134        )
135    }
136
137    pub fn new_update_auth_status<E: Copy + Eq + Hash + Send + Sync>(
138        converter: &dyn EntityAndGlobalEntityConverter<E>,
139        entity: &E,
140        auth_status: EntityAuthStatus,
141    ) -> Self {
142        Self::new(
143            converter,
144            entity,
145            EntityEventMessageAction::UpdateAuthority(auth_status),
146        )
147    }
148
149    pub fn new_entity_migrate_response<E: Copy + Eq + Hash + Send + Sync>(
150        converter: &dyn EntityAndGlobalEntityConverter<E>,
151        entity: &E,
152        host_entity: HostEntity,
153    ) -> Self {
154        Self::new(
155            converter,
156            entity,
157            EntityEventMessageAction::EntityMigrateResponse(host_entity.value()),
158        )
159    }
160
161    fn new<E: Copy + Eq + Hash + Send + Sync>(
162        converter: &dyn EntityAndGlobalEntityConverter<E>,
163        entity: &E,
164        action: EntityEventMessageAction,
165    ) -> Self {
166        let mut output = Self {
167            entity: EntityProperty::new(),
168            action,
169        };
170
171        output.entity.set(converter, entity);
172
173        output
174    }
175}