From 9f38eafcb1405b5c5e9184c9fab0b081344ce760 Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Mon, 5 Aug 2024 21:23:28 -0400 Subject: [PATCH] cleanup -- don't break the spaghetti edition --- server/src/lib.rs | 38 +++++++++++++++++++++--------- server/src/main.rs | 16 ++++++------- server/src/websocket.rs | 6 +++-- server/src/{ => websocket}/meta.rs | 0 server/src/{ => websocket}/user.rs | 28 ++-------------------- 5 files changed, 40 insertions(+), 48 deletions(-) rename server/src/{ => websocket}/meta.rs (100%) rename server/src/{ => websocket}/user.rs (74%) diff --git a/server/src/lib.rs b/server/src/lib.rs index 831e450..8c41538 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -3,6 +3,7 @@ use anyhow::{Context, Result}; use lib::*; use rand::prelude::IteratorRandom; +use rand::prelude::SliceRandom; use rand::thread_rng; use serde::Deserialize; use std::{ @@ -14,9 +15,29 @@ use std::{ }; use tokio::sync::broadcast; pub mod websocket; -pub mod meta; -pub mod user; -use crate::user::*; + +/// User +#[derive(Default, Debug, Eq, PartialEq, Hash)] +pub struct User { + pub name: String, +} + +impl User { + /// Create a new user object from incoming data + pub fn new(state: &Arc) -> User { + User { + name: format!( + "{} {}", + state.first_names.choose(&mut rand::thread_rng()).unwrap(), + state.last_names.choose(&mut rand::thread_rng()).unwrap(), + ), + } + } + + pub fn change_name(&mut self, new_name: String) { + self.name = new_name; + } +} /// Card Set #[derive(Debug)] @@ -332,18 +353,13 @@ pub fn load_names(path: &str) -> Result> { // Our shared state pub struct AppState { - // We require unique usernames. This tracks which usernames have been taken. + pub tx: broadcast::Sender, + pub first_names: Vec, + pub last_names: Vec, pub reserved_names: RwLock>, pub online_users: RwLock>>>, pub offline_users: RwLock>>>, - // Channel used to send messages to all connected clients. - pub tx: broadcast::Sender, - // Master card decks pub packs: CardPacks, pub packs_meta: CardPacksMeta, - // Games list pub games: RwLock>, - // chatrooms: Mutex>>, - pub first_names: Vec, - pub last_names: Vec, } diff --git a/server/src/main.rs b/server/src/main.rs index d8e5421..db3ba70 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,7 +1,6 @@ use crate::websocket::*; use anyhow::{Context, Result}; use axum::{routing::get, Router}; -use server::user::*; use server::*; use std::{ collections::{HashMap, HashSet}, @@ -23,30 +22,29 @@ async fn main() -> Result<()> { .with(tracing_subscriber::fmt::layer()) .init(); - // Set up application state for use with with_state(). - // Main Broadcast Channel + // Set up state let (tx, _rx) = broadcast::channel(100); + let first_names = load_names("data/first.txt")?; + let last_names = load_names("data/last.txt")?; let reserved_names = RwLock::new(HashSet::::new()); let online_users = RwLock::new(HashMap::>>::new()); let offline_users = RwLock::new(HashMap::>>::new()); let (packs, packs_meta) = load_cards_from_json("data/cah-cards-full.json")?; let games = RwLock::new(vec![]); - let first_names = load_names("data/first.txt")?; - let last_names = load_names("data/last.txt")?; let app_state = Arc::new(AppState { + tx, + first_names, + last_names, reserved_names, online_users, offline_users, - tx, packs, packs_meta, games, - first_names, - last_names, }); - // set routes and apply state + // Router let app = Router::new() .route("/websocket", get(websocket_connection_handler)) .nest_service("/", ServeDir::new("dist")) diff --git a/server/src/websocket.rs b/server/src/websocket.rs index 6008a51..0a40adb 100644 --- a/server/src/websocket.rs +++ b/server/src/websocket.rs @@ -1,5 +1,5 @@ -use crate::meta::*; -use crate::user::*; +use crate::websocket::meta::*; +use crate::websocket::user::*; use crate::AppState; use crate::Game; use crate::NewGameManifest; @@ -17,6 +17,8 @@ use lib::*; use serde_json::{from_str, to_string}; use std::{net::SocketAddr, sync::Arc}; use tokio::sync::broadcast::Sender; +pub mod meta; +pub mod user; /// Establish the WebSocket connection pub async fn websocket_connection_handler( diff --git a/server/src/meta.rs b/server/src/websocket/meta.rs similarity index 100% rename from server/src/meta.rs rename to server/src/websocket/meta.rs diff --git a/server/src/user.rs b/server/src/websocket/user.rs similarity index 74% rename from server/src/user.rs rename to server/src/websocket/user.rs index ace29b6..de7509f 100644 --- a/server/src/user.rs +++ b/server/src/websocket/user.rs @@ -1,39 +1,16 @@ -use crate::meta::*; +use crate::websocket::meta::*; use crate::AppState; +use crate::User; 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; -/// User -#[derive(Default, Debug, Eq, PartialEq, Hash)] -pub struct User { - pub name: String, -} - -impl User { - /// Create a new user object from incoming data - pub fn new(state: &Arc) -> User { - User { - name: format!( - "{} {}", - state.first_names.choose(&mut rand::thread_rng()).unwrap(), - state.last_names.choose(&mut rand::thread_rng()).unwrap(), - ), - } - } - - pub fn change_name(&mut self, new_name: String) { - self.name = new_name; - } -} - /// Generate message to notify client of user changes pub fn user_client_self_update(new_user: &Arc>) -> String { to_string::(&UserUpdate { @@ -81,4 +58,3 @@ pub async fn user_handle_new( Ok(()) } -