use std::{
collections::{hash_set::Iter, HashSet},
hash::Hash,
net::SocketAddr,
};
use naia_shared::{BigMapKey, WorldMutType};
use crate::{RoomKey, Server};
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct UserKey(u64);
impl BigMapKey for UserKey {
fn to_u64(&self) -> u64 {
self.0
}
fn from_u64(value: u64) -> Self {
UserKey(value)
}
}
#[derive(Clone)]
pub struct User {
pub address: SocketAddr,
rooms_cache: HashSet<RoomKey>,
}
impl User {
pub fn new(address: SocketAddr) -> User {
User {
address,
rooms_cache: HashSet::new(),
}
}
pub(crate) fn cache_room(&mut self, room_key: &RoomKey) {
self.rooms_cache.insert(*room_key);
}
pub(crate) fn uncache_room(&mut self, room_key: &RoomKey) {
self.rooms_cache.remove(room_key);
}
pub(crate) fn room_keys(&self) -> Iter<RoomKey> {
self.rooms_cache.iter()
}
pub(crate) fn room_count(&self) -> usize {
self.rooms_cache.len()
}
}
pub struct UserRef<'s, E: Copy + Eq + Hash + Send + Sync> {
server: &'s Server<E>,
key: UserKey,
}
impl<'s, E: Copy + Eq + Hash + Send + Sync> UserRef<'s, E> {
pub fn new(server: &'s Server<E>, key: &UserKey) -> Self {
UserRef { server, key: *key }
}
pub fn key(&self) -> UserKey {
self.key
}
pub fn address(&self) -> SocketAddr {
self.server.user_address(&self.key).unwrap()
}
pub fn room_count(&self) -> usize {
self.server.user_rooms_count(&self.key).unwrap()
}
pub fn room_keys(&self) -> impl Iterator<Item = &RoomKey> {
self.server.user_room_keys(&self.key).unwrap()
}
}
pub struct UserMut<'s, E: Copy + Eq + Hash + Send + Sync> {
server: &'s mut Server<E>,
key: UserKey,
}
impl<'s, E: Copy + Eq + Hash + Send + Sync> UserMut<'s, E> {
pub fn new(server: &'s mut Server<E>, key: &UserKey) -> Self {
UserMut { server, key: *key }
}
pub fn key(&self) -> UserKey {
self.key
}
pub fn address(&self) -> SocketAddr {
self.server.user_address(&self.key).unwrap()
}
pub fn disconnect<W: WorldMutType<E>>(&mut self, mut world: W) {
self.server.user_disconnect(&self.key, &mut world);
}
pub fn enter_room(&mut self, room_key: &RoomKey) -> &mut Self {
self.server.room_add_user(room_key, &self.key);
self
}
pub fn leave_room(&mut self, room_key: &RoomKey) -> &mut Self {
self.server.room_remove_user(room_key, &self.key);
self
}
pub fn room_count(&self) -> usize {
self.server.user_rooms_count(&self.key).unwrap()
}
pub fn room_keys(&self) -> impl Iterator<Item = &RoomKey> {
self.server.user_room_keys(&self.key).unwrap()
}
}