diff --git a/server/src/api.rs b/server/src/api.rs index fa9e63a..a76c3bb 100644 --- a/server/src/api.rs +++ b/server/src/api.rs @@ -16,6 +16,7 @@ use std::{net::SocketAddr, sync::Arc}; pub mod message_handler; use crate::message_handler::*; +/// Generate message-of-the-day server greeting fn motd() -> String { to_string::(&ChatMessage { text: "Greetings from the game server!".to_string(), @@ -23,6 +24,7 @@ fn motd() -> String { .unwrap() } +/// Generate server summary update - mostly debug stuff fn server_summary_update(state: &Arc) -> String { to_string::(&ServerStateSummary { online_users: state.users.lock().unwrap().len(), @@ -31,6 +33,7 @@ fn server_summary_update(state: &Arc) -> String { .unwrap() } +/// Generate chatroom metadata update fn chat_update(state: &Arc) -> String { let mut names = vec![]; @@ -45,6 +48,7 @@ fn chat_update(state: &Arc) -> String { .unwrap() } +/// Generate games list update fn games_update(state: &Arc) -> String { let mut names = vec![]; @@ -55,7 +59,7 @@ fn games_update(state: &Arc) -> String { to_string::(&GamesUpdate { games: names }).unwrap() } -/// +/// Generate chatroom join announcement fn announce_join(state: &Arc, addr: &SocketAddr) -> String { let msg = format!( "{} joined.", @@ -78,7 +82,7 @@ fn generate_new_user(state: &Arc) -> User { } } -/// Hydrate client on user changes +/// Generate message to notify client of user changes fn client_self_user_update(new_user: &User) -> String { to_string::(&UserUpdate { username: new_user.name.clone(), @@ -117,6 +121,7 @@ async fn handle_new_user( Ok(()) } +/// This runs right after a WebSocket connection is established pub async fn on_websocket_connection(stream: WebSocket, state: Arc, addr: SocketAddr) { // Split channels to send and receive asynchronously. let (mut sender, mut receiver) = stream.split(); @@ -153,6 +158,7 @@ pub async fn on_websocket_connection(stream: WebSocket, state: Arc, ad }; } +/// Establish the WebSocket connection pub async fn websocket_connection_handler( ws: WebSocketUpgrade, // user_agent: Option>, diff --git a/server/src/api/message_handler.rs b/server/src/api/message_handler.rs index 637cfa6..7a9442c 100644 --- a/server/src/api/message_handler.rs +++ b/server/src/api/message_handler.rs @@ -5,6 +5,7 @@ use anyhow::Result; use serde_json::{from_str, to_string}; use tokio::sync::broadcast::Sender; +/// This runs when a NewGameRequest is received fn handle_new_game( new_game: NewGameRequest, state: &Arc, @@ -29,6 +30,7 @@ fn handle_new_game( Ok(()) } +/// This runs when a ChatMessage is received fn handle_chat_message( chat_message: ChatMessage, state: &Arc, @@ -42,6 +44,7 @@ fn handle_chat_message( Ok(()) } +/// This runs when a UserLogIn is received fn handle_user_log_in( user_log_in: UserLogIn, state: &Arc, @@ -68,6 +71,7 @@ fn handle_user_log_in( Ok(()) } +/// Handle incoming messages over the WebSocket pub async fn message_handler( state: &Arc, addr: SocketAddr,