cards/server/src/api/message_handler.rs
2024-07-30 20:04:06 -04:00

136 lines
4.1 KiB
Rust

use crate::api::*;
use crate::AppState;
use crate::Arc;
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>,
tx: &Sender<String>,
) -> Result<()> {
// create game
if let Ok(new_game_object) = Game::new(new_game) {
if let Ok(game_json) = to_string(&new_game_object) {
tracing::debug!("Sent new game JSON.");
// this is a broadcast
// change this
tx.send(game_json)?;
} else {
// change this
tracing::error!("Failed to convert Game object to JSON.")
}
state.games.lock().unwrap().push(new_game_object);
tx.send(games_update(state))?;
tx.send(server_summary_update(state))?;
}
Ok(())
}
/// This runs when a ChatMessage is received
fn handle_chat_message(
chat_message: ChatMessage,
state: &Arc<AppState>,
tx: &Sender<String>,
addr: SocketAddr,
) -> Result<()> {
let msg = format! {"{0}: {1}", state.users.lock().unwrap().get(&addr).unwrap().name, chat_message.text};
tracing::debug!("{msg}");
tx.send(to_string::<ChatMessage>(&ChatMessage { text: msg })?)?;
Ok(())
}
/// This runs when a UserLogIn is received
fn handle_user_log_in(
user_log_in: UserLogIn,
state: &Arc<AppState>,
tx: &Sender<String>,
addr: SocketAddr,
) -> Result<()> {
let old_name = state.users.lock().unwrap().get(&addr).unwrap().name.clone();
let new_name = user_log_in.username.clone();
state
.users
.lock()
.unwrap()
.get_mut(&addr)
.unwrap()
.change_name(user_log_in.username);
let msg = format! {
"{0} changed name to {1}.",
old_name,
new_name
};
tracing::debug!("{msg}");
tx.send(to_string::<ChatMessage>(&ChatMessage { text: msg })?)?;
Ok(())
}
/// Handle incoming messages over the WebSocket
pub async fn message_handler(
state: &Arc<AppState>,
addr: SocketAddr,
message: Message,
) -> Result<()> {
let tx = &state.tx;
match message {
Message::Text(text) => match text {
_new_game if let Ok(_new_game) = from_str::<NewGameRequest>(&text) => {
tracing::debug!("New game request received.");
handle_new_game(_new_game, state, tx)?;
}
_chat_message if let Ok(_chat_message) = from_str::<ChatMessage>(&text) => {
handle_chat_message(_chat_message, state, tx, addr)?;
}
_user_log_in if let Ok(_user_log_in) = from_str::<UserLogIn>(&text) => {
handle_user_log_in(_user_log_in, state, tx, addr)?;
}
_ => {
tracing::debug!("Unhandled message: {}", &text);
}
},
Message::Binary(data) => {
tracing::debug!("Binary: {:?}", data)
}
Message::Close(c) => {
if let Some(cf) = c {
tracing::debug!(
"Close received from {0} with code: {1} and reason: {2}",
state.users.lock().unwrap().get(&addr).unwrap().name,
cf.code,
cf.reason
)
} else {
tracing::debug!("close received without close frame")
}
let msg = ChatMessage {
text: format!(
"{0} left.",
state.users.lock().unwrap().get(&addr).unwrap().name
),
};
tracing::debug!("{}", msg.text);
let _ = tx.send(to_string::<ChatMessage>(&msg).unwrap());
state.users.lock().unwrap().remove(&addr).unwrap();
let _ = tx.send(server_summary_update(state));
let _ = tx.send(chat_meta_update(state));
}
Message::Pong(ping) => {
tracing::debug!("Pong received with: {:?}", ping);
}
Message::Ping(pong) => {
tracing::debug!("Pong received with: {:?}", pong);
}
}
Ok(())
}