uuid and finish game meta

This commit is contained in:
Adam 2024-08-11 19:51:30 -04:00
parent e3b4001cd1
commit a7a5c1a7d5
6 changed files with 45 additions and 9 deletions

3
Cargo.lock generated
View file

@ -1741,6 +1741,7 @@ dependencies = [
"tower-http", "tower-http",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
"uuid",
] ]
[[package]] [[package]]
@ -2297,6 +2298,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314"
dependencies = [ dependencies = [
"getrandom", "getrandom",
"rand",
"serde",
] ]
[[package]] [[package]]

View file

@ -18,3 +18,4 @@ anyhow = "1.0.82"
tower-http = { version = "0.5.2", features = ["fs", "trace"] } tower-http = { version = "0.5.2", features = ["fs", "trace"] }
lib = { workspace = true } lib = { workspace = true }
uuid = { version = "1.10.0", features = ["v7", "serde", "fast-rng"] }

View file

@ -89,11 +89,25 @@ impl GameHandler {
players: new_game_object players: new_game_object
.players .players
.iter() .iter()
.map(|player| player.user.read().unwrap().name.clone()) .map(|player| {
self.state
.user_uuid
.read()
.unwrap()
.get(&player.0)
.unwrap()
.read()
.unwrap()
.name
.clone()
})
.collect(), .collect(),
czar: new_game_object.host.read().unwrap().name.clone(), czar: new_game_object.host.read().unwrap().name.clone(),
black: black_text, black: black_text,
white: new_game_object white: new_game_object
.players
.get(&new_game_object.host.read().unwrap().uuid)
.unwrap()
.white .white
.iter() .iter()
.map(|card| card.text.clone()) .map(|card| card.text.clone())

View file

@ -17,6 +17,7 @@ use std::{
use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::Sender;
use tokio::sync::{broadcast, mpsc}; use tokio::sync::{broadcast, mpsc};
use user_handler::UserHandlerMessage; use user_handler::UserHandlerMessage;
use uuid::Uuid;
pub mod game_handler; pub mod game_handler;
pub mod message_handler; pub mod message_handler;
pub mod user_handler; pub mod user_handler;
@ -25,6 +26,7 @@ pub mod websocket;
/// User /// User
#[derive(Debug)] #[derive(Debug)]
pub struct User { pub struct User {
pub uuid: Uuid,
pub name: String, pub name: String,
pub tx: Sender<String>, pub tx: Sender<String>,
} }
@ -32,7 +34,11 @@ pub struct User {
impl User { impl User {
/// Create a new user object from incoming data /// Create a new user object from incoming data
pub fn new(name: String, tx: Sender<String>) -> User { pub fn new(name: String, tx: Sender<String>) -> User {
User { name, tx } User {
uuid: Uuid::now_v7(),
name,
tx,
}
} }
pub fn change_name(&mut self, new_name: String) { pub fn change_name(&mut self, new_name: String) {
@ -99,8 +105,8 @@ pub struct NewGameManifest {
/// A struct that represents a player /// A struct that represents a player
#[derive(Debug)] #[derive(Debug)]
pub struct Player { pub struct Player {
/// Player's username /// Player's user's uuid
pub user: Arc<RwLock<User>>, pub user_uuid: Uuid,
/// The player's hand /// The player's hand
pub white: Vec<CardWhite>, pub white: Vec<CardWhite>,
/// The player's wins /// The player's wins
@ -125,7 +131,7 @@ pub struct Game {
/// Indicates game active/game over /// Indicates game active/game over
pub game_active: bool, pub game_active: bool,
/// List of current players /// List of current players
pub players: Vec<Player>, pub players: HashMap<Uuid, Player>,
// /// Reference to current card czar // /// Reference to current card czar
// czar: &Player, // czar: &Player,
/// Black card for the current round /// Black card for the current round
@ -157,7 +163,7 @@ impl Game {
white_discard: vec![], white_discard: vec![],
black_discard: vec![], black_discard: vec![],
game_active: false, game_active: false,
players: vec![], players: HashMap::new(),
current_black: Option::None, current_black: Option::None,
}; };
tracing::debug!( tracing::debug!(
@ -228,7 +234,7 @@ impl Game {
/// Create a new player and add them to the game. /// Create a new player and add them to the game.
pub fn create_player(&mut self, user: Arc<RwLock<User>>) -> Result<()> { pub fn create_player(&mut self, user: Arc<RwLock<User>>) -> Result<()> {
let mut new_player = Player { let mut new_player = Player {
user: user.clone(), user_uuid: user.read().unwrap().uuid.clone(),
white: vec![], white: vec![],
black: vec![], black: vec![],
}; };
@ -244,7 +250,8 @@ impl Game {
new_player.white.extend(hand_buf); new_player.white.extend(hand_buf);
self.players.push(new_player); self.players
.insert(user.read().unwrap().uuid.clone(), new_player);
Ok(()) Ok(())
} }
@ -369,6 +376,7 @@ pub struct AppState {
pub first_names: Vec<String>, pub first_names: Vec<String>,
pub last_names: Vec<String>, pub last_names: Vec<String>,
pub reserved_names: RwLock<HashSet<String>>, pub reserved_names: RwLock<HashSet<String>>,
pub user_uuid: RwLock<HashMap<Uuid, Arc<RwLock<User>>>>,
pub online_users: RwLock<HashMap<SocketAddr, Arc<RwLock<User>>>>, pub online_users: RwLock<HashMap<SocketAddr, Arc<RwLock<User>>>>,
pub offline_users: RwLock<HashMap<String, Arc<RwLock<User>>>>, pub offline_users: RwLock<HashMap<String, Arc<RwLock<User>>>>,
pub packs: CardPacks, pub packs: CardPacks,

View file

@ -13,6 +13,7 @@ use tokio::sync::{broadcast, mpsc};
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use user_handler::UserHandler; use user_handler::UserHandler;
use uuid::Uuid;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
@ -33,6 +34,7 @@ async fn main() -> Result<()> {
let first_names = load_names("data/first.txt")?; let first_names = load_names("data/first.txt")?;
let last_names = load_names("data/last.txt")?; let last_names = load_names("data/last.txt")?;
let reserved_names = RwLock::new(HashSet::<String>::new()); let reserved_names = RwLock::new(HashSet::<String>::new());
let user_uuid = RwLock::new(HashMap::<Uuid, Arc<RwLock<User>>>::new());
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")?;
@ -46,6 +48,7 @@ async fn main() -> Result<()> {
first_names, first_names,
last_names, last_names,
reserved_names, reserved_names,
user_uuid,
online_users, online_users,
offline_users, offline_users,
packs, packs,

View file

@ -41,7 +41,14 @@ impl UserHandler {
// Notify client of new username // Notify client of new username
tx.send(user_client_self_update(&new_user)).await.unwrap(); tx.send(user_client_self_update(&new_user)).await.unwrap();
// Register using `addr` as key until something longer lived exists // Register uuid
self.state
.user_uuid
.write()
.unwrap()
.insert(new_user.read().unwrap().uuid.clone(), new_user.clone());
// Register online using `addr` as key until something longer lived exists
self.state self.state
.online_users .online_users
.write() .write()