From 4c6fbfe66b924e0d0f9ef169bd7a762f45859af3 Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Mon, 5 Aug 2024 02:05:14 -0400 Subject: [PATCH] cleanup --- server/src/user.rs | 47 +++++++++++++++++++++++++++++++++++++++++ server/src/websocket.rs | 46 +--------------------------------------- 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/server/src/user.rs b/server/src/user.rs index 734729c..ace29b6 100644 --- a/server/src/user.rs +++ b/server/src/user.rs @@ -1,7 +1,13 @@ +use crate::meta::*; use crate::AppState; +use anyhow::Result; +use axum::extract::ws::{Message, WebSocket}; +use futures::stream::SplitSink; +use futures::SinkExt; use lib::*; use rand::prelude::SliceRandom; use serde_json::to_string; +use std::net::SocketAddr; use std::sync::Arc; use std::sync::RwLock; @@ -35,3 +41,44 @@ pub fn user_client_self_update(new_user: &Arc>) -> String { }) .unwrap() } + +/// Create, Register, and Hydrate new user +pub async fn user_handle_new( + sender: &mut SplitSink, + state: &Arc, + addr: &SocketAddr, +) -> Result<()> { + // Create + let new_user = Arc::new(RwLock::new(User::new(state))); + + // Notify client of new username + sender + .send(Message::Text(user_client_self_update(&new_user))) + .await?; + + // Register using `addr` as key until something longer lived exists + state.online_users.write().unwrap().insert(*addr, new_user); + + // Hydrate client + // this should probably be combined and sent as one + sender.send(Message::Text(meta_chat_update(state))).await?; + sender.send(Message::Text(meta_motd())).await?; + sender + .send(Message::Text(meta_server_summary_update(state))) + .await?; + sender + .send(Message::Text(meta_games_browser_update(state))) + .await?; + sender + .send(Message::Text(meta_new_game_card_packs(state))) + .await?; + + // Broadcast new user's existence + // this should probably be combined and sent as one + state.tx.send(meta_announce_user_join(state, addr))?; + state.tx.send(meta_server_summary_update(state))?; + state.tx.send(meta_chat_update(state))?; + + Ok(()) +} + diff --git a/server/src/websocket.rs b/server/src/websocket.rs index bfbf571..6008a51 100644 --- a/server/src/websocket.rs +++ b/server/src/websocket.rs @@ -12,14 +12,10 @@ use axum::{ }, response::IntoResponse, }; -use futures::stream::SplitSink; use futures::{SinkExt, StreamExt}; use lib::*; use serde_json::{from_str, to_string}; -use std::{ - net::SocketAddr, - sync::{Arc, RwLock}, -}; +use std::{net::SocketAddr, sync::Arc}; use tokio::sync::broadcast::Sender; /// Establish the WebSocket connection @@ -71,46 +67,6 @@ pub async fn websocket_on_connection(stream: WebSocket, state: Arc, ad }; } -/// Create, Register, and Hydrate new user -pub async fn user_handle_new( - sender: &mut SplitSink, - state: &Arc, - addr: &SocketAddr, -) -> Result<()> { - // Create - let new_user = Arc::new(RwLock::new(User::new(state))); - - // Notify client of new username - sender - .send(Message::Text(user_client_self_update(&new_user))) - .await?; - - // Register using `addr` as key until something longer lived exists - state.online_users.write().unwrap().insert(*addr, new_user); - - // Hydrate client - // this should probably be combined and sent as one - sender.send(Message::Text(meta_chat_update(state))).await?; - sender.send(Message::Text(meta_motd())).await?; - sender - .send(Message::Text(meta_server_summary_update(state))) - .await?; - sender - .send(Message::Text(meta_games_browser_update(state))) - .await?; - sender - .send(Message::Text(meta_new_game_card_packs(state))) - .await?; - - // Broadcast new user's existence - // this should probably be combined and sent as one - state.tx.send(meta_announce_user_join(state, addr))?; - state.tx.send(meta_server_summary_update(state))?; - state.tx.send(meta_chat_update(state))?; - - Ok(()) -} - /// Handle incoming messages over the WebSocket pub async fn websocket_message_handler( state: Arc,