2024-07-22 01:32:09 -04:00
|
|
|
use crate::api::*;
|
2024-05-04 02:23:40 -04:00
|
|
|
use crate::AppState;
|
|
|
|
use crate::Arc;
|
2024-07-30 03:22:32 -04:00
|
|
|
use anyhow::Result;
|
2024-07-30 22:40:34 -04:00
|
|
|
use axum::extract::ws::CloseFrame;
|
2024-07-30 03:22:32 -04:00
|
|
|
use serde_json::{from_str, to_string};
|
|
|
|
use tokio::sync::broadcast::Sender;
|
|
|
|
|
2024-07-30 03:29:20 -04:00
|
|
|
/// This runs when a NewGameRequest is received
|
2024-07-30 03:22:32 -04:00
|
|
|
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.")
|
|
|
|
}
|
2024-07-30 22:40:34 -04:00
|
|
|
|
2024-07-30 03:22:32 -04:00
|
|
|
state.games.lock().unwrap().push(new_game_object);
|
|
|
|
tx.send(games_update(state))?;
|
|
|
|
tx.send(server_summary_update(state))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-07-30 03:29:20 -04:00
|
|
|
/// This runs when a ChatMessage is received
|
2024-07-30 03:22:32 -04:00
|
|
|
fn handle_chat_message(
|
|
|
|
chat_message: ChatMessage,
|
|
|
|
state: &Arc<AppState>,
|
|
|
|
tx: &Sender<String>,
|
|
|
|
addr: SocketAddr,
|
|
|
|
) -> Result<()> {
|
2024-07-31 21:19:29 -04:00
|
|
|
let msg = format! {"{0}: {1}", state.online_users.lock().unwrap().get(&addr).unwrap().name, chat_message.text};
|
2024-07-30 03:22:32 -04:00
|
|
|
tracing::debug!("{msg}");
|
|
|
|
tx.send(to_string::<ChatMessage>(&ChatMessage { text: msg })?)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-06-22 02:16:35 -04:00
|
|
|
|
2024-07-30 03:29:20 -04:00
|
|
|
/// This runs when a UserLogIn is received
|
2024-07-30 03:22:32 -04:00
|
|
|
fn handle_user_log_in(
|
|
|
|
user_log_in: UserLogIn,
|
|
|
|
state: &Arc<AppState>,
|
|
|
|
tx: &Sender<String>,
|
|
|
|
addr: SocketAddr,
|
|
|
|
) -> Result<()> {
|
2024-07-31 21:19:29 -04:00
|
|
|
let old_name = state
|
|
|
|
.online_users
|
2024-07-30 03:22:32 -04:00
|
|
|
.lock()
|
|
|
|
.unwrap()
|
2024-07-31 21:19:29 -04:00
|
|
|
.get(&addr)
|
2024-07-30 03:22:32 -04:00
|
|
|
.unwrap()
|
2024-07-31 21:19:29 -04:00
|
|
|
.name
|
|
|
|
.clone();
|
|
|
|
let new_name = user_log_in.username.clone();
|
2024-07-30 22:40:34 -04:00
|
|
|
|
2024-07-31 21:19:29 -04:00
|
|
|
if state.offline_users.lock().unwrap().contains_key(&new_name) {
|
|
|
|
state
|
|
|
|
.online_users
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.insert(
|
|
|
|
addr,
|
|
|
|
state
|
|
|
|
.offline_users
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.remove(&new_name)
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let msg = format! {
|
|
|
|
"{0} changed name to {1}. Welcome back!",
|
|
|
|
old_name,
|
|
|
|
new_name
|
|
|
|
};
|
|
|
|
|
|
|
|
tracing::debug!("{msg}");
|
|
|
|
} else {
|
|
|
|
state
|
|
|
|
.online_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 })?)?;
|
|
|
|
}
|
2024-07-30 22:40:34 -04:00
|
|
|
|
2024-07-31 21:19:29 -04:00
|
|
|
tracing::debug!(
|
|
|
|
"User updated at ptr: {:p}",
|
|
|
|
state.online_users.lock().unwrap().get(&addr).unwrap()
|
|
|
|
);
|
|
|
|
tracing::debug!(
|
|
|
|
"User updated deref ptr: {:p}",
|
|
|
|
*state.online_users.lock().unwrap().get(&addr).unwrap()
|
|
|
|
);
|
|
|
|
tracing::debug!(
|
|
|
|
"Online Users: {} Offline Users: {}",
|
|
|
|
state.online_users.lock().unwrap().len(),
|
|
|
|
state.offline_users.lock().unwrap().len()
|
|
|
|
);
|
2024-07-30 03:22:32 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-07-30 22:40:34 -04:00
|
|
|
/// This runs when a connection closes
|
|
|
|
fn handle_close(
|
|
|
|
close_frame: Option<CloseFrame>,
|
|
|
|
state: &Arc<AppState>,
|
|
|
|
tx: &Sender<String>,
|
|
|
|
addr: SocketAddr,
|
|
|
|
) -> Result<()> {
|
|
|
|
if let Some(cf) = close_frame {
|
|
|
|
tracing::debug!(
|
|
|
|
"Close received from {0} with code: {1} and reason: {2}",
|
2024-07-31 21:19:29 -04:00
|
|
|
state.online_users.lock().unwrap().get(&addr).unwrap().name,
|
2024-07-30 22:40:34 -04:00
|
|
|
cf.code,
|
|
|
|
cf.reason
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
tracing::debug!("close received without close frame")
|
|
|
|
}
|
|
|
|
|
|
|
|
let msg = ChatMessage {
|
|
|
|
text: format!(
|
|
|
|
"{0} left.",
|
2024-07-31 21:19:29 -04:00
|
|
|
state.online_users.lock().unwrap().get(&addr).unwrap().name
|
2024-07-30 22:40:34 -04:00
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
tracing::debug!("{}", msg.text);
|
|
|
|
tx.send(to_string::<ChatMessage>(&msg)?)?;
|
2024-07-31 21:19:29 -04:00
|
|
|
let name = state
|
|
|
|
.online_users
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.get(&addr)
|
|
|
|
.unwrap()
|
|
|
|
.name
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
state.offline_users.lock().unwrap().insert(
|
|
|
|
name.clone(),
|
|
|
|
state.online_users.lock().unwrap().remove(&addr).unwrap(),
|
|
|
|
);
|
|
|
|
|
|
|
|
tracing::debug!(
|
|
|
|
"User moved to offline ptr: {:p}",
|
|
|
|
state.offline_users.lock().unwrap().get(&name).unwrap()
|
|
|
|
);
|
|
|
|
tracing::debug!(
|
|
|
|
"User offline deref ptr: {:p}",
|
|
|
|
*state.offline_users.lock().unwrap().get(&name).unwrap()
|
|
|
|
);
|
2024-07-30 22:40:34 -04:00
|
|
|
tx.send(server_summary_update(state))?;
|
|
|
|
tx.send(chat_meta_update(state))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-07-30 03:29:20 -04:00
|
|
|
/// Handle incoming messages over the WebSocket
|
2024-07-30 03:22:32 -04:00
|
|
|
pub async fn message_handler(
|
|
|
|
state: &Arc<AppState>,
|
|
|
|
addr: SocketAddr,
|
|
|
|
message: Message,
|
|
|
|
) -> Result<()> {
|
2024-05-04 02:23:40 -04:00
|
|
|
let tx = &state.tx;
|
|
|
|
|
|
|
|
match message {
|
2024-07-30 03:22:32 -04:00
|
|
|
Message::Text(text) => match text {
|
|
|
|
_new_game if let Ok(_new_game) = from_str::<NewGameRequest>(&text) => {
|
2024-07-21 22:48:15 -04:00
|
|
|
tracing::debug!("New game request received.");
|
2024-07-30 03:22:32 -04:00
|
|
|
handle_new_game(_new_game, state, tx)?;
|
|
|
|
}
|
|
|
|
_chat_message if let Ok(_chat_message) = from_str::<ChatMessage>(&text) => {
|
2024-07-30 19:57:06 -04:00
|
|
|
handle_chat_message(_chat_message, state, tx, addr)?;
|
2024-07-30 03:22:32 -04:00
|
|
|
}
|
|
|
|
_user_log_in if let Ok(_user_log_in) = from_str::<UserLogIn>(&text) => {
|
2024-07-30 19:57:06 -04:00
|
|
|
handle_user_log_in(_user_log_in, state, tx, addr)?;
|
2024-07-30 03:22:32 -04:00
|
|
|
}
|
|
|
|
_ => {
|
2024-07-30 22:40:34 -04:00
|
|
|
tracing::debug!("Unhandled text message: {}", &text);
|
2024-05-04 02:23:40 -04:00
|
|
|
}
|
2024-07-30 03:22:32 -04:00
|
|
|
},
|
2024-05-04 02:23:40 -04:00
|
|
|
Message::Binary(data) => {
|
|
|
|
tracing::debug!("Binary: {:?}", data)
|
|
|
|
}
|
2024-07-30 22:40:34 -04:00
|
|
|
Message::Close(close_frame) => {
|
|
|
|
handle_close(close_frame, state, tx, addr)?;
|
2024-05-04 02:23:40 -04:00
|
|
|
}
|
|
|
|
Message::Pong(ping) => {
|
|
|
|
tracing::debug!("Pong received with: {:?}", ping);
|
|
|
|
}
|
|
|
|
Message::Ping(pong) => {
|
|
|
|
tracing::debug!("Pong received with: {:?}", pong);
|
|
|
|
}
|
|
|
|
}
|
2024-07-30 22:40:34 -04:00
|
|
|
|
2024-07-30 03:22:32 -04:00
|
|
|
Ok(())
|
2024-05-04 02:23:40 -04:00
|
|
|
}
|