let game handler handle game things

This commit is contained in:
Adam 2024-10-11 23:09:19 -04:00
parent c4b1fc685f
commit f7a1f66667
2 changed files with 39 additions and 50 deletions

View file

@ -6,6 +6,7 @@ use crate::GameHandlerMessage::*;
use crate::SendUserMessage::*;
use crate::UserHandlerMessage::*;
use lib::*;
use serde_json::to_string;
use std::{
net::SocketAddr,
sync::{Arc, RwLock},
@ -21,6 +22,7 @@ pub enum GameHandlerMessage {
DeleteGame(GameDeleteRequest),
SendGameStateUpdate(Vec<String>),
SendGameMetaUpdate(Vec<String>),
BroadcastGamesUpdate(),
}
/// Handles game stuff
@ -45,6 +47,7 @@ impl GameHandler {
DeleteGame(request) => self.delete_game(request).await,
SendGameStateUpdate(game_ids) => self.send_game_state_update_all(game_ids),
SendGameMetaUpdate(game_ids) => self.send_game_meta_update(game_ids),
BroadcastGamesUpdate() => self.broadcast_games_update(),
}
}
@ -59,14 +62,7 @@ impl GameHandler {
.remove(&request.delete_game_id)
.is_some()
{
// Broadcast game browser update
if let Err(e) = self
.state
.broadcast_tx
.send(meta_games_browser_update(&self.state))
{
tracing::error!("Error broadcasting game browser update: {}", e);
}
self.broadcast_games_update();
} else {
tracing::error!("User tried to delete a nonexistent game!");
}
@ -206,14 +202,7 @@ impl GameHandler {
// Send updates for all players
self.send_game_meta_update(vec![game_id.clone()]);
// Broadcast game browser update
if let Err(e) = self
.state
.broadcast_tx
.send(meta_games_browser_update(&self.state))
{
tracing::error!("Could not broadcast game browser update: {}", e);
};
self.broadcast_games_update();
// Broadcast server meta update
if let Err(e) = self
@ -394,14 +383,7 @@ impl GameHandler {
self.send_game_state_update_all(vec![game_id.clone()]);
self.send_game_meta_update(vec![game_id]);
// Broadcast game browser update
let msg = meta_games_browser_update(&self.state);
let tx = self.state.broadcast_tx.clone();
tokio::spawn(async move {
if let Err(e) = tx.send(msg) {
tracing::error!("Could not broadcast game browser update: {}", e);
}
});
self.broadcast_games_update();
// Broadcast server meta update
let msg = meta_server_summary_update(&self.state);
@ -415,4 +397,32 @@ impl GameHandler {
tracing::error!("Attempted to create game for nonexistent player!");
}
}
/// Generate games list update
fn broadcast_games_update(&self) {
// TODO: this may get expensive if there are many games
let games = self
.state
.games
.read()
.unwrap()
.values()
.map(|game| GameBrowserMeta {
uuid: game.read().unwrap().uuid.to_string(),
name: game.read().unwrap().name.clone(),
host: game.read().unwrap().host.read().unwrap().name.clone(),
players: game.read().unwrap().players.len(),
packs: game.read().unwrap().packs.clone(),
})
.collect::<Vec<GameBrowserMeta>>();
let msg = to_string::<GamesUpdate>(&GamesUpdate { games }).unwrap();
let tx = self.state.broadcast_tx.clone();
tokio::spawn(async move {
if let Err(e) = tx.send(msg) {
tracing::error!("Error broadcasting games update: {}", e);
}
});
}
}

View file

@ -135,8 +135,8 @@ impl UserHandler {
.insert(addr, new_user.clone());
// Hydrate client
let tx = user_tx.clone();
let msg = meta_games_browser_update(&self.state);
let tx = self.state.games_tx.clone();
let msg = GameHandlerMessage::BroadcastGamesUpdate();
tokio::spawn(async move { tx.send(msg).await });
let tx = user_tx.clone();
let msg = meta_new_game_card_packs(&self.state);
@ -303,9 +303,9 @@ impl UserHandler {
}
}
// Send client updates
let tx = broadcast_tx.clone();
let msg = meta_games_browser_update(&self.state);
tokio::spawn(async move { tx.send(msg) });
let tx = self.state.games_tx.clone();
let msg = GameHandlerMessage::BroadcastGamesUpdate();
tokio::spawn(async move { tx.send(msg).await });
let tx = broadcast_tx.clone();
let msg = meta_chat_update(&self.state);
tokio::spawn(async move { tx.send(msg) });
@ -372,24 +372,3 @@ pub fn meta_server_summary_update(state: &Arc<AppState>) -> String {
})
.unwrap()
}
/// Generate games list update
pub fn meta_games_browser_update(state: &Arc<AppState>) -> String {
// TODO: this may get expensive if there are many games
let games = state
.games
.read()
.unwrap()
.values()
.map(|game| GameBrowserMeta {
uuid: game.read().unwrap().uuid.to_string(),
name: game.read().unwrap().name.clone(),
host: game.read().unwrap().host.read().unwrap().name.clone(),
players: game.read().unwrap().players.len(),
packs: game.read().unwrap().packs.clone(),
})
.collect::<Vec<GameBrowserMeta>>();
to_string::<GamesUpdate>(&GamesUpdate { games }).unwrap()
}