This commit is contained in:
Adam 2024-07-30 03:29:20 -04:00
parent 9de7f542b3
commit 1d8f053238
2 changed files with 12 additions and 2 deletions

View file

@ -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>(&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<AppState>) -> String {
to_string::<ServerStateSummary>(&ServerStateSummary {
online_users: state.users.lock().unwrap().len(),
@ -31,6 +33,7 @@ fn server_summary_update(state: &Arc<AppState>) -> String {
.unwrap()
}
/// Generate chatroom metadata update
fn chat_update(state: &Arc<AppState>) -> String {
let mut names = vec![];
@ -45,6 +48,7 @@ fn chat_update(state: &Arc<AppState>) -> String {
.unwrap()
}
/// Generate games list update
fn games_update(state: &Arc<AppState>) -> String {
let mut names = vec![];
@ -55,7 +59,7 @@ fn games_update(state: &Arc<AppState>) -> String {
to_string::<GamesUpdate>(&GamesUpdate { games: names }).unwrap()
}
///
/// Generate chatroom join announcement
fn announce_join(state: &Arc<AppState>, addr: &SocketAddr) -> String {
let msg = format!(
"{} joined.",
@ -78,7 +82,7 @@ fn generate_new_user(state: &Arc<AppState>) -> 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>(&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<AppState>, 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<AppState>, ad
};
}
/// Establish the WebSocket connection
pub async fn websocket_connection_handler(
ws: WebSocketUpgrade,
// user_agent: Option<TypedHeader<headers::UserAgent>>,

View file

@ -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<AppState>,
@ -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<AppState>,
@ -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<AppState>,
@ -68,6 +71,7 @@ fn handle_user_log_in(
Ok(())
}
/// Handle incoming messages over the WebSocket
pub async fn message_handler(
state: &Arc<AppState>,
addr: SocketAddr,