move more shit
This commit is contained in:
parent
1f51d9d504
commit
9aaa0c87ea
4 changed files with 87 additions and 97 deletions
|
@ -1,9 +1,10 @@
|
|||
use crate::websocket::meta::*;
|
||||
use crate::websocket::user::*;
|
||||
use crate::AppState;
|
||||
use crate::NewUser;
|
||||
use crate::User;
|
||||
use anyhow::Result;
|
||||
use lib::*;
|
||||
use serde_json::to_string;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
pub struct UserHandler {
|
||||
|
@ -83,3 +84,86 @@ impl UserHandler {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate message to notify client of user changes
|
||||
pub fn user_client_self_update(new_user: &Arc<RwLock<User>>) -> String {
|
||||
to_string::<UserUpdate>(&UserUpdate {
|
||||
username: new_user.read().unwrap().name.clone(),
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Generate chatroom metadata update
|
||||
pub fn meta_chat_update(state: &Arc<AppState>) -> String {
|
||||
// this may get expensive if there are many users
|
||||
let mut names = vec![];
|
||||
|
||||
for user in state.online_users.read().unwrap().iter() {
|
||||
names.push(user.1.read().unwrap().name.clone());
|
||||
}
|
||||
|
||||
to_string::<ChatUpdate>(&ChatUpdate {
|
||||
room: "Lobby".to_string(),
|
||||
users: names,
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Generage cards meta message
|
||||
pub fn meta_new_game_card_packs(state: &Arc<AppState>) -> String {
|
||||
to_string::<CardPacksMeta>(&state.packs_meta).unwrap()
|
||||
}
|
||||
|
||||
/// Generate message-of-the-day server greeting
|
||||
pub fn meta_motd() -> String {
|
||||
to_string::<ChatMessage>(&ChatMessage {
|
||||
text: "Greetings from the game server!".to_string(),
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Generate server summary update - mostly debug stuff
|
||||
pub fn meta_server_summary_update(state: &Arc<AppState>) -> String {
|
||||
let online_users = state.online_users.read().unwrap().len();
|
||||
let active_games = state.games.read().unwrap().len();
|
||||
to_string::<ServerStateSummary>(&ServerStateSummary {
|
||||
online_users,
|
||||
active_games,
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Generate games list update
|
||||
pub fn meta_games_browser_update(state: &Arc<AppState>) -> String {
|
||||
// this may get expensive if there are many games
|
||||
let mut names = vec![];
|
||||
|
||||
for game in state.games.read().unwrap().values() {
|
||||
names.push(format!(
|
||||
"Name: {} Host: {}",
|
||||
game.read().unwrap().name,
|
||||
game.read().unwrap().host.read().unwrap().name
|
||||
));
|
||||
}
|
||||
|
||||
to_string::<GamesUpdate>(&GamesUpdate { games: names }).unwrap()
|
||||
}
|
||||
|
||||
/// Generate chatroom join announcement
|
||||
pub fn meta_announce_user_join(state: &Arc<AppState>, addr: &SocketAddr) -> String {
|
||||
let msg = format!(
|
||||
"{} joined.",
|
||||
state
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(addr)
|
||||
.unwrap()
|
||||
.read()
|
||||
.unwrap()
|
||||
.name
|
||||
);
|
||||
|
||||
tracing::debug!("{}", &msg);
|
||||
to_string::<ChatMessage>(&ChatMessage { text: msg }).unwrap()
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::websocket::meta::*;
|
||||
use crate::user_handler::*;
|
||||
use crate::AppState;
|
||||
use crate::Game;
|
||||
use crate::NewGameManifest;
|
||||
|
@ -20,8 +20,6 @@ use std::{
|
|||
sync::{Arc, RwLock},
|
||||
};
|
||||
use tokio::sync::{broadcast::Sender, mpsc};
|
||||
pub mod meta;
|
||||
pub mod user;
|
||||
|
||||
/// Establish the WebSocket connection
|
||||
pub async fn websocket_connection_handler(
|
||||
|
|
|
@ -1,79 +0,0 @@
|
|||
use crate::AppState;
|
||||
use lib::*;
|
||||
use serde_json::to_string;
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
|
||||
/// Generate chatroom metadata update
|
||||
pub fn meta_chat_update(state: &Arc<AppState>) -> String {
|
||||
// this may get expensive if there are many users
|
||||
let mut names = vec![];
|
||||
|
||||
for user in state.online_users.read().unwrap().iter() {
|
||||
names.push(user.1.read().unwrap().name.clone());
|
||||
}
|
||||
|
||||
to_string::<ChatUpdate>(&ChatUpdate {
|
||||
room: "Lobby".to_string(),
|
||||
users: names,
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Generage cards meta message
|
||||
pub fn meta_new_game_card_packs(state: &Arc<AppState>) -> String {
|
||||
to_string::<CardPacksMeta>(&state.packs_meta).unwrap()
|
||||
}
|
||||
|
||||
/// Generate message-of-the-day server greeting
|
||||
pub fn meta_motd() -> String {
|
||||
to_string::<ChatMessage>(&ChatMessage {
|
||||
text: "Greetings from the game server!".to_string(),
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Generate server summary update - mostly debug stuff
|
||||
pub fn meta_server_summary_update(state: &Arc<AppState>) -> String {
|
||||
let online_users = state.online_users.read().unwrap().len();
|
||||
let active_games = state.games.read().unwrap().len();
|
||||
to_string::<ServerStateSummary>(&ServerStateSummary {
|
||||
online_users,
|
||||
active_games,
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Generate games list update
|
||||
pub fn meta_games_browser_update(state: &Arc<AppState>) -> String {
|
||||
// this may get expensive if there are many games
|
||||
let mut names = vec![];
|
||||
|
||||
for game in state.games.read().unwrap().values() {
|
||||
names.push(format!(
|
||||
"Name: {} Host: {}",
|
||||
game.read().unwrap().name,
|
||||
game.read().unwrap().host.read().unwrap().name
|
||||
));
|
||||
}
|
||||
|
||||
to_string::<GamesUpdate>(&GamesUpdate { games: names }).unwrap()
|
||||
}
|
||||
|
||||
/// Generate chatroom join announcement
|
||||
pub fn meta_announce_user_join(state: &Arc<AppState>, addr: &SocketAddr) -> String {
|
||||
let msg = format!(
|
||||
"{} joined.",
|
||||
state
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(addr)
|
||||
.unwrap()
|
||||
.read()
|
||||
.unwrap()
|
||||
.name
|
||||
);
|
||||
|
||||
tracing::debug!("{}", &msg);
|
||||
to_string::<ChatMessage>(&ChatMessage { text: msg }).unwrap()
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
use crate::User;
|
||||
use lib::*;
|
||||
use serde_json::to_string;
|
||||
use std::sync::Arc;
|
||||
use std::sync::RwLock;
|
||||
|
||||
/// Generate message to notify client of user changes
|
||||
pub fn user_client_self_update(new_user: &Arc<RwLock<User>>) -> String {
|
||||
to_string::<UserUpdate>(&UserUpdate {
|
||||
username: new_user.read().unwrap().name.clone(),
|
||||
})
|
||||
.unwrap()
|
||||
}
|
Loading…
Add table
Reference in a new issue