diff --git a/server/src/user_handler.rs b/server/src/user_handler.rs index 0eebe2c..b953948 100644 --- a/server/src/user_handler.rs +++ b/server/src/user_handler.rs @@ -1,9 +1,10 @@ -use crate::websocket::meta::*; -use crate::websocket::user::*; use crate::AppState; use crate::NewUser; use crate::User; use anyhow::Result; +use lib::*; +use serde_json::to_string; +use std::net::SocketAddr; use std::sync::{Arc, RwLock}; pub struct UserHandler { @@ -83,3 +84,86 @@ impl UserHandler { Ok(()) } } + +/// Generate message to notify client of user changes +pub fn user_client_self_update(new_user: &Arc>) -> String { + to_string::(&UserUpdate { + username: new_user.read().unwrap().name.clone(), + }) + .unwrap() +} + +/// Generate chatroom metadata update +pub fn meta_chat_update(state: &Arc) -> String { + // this may get expensive if there are many users + let mut names = vec![]; + + for user in state.online_users.read().unwrap().iter() { + names.push(user.1.read().unwrap().name.clone()); + } + + to_string::(&ChatUpdate { + room: "Lobby".to_string(), + users: names, + }) + .unwrap() +} + +/// Generage cards meta message +pub fn meta_new_game_card_packs(state: &Arc) -> String { + to_string::(&state.packs_meta).unwrap() +} + +/// Generate message-of-the-day server greeting +pub fn meta_motd() -> String { + to_string::(&ChatMessage { + text: "Greetings from the game server!".to_string(), + }) + .unwrap() +} + +/// Generate server summary update - mostly debug stuff +pub fn meta_server_summary_update(state: &Arc) -> String { + let online_users = state.online_users.read().unwrap().len(); + let active_games = state.games.read().unwrap().len(); + to_string::(&ServerStateSummary { + online_users, + active_games, + }) + .unwrap() +} + +/// Generate games list update +pub fn meta_games_browser_update(state: &Arc) -> String { + // this may get expensive if there are many games + let mut names = vec![]; + + for game in state.games.read().unwrap().values() { + names.push(format!( + "Name: {} Host: {}", + game.read().unwrap().name, + game.read().unwrap().host.read().unwrap().name + )); + } + + to_string::(&GamesUpdate { games: names }).unwrap() +} + +/// Generate chatroom join announcement +pub fn meta_announce_user_join(state: &Arc, addr: &SocketAddr) -> String { + let msg = format!( + "{} joined.", + state + .online_users + .read() + .unwrap() + .get(addr) + .unwrap() + .read() + .unwrap() + .name + ); + + tracing::debug!("{}", &msg); + to_string::(&ChatMessage { text: msg }).unwrap() +} diff --git a/server/src/websocket.rs b/server/src/websocket.rs index e6e7b6d..15211a6 100644 --- a/server/src/websocket.rs +++ b/server/src/websocket.rs @@ -1,4 +1,4 @@ -use crate::websocket::meta::*; +use crate::user_handler::*; use crate::AppState; use crate::Game; use crate::NewGameManifest; @@ -20,8 +20,6 @@ use std::{ sync::{Arc, RwLock}, }; use tokio::sync::{broadcast::Sender, mpsc}; -pub mod meta; -pub mod user; /// Establish the WebSocket connection pub async fn websocket_connection_handler( diff --git a/server/src/websocket/meta.rs b/server/src/websocket/meta.rs deleted file mode 100644 index 6ceab34..0000000 --- a/server/src/websocket/meta.rs +++ /dev/null @@ -1,79 +0,0 @@ -use crate::AppState; -use lib::*; -use serde_json::to_string; -use std::{net::SocketAddr, sync::Arc}; - -/// Generate chatroom metadata update -pub fn meta_chat_update(state: &Arc) -> String { - // this may get expensive if there are many users - let mut names = vec![]; - - for user in state.online_users.read().unwrap().iter() { - names.push(user.1.read().unwrap().name.clone()); - } - - to_string::(&ChatUpdate { - room: "Lobby".to_string(), - users: names, - }) - .unwrap() -} - -/// Generage cards meta message -pub fn meta_new_game_card_packs(state: &Arc) -> String { - to_string::(&state.packs_meta).unwrap() -} - -/// Generate message-of-the-day server greeting -pub fn meta_motd() -> String { - to_string::(&ChatMessage { - text: "Greetings from the game server!".to_string(), - }) - .unwrap() -} - -/// Generate server summary update - mostly debug stuff -pub fn meta_server_summary_update(state: &Arc) -> String { - let online_users = state.online_users.read().unwrap().len(); - let active_games = state.games.read().unwrap().len(); - to_string::(&ServerStateSummary { - online_users, - active_games, - }) - .unwrap() -} - -/// Generate games list update -pub fn meta_games_browser_update(state: &Arc) -> String { - // this may get expensive if there are many games - let mut names = vec![]; - - for game in state.games.read().unwrap().values() { - names.push(format!( - "Name: {} Host: {}", - game.read().unwrap().name, - game.read().unwrap().host.read().unwrap().name - )); - } - - to_string::(&GamesUpdate { games: names }).unwrap() -} - -/// Generate chatroom join announcement -pub fn meta_announce_user_join(state: &Arc, addr: &SocketAddr) -> String { - let msg = format!( - "{} joined.", - state - .online_users - .read() - .unwrap() - .get(addr) - .unwrap() - .read() - .unwrap() - .name - ); - - tracing::debug!("{}", &msg); - to_string::(&ChatMessage { text: msg }).unwrap() -} diff --git a/server/src/websocket/user.rs b/server/src/websocket/user.rs deleted file mode 100644 index 17b57e2..0000000 --- a/server/src/websocket/user.rs +++ /dev/null @@ -1,13 +0,0 @@ -use crate::User; -use lib::*; -use serde_json::to_string; -use std::sync::Arc; -use std::sync::RwLock; - -/// Generate message to notify client of user changes -pub fn user_client_self_update(new_user: &Arc>) -> String { - to_string::(&UserUpdate { - username: new_user.read().unwrap().name.clone(), - }) - .unwrap() -}