let user_handler own offline_users

This commit is contained in:
Adam 2024-12-11 23:49:03 -05:00
parent a70050fdbd
commit 3b2862b675
3 changed files with 12 additions and 16 deletions

View file

@ -46,7 +46,6 @@ impl User {
/// Shared state
pub struct AppState {
pub offline_users: RwLock<HashMap<String, Arc<RwLock<User>>>>,
pub online_users: RwLock<HashMap<SocketAddr, Arc<RwLock<User>>>>,
pub tx_broadcast: tokio::sync::broadcast::Sender<Message>,
pub tx_game_handler: mpsc::Sender<GameHandlerMessage>,

View file

@ -82,11 +82,9 @@ async fn main() -> Result<()> {
let (tx_incoming_message_handler, mut rx_incoming_message_handler) = mpsc::channel(32);
let (tx_outgoing_message_handler, mut rx_outgoing_message_handler) = mpsc::channel(32);
let (tx_user_handler, mut rx_user_handler) = mpsc::channel(32);
let offline_users = RwLock::new(HashMap::<String, Arc<RwLock<User>>>::new());
let online_users = RwLock::new(HashMap::<SocketAddr, Arc<RwLock<User>>>::new());
let app_state = Arc::new(AppState {
offline_users,
online_users,
tx_broadcast: tx_broadcast.clone(),
tx_game_handler,

View file

@ -24,6 +24,7 @@ pub struct UserHandler {
users_by_id: HashMap<String, Arc<RwLock<User>>>,
name_generator: NameGenerator,
reserved_names: HashSet<String>,
offline_users: HashMap<String, Arc<RwLock<User>>>,
}
pub enum DmUserMethod {
@ -50,7 +51,9 @@ pub enum SendUserMessage {
impl UserHandler {
/// Returns new UserHandler
pub fn new(state: Arc<AppState>) -> Self {
let offline_users = HashMap::<String, Arc<RwLock<User>>>::new();
UserHandler {
offline_users,
state,
users_by_id: HashMap::<String, Arc<RwLock<User>>>::new(),
reserved_names: HashSet::<String>::new(),
@ -225,25 +228,20 @@ impl UserHandler {
if let Some(user) = self.state.online_users.read().unwrap().get(&addr) {
old_name = user.read().unwrap().name.clone();
// User abandons current name by requesting a new one
self.reserved_names.remove(&old_name);
} else {
tracing::error!("Nonexistent user attempting login");
return;
}
// Resume user's old session if they exist as offline
if self
.state
.offline_users
.read()
.unwrap()
.contains_key(&new_name)
{
if self.offline_users.contains_key(&new_name) {
let buf;
// Copy over new tx
if let Some(online_user) = self.state.online_users.write().unwrap().remove(&addr) {
if let Some(offline_user) =
self.state.offline_users.write().unwrap().remove(&new_name)
{
if let Some(offline_user) = self.offline_users.remove(&new_name) {
offline_user.write().unwrap().tx = online_user.write().unwrap().tx.clone();
buf = offline_user;
} else {
@ -292,6 +290,7 @@ impl UserHandler {
Addr(addr),
)
.await;
return;
} else {
// Reserve name
self.reserved_names.insert(new_name.clone());
@ -362,7 +361,7 @@ impl UserHandler {
}
/// Clean up after a user when they disconnect
async fn user_cleanup(&self, addr: SocketAddr) {
async fn user_cleanup(&mut self, addr: SocketAddr) {
let user_name = self
.state
.online_users
@ -403,8 +402,8 @@ impl UserHandler {
}
/// Set user status to offline
fn set_user_offline(&self, user_name: String, addr: &SocketAddr) {
self.state.offline_users.write().unwrap().insert(
fn set_user_offline(&mut self, user_name: String, addr: &SocketAddr) {
self.offline_users.insert(
user_name,
self.state
.online_users