uuid and finish game meta
This commit is contained in:
parent
e3b4001cd1
commit
a7a5c1a7d5
6 changed files with 45 additions and 9 deletions
3
Cargo.lock
generated
3
Cargo.lock
generated
|
@ -1741,6 +1741,7 @@ dependencies = [
|
|||
"tower-http",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -2297,6 +2298,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"rand",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -18,3 +18,4 @@ anyhow = "1.0.82"
|
|||
tower-http = { version = "0.5.2", features = ["fs", "trace"] }
|
||||
|
||||
lib = { workspace = true }
|
||||
uuid = { version = "1.10.0", features = ["v7", "serde", "fast-rng"] }
|
||||
|
|
|
@ -89,11 +89,25 @@ impl GameHandler {
|
|||
players: new_game_object
|
||||
.players
|
||||
.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(),
|
||||
czar: new_game_object.host.read().unwrap().name.clone(),
|
||||
black: black_text,
|
||||
white: new_game_object
|
||||
.players
|
||||
.get(&new_game_object.host.read().unwrap().uuid)
|
||||
.unwrap()
|
||||
.white
|
||||
.iter()
|
||||
.map(|card| card.text.clone())
|
||||
|
|
|
@ -17,6 +17,7 @@ use std::{
|
|||
use tokio::sync::mpsc::Sender;
|
||||
use tokio::sync::{broadcast, mpsc};
|
||||
use user_handler::UserHandlerMessage;
|
||||
use uuid::Uuid;
|
||||
pub mod game_handler;
|
||||
pub mod message_handler;
|
||||
pub mod user_handler;
|
||||
|
@ -25,6 +26,7 @@ pub mod websocket;
|
|||
/// User
|
||||
#[derive(Debug)]
|
||||
pub struct User {
|
||||
pub uuid: Uuid,
|
||||
pub name: String,
|
||||
pub tx: Sender<String>,
|
||||
}
|
||||
|
@ -32,7 +34,11 @@ pub struct User {
|
|||
impl User {
|
||||
/// Create a new user object from incoming data
|
||||
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) {
|
||||
|
@ -99,8 +105,8 @@ pub struct NewGameManifest {
|
|||
/// A struct that represents a player
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
/// Player's username
|
||||
pub user: Arc<RwLock<User>>,
|
||||
/// Player's user's uuid
|
||||
pub user_uuid: Uuid,
|
||||
/// The player's hand
|
||||
pub white: Vec<CardWhite>,
|
||||
/// The player's wins
|
||||
|
@ -125,7 +131,7 @@ pub struct Game {
|
|||
/// Indicates game active/game over
|
||||
pub game_active: bool,
|
||||
/// List of current players
|
||||
pub players: Vec<Player>,
|
||||
pub players: HashMap<Uuid, Player>,
|
||||
// /// Reference to current card czar
|
||||
// czar: &Player,
|
||||
/// Black card for the current round
|
||||
|
@ -157,7 +163,7 @@ impl Game {
|
|||
white_discard: vec![],
|
||||
black_discard: vec![],
|
||||
game_active: false,
|
||||
players: vec![],
|
||||
players: HashMap::new(),
|
||||
current_black: Option::None,
|
||||
};
|
||||
tracing::debug!(
|
||||
|
@ -228,7 +234,7 @@ impl Game {
|
|||
/// Create a new player and add them to the game.
|
||||
pub fn create_player(&mut self, user: Arc<RwLock<User>>) -> Result<()> {
|
||||
let mut new_player = Player {
|
||||
user: user.clone(),
|
||||
user_uuid: user.read().unwrap().uuid.clone(),
|
||||
white: vec![],
|
||||
black: vec![],
|
||||
};
|
||||
|
@ -244,7 +250,8 @@ impl Game {
|
|||
|
||||
new_player.white.extend(hand_buf);
|
||||
|
||||
self.players.push(new_player);
|
||||
self.players
|
||||
.insert(user.read().unwrap().uuid.clone(), new_player);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -369,6 +376,7 @@ pub struct AppState {
|
|||
pub first_names: Vec<String>,
|
||||
pub last_names: Vec<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 offline_users: RwLock<HashMap<String, Arc<RwLock<User>>>>,
|
||||
pub packs: CardPacks,
|
||||
|
|
|
@ -13,6 +13,7 @@ use tokio::sync::{broadcast, mpsc};
|
|||
use tower_http::services::ServeDir;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
use user_handler::UserHandler;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
|
@ -33,6 +34,7 @@ async fn main() -> Result<()> {
|
|||
let first_names = load_names("data/first.txt")?;
|
||||
let last_names = load_names("data/last.txt")?;
|
||||
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 offline_users = RwLock::new(HashMap::<String, Arc<RwLock<User>>>::new());
|
||||
let (packs, packs_meta) = load_cards_from_json("data/cah-cards-full.json")?;
|
||||
|
@ -46,6 +48,7 @@ async fn main() -> Result<()> {
|
|||
first_names,
|
||||
last_names,
|
||||
reserved_names,
|
||||
user_uuid,
|
||||
online_users,
|
||||
offline_users,
|
||||
packs,
|
||||
|
|
|
@ -41,7 +41,14 @@ impl UserHandler {
|
|||
// Notify client of new username
|
||||
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
|
||||
.online_users
|
||||
.write()
|
||||
|
|
Loading…
Add table
Reference in a new issue