follow the pattern

This commit is contained in:
Adam 2024-08-06 00:33:37 -04:00
parent 0c021c70f0
commit cc332c63ae
4 changed files with 14 additions and 7 deletions

View file

@ -361,5 +361,5 @@ pub struct AppState {
pub offline_users: RwLock<HashMap<String, Arc<RwLock<User>>>>, pub offline_users: RwLock<HashMap<String, Arc<RwLock<User>>>>,
pub packs: CardPacks, pub packs: CardPacks,
pub packs_meta: CardPacksMeta, pub packs_meta: CardPacksMeta,
pub games: RwLock<Vec<Game>>, pub games: RwLock<HashMap<String, RwLock<Game>>>,
} }

View file

@ -30,7 +30,7 @@ async fn main() -> Result<()> {
let online_users = RwLock::new(HashMap::<SocketAddr, Arc<RwLock<User>>>::new()); let online_users = RwLock::new(HashMap::<SocketAddr, Arc<RwLock<User>>>::new());
let offline_users = RwLock::new(HashMap::<String, Arc<RwLock<User>>>::new()); let offline_users = RwLock::new(HashMap::<String, Arc<RwLock<User>>>::new());
let (packs, packs_meta) = load_cards_from_json("data/cah-cards-full.json")?; let (packs, packs_meta) = load_cards_from_json("data/cah-cards-full.json")?;
let games = RwLock::new(vec![]); let games = RwLock::new(HashMap::new());
let app_state = Arc::new(AppState { let app_state = Arc::new(AppState {
tx, tx,

View file

@ -15,7 +15,10 @@ use axum::{
use futures::{SinkExt, StreamExt}; use futures::{SinkExt, StreamExt};
use lib::*; use lib::*;
use serde_json::{from_str, to_string}; use serde_json::{from_str, to_string};
use std::{net::SocketAddr, sync::Arc}; use std::{
net::SocketAddr,
sync::{Arc, RwLock},
};
use tokio::sync::broadcast::Sender; use tokio::sync::broadcast::Sender;
pub mod meta; pub mod meta;
pub mod user; pub mod user;
@ -131,7 +134,11 @@ fn game_handle_new_game(
// create game // create game
if let Ok(new_game_object) = Game::new(manifest) { if let Ok(new_game_object) = Game::new(manifest) {
state.games.write().unwrap().push(new_game_object); state
.games
.write()
.unwrap()
.insert(new_game_object.name.clone(), RwLock::new(new_game_object));
tx.send(meta_games_browser_update(state))?; tx.send(meta_games_browser_update(state))?;
tx.send(meta_server_summary_update(state))?; tx.send(meta_server_summary_update(state))?;
} }

View file

@ -49,11 +49,11 @@ pub fn meta_games_browser_update(state: &Arc<AppState>) -> String {
// this may get expensive if there are many games // this may get expensive if there are many games
let mut names = vec![]; let mut names = vec![];
for game in state.games.read().unwrap().iter() { for game in state.games.read().unwrap().values() {
names.push(format!( names.push(format!(
"Name: {} Host: {}", "Name: {} Host: {}",
game.name, game.read().unwrap().name,
game.host.read().unwrap().name game.read().unwrap().host.read().unwrap().name
)); ));
} }