naia_server/
user.rs

1use std::{
2    collections::{hash_set::Iter, HashSet},
3    hash::Hash,
4    net::SocketAddr,
5};
6
7use naia_shared::BigMapKey;
8
9use crate::{RoomKey, Server};
10
11// UserKey
12#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
13pub struct UserKey(u64);
14
15impl BigMapKey for UserKey {
16    fn to_u64(&self) -> u64 {
17        self.0
18    }
19
20    fn from_u64(value: u64) -> Self {
21        UserKey(value)
22    }
23}
24
25// UserAuthAddr
26#[derive(Clone, Debug)]
27pub struct UserAuthAddr {
28    addr: SocketAddr,
29}
30
31impl UserAuthAddr {
32    pub fn new(addr: SocketAddr) -> Self {
33        Self { addr }
34    }
35
36    pub fn addr(&self) -> SocketAddr {
37        self.addr
38    }
39}
40
41// User
42
43#[derive(Clone)]
44pub struct User {
45    auth_addr: Option<UserAuthAddr>,
46    data_addr: Option<SocketAddr>,
47    rooms_cache: HashSet<RoomKey>,
48}
49
50impl User {
51    pub fn new(auth_addr: UserAuthAddr) -> User {
52        Self {
53            auth_addr: Some(auth_addr),
54            data_addr: None,
55            rooms_cache: HashSet::new(),
56        }
57    }
58
59    pub fn has_address(&self) -> bool {
60        self.data_addr.is_some()
61    }
62
63    pub fn address(&self) -> SocketAddr {
64        self.data_addr.unwrap()
65    }
66
67    pub fn address_opt(&self) -> Option<SocketAddr> {
68        self.data_addr
69    }
70
71    pub(crate) fn take_auth_address(&mut self) -> UserAuthAddr {
72        self.auth_addr.take().unwrap()
73    }
74
75    pub(crate) fn set_address(&mut self, addr: &SocketAddr) {
76        self.data_addr = Some(*addr);
77    }
78
79    pub(crate) fn cache_room(&mut self, room_key: &RoomKey) {
80        self.rooms_cache.insert(*room_key);
81    }
82
83    pub(crate) fn uncache_room(&mut self, room_key: &RoomKey) {
84        self.rooms_cache.remove(room_key);
85    }
86
87    pub(crate) fn room_keys(&self) -> &HashSet<RoomKey> {
88        &self.rooms_cache
89    }
90
91    pub(crate) fn room_count(&self) -> usize {
92        self.rooms_cache.len()
93    }
94}
95
96// UserRef
97
98pub struct UserRef<'s, E: Copy + Eq + Hash + Send + Sync> {
99    server: &'s Server<E>,
100    key: UserKey,
101}
102
103impl<'s, E: Copy + Eq + Hash + Send + Sync> UserRef<'s, E> {
104    pub fn new(server: &'s Server<E>, key: &UserKey) -> Self {
105        UserRef { server, key: *key }
106    }
107
108    pub fn key(&self) -> UserKey {
109        self.key
110    }
111
112    pub fn address(&self) -> SocketAddr {
113        self.server.user_address(&self.key).unwrap()
114    }
115
116    pub fn room_count(&self) -> usize {
117        self.server.user_rooms_count(&self.key).unwrap()
118    }
119
120    /// Returns an iterator of all the keys of the [`Room`]s the User belongs to
121    pub fn room_keys(&self) -> impl Iterator<Item = &RoomKey> {
122        self.server.user_room_keys(&self.key).unwrap()
123    }
124}
125
126// UserMut
127pub struct UserMut<'s, E: Copy + Eq + Hash + Send + Sync> {
128    server: &'s mut Server<E>,
129    key: UserKey,
130}
131
132impl<'s, E: Copy + Eq + Hash + Send + Sync> UserMut<'s, E> {
133    pub fn new(server: &'s mut Server<E>, key: &UserKey) -> Self {
134        UserMut { server, key: *key }
135    }
136
137    pub fn key(&self) -> UserKey {
138        self.key
139    }
140
141    pub fn address(&self) -> SocketAddr {
142        self.server.user_address(&self.key).unwrap()
143    }
144
145    pub fn disconnect(&mut self) {
146        self.server.user_queue_disconnect(&self.key);
147    }
148
149    // Rooms
150
151    pub fn enter_room(&mut self, room_key: &RoomKey) -> &mut Self {
152        self.server.room_add_user(room_key, &self.key);
153
154        self
155    }
156
157    pub fn leave_room(&mut self, room_key: &RoomKey) -> &mut Self {
158        self.server.room_remove_user(room_key, &self.key);
159
160        self
161    }
162
163    pub fn room_count(&self) -> usize {
164        self.server.user_rooms_count(&self.key).unwrap()
165    }
166
167    /// Returns an iterator of all the keys of the [`Room`]s the User belongs to
168    pub fn room_keys(&self) -> Iter<RoomKey> {
169        self.server.user_room_keys(&self.key).unwrap()
170    }
171}