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::SendUserMessage::*;
use crate::UserHandlerMessage::*; use crate::UserHandlerMessage::*;
use lib::*; use lib::*;
use serde_json::to_string;
use std::{ use std::{
net::SocketAddr, net::SocketAddr,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
@ -21,6 +22,7 @@ pub enum GameHandlerMessage {
DeleteGame(GameDeleteRequest), DeleteGame(GameDeleteRequest),
SendGameStateUpdate(Vec<String>), SendGameStateUpdate(Vec<String>),
SendGameMetaUpdate(Vec<String>), SendGameMetaUpdate(Vec<String>),
BroadcastGamesUpdate(),
} }
/// Handles game stuff /// Handles game stuff
@ -45,6 +47,7 @@ impl GameHandler {
DeleteGame(request) => self.delete_game(request).await, DeleteGame(request) => self.delete_game(request).await,
SendGameStateUpdate(game_ids) => self.send_game_state_update_all(game_ids), SendGameStateUpdate(game_ids) => self.send_game_state_update_all(game_ids),
SendGameMetaUpdate(game_ids) => self.send_game_meta_update(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) .remove(&request.delete_game_id)
.is_some() .is_some()
{ {
// Broadcast game browser update self.broadcast_games_update();
if let Err(e) = self
.state
.broadcast_tx
.send(meta_games_browser_update(&self.state))
{
tracing::error!("Error broadcasting game browser update: {}", e);
}
} else { } else {
tracing::error!("User tried to delete a nonexistent game!"); tracing::error!("User tried to delete a nonexistent game!");
} }
@ -206,14 +202,7 @@ impl GameHandler {
// Send updates for all players // Send updates for all players
self.send_game_meta_update(vec![game_id.clone()]); self.send_game_meta_update(vec![game_id.clone()]);
// Broadcast game browser update self.broadcast_games_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);
};
// Broadcast server meta update // Broadcast server meta update
if let Err(e) = self if let Err(e) = self
@ -394,14 +383,7 @@ impl GameHandler {
self.send_game_state_update_all(vec![game_id.clone()]); self.send_game_state_update_all(vec![game_id.clone()]);
self.send_game_meta_update(vec![game_id]); self.send_game_meta_update(vec![game_id]);
// Broadcast game browser update self.broadcast_games_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);
}
});
// Broadcast server meta update // Broadcast server meta update
let msg = meta_server_summary_update(&self.state); let msg = meta_server_summary_update(&self.state);
@ -415,4 +397,32 @@ impl GameHandler {
tracing::error!("Attempted to create game for nonexistent player!"); 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()); .insert(addr, new_user.clone());
// Hydrate client // Hydrate client
let tx = user_tx.clone(); let tx = self.state.games_tx.clone();
let msg = meta_games_browser_update(&self.state); let msg = GameHandlerMessage::BroadcastGamesUpdate();
tokio::spawn(async move { tx.send(msg).await }); tokio::spawn(async move { tx.send(msg).await });
let tx = user_tx.clone(); let tx = user_tx.clone();
let msg = meta_new_game_card_packs(&self.state); let msg = meta_new_game_card_packs(&self.state);
@ -303,9 +303,9 @@ impl UserHandler {
} }
} }
// Send client updates // Send client updates
let tx = broadcast_tx.clone(); let tx = self.state.games_tx.clone();
let msg = meta_games_browser_update(&self.state); let msg = GameHandlerMessage::BroadcastGamesUpdate();
tokio::spawn(async move { tx.send(msg) }); tokio::spawn(async move { tx.send(msg).await });
let tx = broadcast_tx.clone(); let tx = broadcast_tx.clone();
let msg = meta_chat_update(&self.state); let msg = meta_chat_update(&self.state);
tokio::spawn(async move { tx.send(msg) }); tokio::spawn(async move { tx.send(msg) });
@ -372,24 +372,3 @@ pub fn meta_server_summary_update(state: &Arc<AppState>) -> String {
}) })
.unwrap() .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()
}