2024-05-03 23:17:39 -04:00
|
|
|
use crate::AppState;
|
2024-05-04 02:46:10 -04:00
|
|
|
use axum::{
|
|
|
|
extract::{
|
|
|
|
ws::{Message, WebSocket},
|
|
|
|
ConnectInfo, State, WebSocketUpgrade,
|
|
|
|
},
|
|
|
|
response::IntoResponse,
|
|
|
|
};
|
|
|
|
use futures::{SinkExt, StreamExt};
|
2024-07-21 02:35:13 -04:00
|
|
|
use lib::models::*;
|
2024-07-24 22:06:19 -04:00
|
|
|
use serde_json::to_string;
|
2024-05-03 23:17:39 -04:00
|
|
|
use std::{net::SocketAddr, sync::Arc};
|
2024-07-21 00:59:21 -04:00
|
|
|
|
2024-05-04 02:23:40 -04:00
|
|
|
pub mod message_handler;
|
|
|
|
use crate::message_handler::*;
|
2024-04-27 18:22:35 -04:00
|
|
|
|
2024-07-24 22:06:19 -04:00
|
|
|
fn motd() -> ChatMessage {
|
|
|
|
ChatMessage {
|
|
|
|
text: "Greetings from the game server!".to_string(),
|
|
|
|
}
|
2024-05-03 23:17:39 -04:00
|
|
|
}
|
2024-05-04 02:23:40 -04:00
|
|
|
|
2024-07-22 01:32:09 -04:00
|
|
|
fn server_sum_update(state: &Arc<AppState>) -> ServerStateSummary {
|
|
|
|
ServerStateSummary {
|
|
|
|
online_users: state.users.lock().unwrap().len(),
|
|
|
|
active_games: state.games.lock().unwrap().len(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn on_websocket_connection(stream: WebSocket, state: Arc<AppState>, who: User) {
|
|
|
|
// Add user to users
|
|
|
|
//if doesn't exist
|
|
|
|
let _true_if_not_exist = &state.users.lock().unwrap().insert(who.clone());
|
|
|
|
//etc
|
|
|
|
|
2024-04-28 04:53:00 -04:00
|
|
|
// By splitting, we can send and receive at the same time.
|
|
|
|
let (mut sender, mut receiver) = stream.split();
|
|
|
|
|
2024-07-22 01:32:09 -04:00
|
|
|
// hydrate user
|
2024-07-24 22:06:19 -04:00
|
|
|
let _ = &sender
|
|
|
|
.send(Message::Text(to_string::<ChatMessage>(&motd()).unwrap()))
|
|
|
|
.await;
|
|
|
|
let _ = &sender
|
|
|
|
.send(Message::Text(
|
|
|
|
to_string(&server_sum_update(&state)).unwrap(),
|
|
|
|
))
|
|
|
|
.await;
|
2024-05-03 23:17:39 -04:00
|
|
|
|
2024-05-03 23:30:02 -04:00
|
|
|
// ANNOUNCE THY PRESENCE
|
2024-07-24 22:06:19 -04:00
|
|
|
let msg = ChatMessage {
|
|
|
|
text: format!("{} joined.", who.name),
|
|
|
|
};
|
|
|
|
tracing::debug!("{}", msg.text);
|
|
|
|
let _ = &state.tx.send(to_string::<ChatMessage>(&msg).unwrap());
|
2024-07-22 01:32:09 -04:00
|
|
|
|
|
|
|
// Broadcast server state summary update
|
|
|
|
let _ = &state
|
|
|
|
.tx
|
2024-07-24 22:06:19 -04:00
|
|
|
.send(to_string(&server_sum_update(&state)).unwrap());
|
2024-07-22 01:32:09 -04:00
|
|
|
|
|
|
|
// subscribe to broadcast channel
|
|
|
|
let mut rx = state.tx.subscribe();
|
2024-05-03 18:48:12 -04:00
|
|
|
|
2024-05-03 23:30:02 -04:00
|
|
|
// handle broadcasting further awesome messages
|
2024-05-03 23:17:39 -04:00
|
|
|
let mut send_task = tokio::spawn(async move {
|
|
|
|
while let Ok(msg) = rx.recv().await {
|
|
|
|
if sender.send(Message::Text(msg)).await.is_err() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-05-03 23:30:02 -04:00
|
|
|
// handle new incoming messages
|
2024-05-03 23:17:39 -04:00
|
|
|
let mut recv_task = tokio::spawn(async move {
|
|
|
|
while let Some(Ok(message)) = receiver.next().await {
|
2024-07-16 21:11:24 -04:00
|
|
|
message_handler(message, &state, &who).await
|
2024-04-28 04:53:00 -04:00
|
|
|
}
|
2024-05-03 23:17:39 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// if either task completes then abort the other
|
|
|
|
tokio::select! {
|
|
|
|
_ = (&mut send_task) => recv_task.abort(),
|
|
|
|
_ = (&mut recv_task) => send_task.abort(),
|
|
|
|
};
|
2024-04-27 18:22:35 -04:00
|
|
|
}
|
2024-05-04 02:49:17 -04:00
|
|
|
|
2024-07-22 01:32:09 -04:00
|
|
|
pub async fn websocket_connection_handler(
|
2024-05-04 02:49:17 -04:00
|
|
|
ws: WebSocketUpgrade,
|
|
|
|
// user_agent: Option<TypedHeader<headers::UserAgent>>,
|
|
|
|
ConnectInfo(addr): ConnectInfo<SocketAddr>,
|
|
|
|
State(state): State<Arc<AppState>>,
|
|
|
|
) -> impl IntoResponse {
|
|
|
|
tracing::debug!("New connection from {addr}");
|
2024-07-16 20:49:24 -04:00
|
|
|
ws.on_upgrade(move |socket| {
|
2024-07-22 01:32:09 -04:00
|
|
|
on_websocket_connection(
|
2024-07-16 20:49:24 -04:00
|
|
|
socket,
|
|
|
|
state,
|
|
|
|
User {
|
|
|
|
name: "Anonymous".to_string(),
|
|
|
|
addr,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
2024-05-04 02:49:17 -04:00
|
|
|
}
|