From 788bf5b00e53659e211d352c002f00b747aea2fd Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Sat, 17 Aug 2024 15:57:18 -0400 Subject: [PATCH] stop being a dumbass --- Cargo.lock | 4 +- client/src/components/websocket.rs | 2 +- server/src/game_handler.rs | 113 +++++++++++++++++------------ 3 files changed, 69 insertions(+), 50 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 32fcb95..5147cb8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2098,9 +2098,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.39.2" +version = "1.39.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "daa4fb1bc778bd6f04cbfc4bb2d06a7396a8f299dc33ea1900cedaa316f467b1" +checksum = "9babc99b9923bfa4804bd74722ff02c0381021eafa4db9949217e3be8e84fff5" dependencies = [ "backtrace", "bytes", diff --git a/client/src/components/websocket.rs b/client/src/components/websocket.rs index cd43eed..0580d7e 100644 --- a/client/src/components/websocket.rs +++ b/client/src/components/websocket.rs @@ -45,7 +45,7 @@ pub fn Websocket() -> impl IntoView { open, close, .. - } = use_websocket::("ws://0.0.0.0:3030/websocket"); + } = use_websocket::("ws://10.0.0.107:3030/websocket"); provide_context(WebSocketContext::new( ready_state, diff --git a/server/src/game_handler.rs b/server/src/game_handler.rs index 48204f5..2b9d02e 100644 --- a/server/src/game_handler.rs +++ b/server/src/game_handler.rs @@ -131,9 +131,13 @@ impl GameHandler { host: host.clone(), packs: new_game.packs, }; + tracing::debug!("{:#?}", host.clone().read().unwrap().uuid); // Create game using manifest - let new_game_object = Game::new(self.state.clone(), manifest); + let mut new_game_object = Game::new(self.state.clone(), manifest); + + // Don't forget to create the host player!!! + new_game_object.create_player(host.clone()); // Create update for user's game view let meta = GameStateMeta { @@ -201,8 +205,8 @@ impl GameHandler { /// Card Set #[derive(Debug)] struct CardSet { - white: Option>>, - black: Option>>, + white: Option>>, + black: Option>>, } /// Card Packs @@ -212,18 +216,18 @@ pub struct CardPacks { unofficial: HashMap, } -/// A white card +/// A raw white card as it exists in the source json #[derive(Debug, Deserialize)] -struct CardWhite { +struct CardWhiteFromJSON { /// Card text text: String, /// ID of the pack it came from pack: u8, } -/// A black card +/// A raw black card as it exists in the source json #[derive(Debug, Deserialize)] -struct CardBlack { +struct CardBlackFromJSON { /// Card text text: String, /// Amount of cards to submit for judging @@ -232,6 +236,26 @@ struct CardBlack { pack: u8, } +/// A processed white card for use server-side +#[derive(Debug)] +struct CardWhiteWithID { + /// Unique identifier + uuid: Uuid, + /// Card text + text: String, +} + +/// A processed black card for use server-side +#[derive(Debug)] +struct CardBlackWithID { + /// Unique identifier + uuid: Uuid, + /// Card text + text: String, + /// Amount of cards to submit for judging + pick: u8, +} + /// A card pack #[derive(Debug, Deserialize)] struct CardPack { @@ -240,9 +264,9 @@ struct CardPack { /// Whether or not this is an official card pack official: bool, /// White card data - white: Option>, + white: Option>, /// Black card data - black: Option>, + black: Option>, } /// Internal manifest for making a new game @@ -262,9 +286,9 @@ pub struct Player { /// Pointer to user user: Arc>, /// The player's hand - white: Vec>, + white: Vec>, /// The player's wins - black: Vec>, + black: Vec>, } /// The game object @@ -277,12 +301,12 @@ pub struct Game { /// The host user of the game pub host: Arc>, /// White draw pile - white: Vec>, + pub white: Vec>, /// Black draw pile - black: Vec>, + black: Vec>, pub players: HashMap, /// Black card for the current round - current_black: Arc, + current_black: Arc, pub packs: Vec, } @@ -311,7 +335,11 @@ impl Game { } // Draw first black card - let current_black = black.swap_remove((0..black.len()).choose(&mut thread_rng()).unwrap()); + let current_black = black.swap_remove( + (0..black.len()) + .choose(&mut thread_rng()) + .expect("No black cards to draw from!"), + ); // These are at the largest size they should ever be white.shrink_to_fit(); @@ -331,7 +359,7 @@ impl Game { } /// Draw one white card at random from play deck. - fn draw_one_white(&mut self) -> Option> { + fn draw_one_white(&mut self) -> Option> { let deck = &mut self.white; if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) { @@ -380,7 +408,7 @@ pub fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> { let mut unofficial_meta: Vec = vec![]; // Unpack the json - for set in jayson { + for sets in jayson { let mut num_white = 0; let mut num_black = 0; @@ -393,55 +421,47 @@ pub fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> { let mut pack: Option = Option::None; // Process white cards if there are any - if let Some(ref white) = set.white { + if let Some(ref white) = sets.white { num_white = white.len(); if num_white > 0 { pack = Some(white[0].pack); - newset.white = Some( - set.white - .unwrap() - .iter() - .map(|card| { - Arc::new(CardWhite { - text: card.text.clone(), - pack: card.pack.clone(), - }) - }) - .collect(), - ); + let mut white_buf = vec![]; + for card in sets.white.unwrap() { + white_buf.push(Arc::new(CardWhiteWithID { + uuid: Uuid::now_v7(), + text: card.text, + })); + } + newset.white = Some(white_buf); } } // Process black cards if there are any - if let Some(ref black) = set.black { + if let Some(ref black) = sets.black { num_black = black.len(); if num_black > 0 { pack = Some(black[0].pack); - newset.black = Some( - set.black - .unwrap() - .iter() - .map(|card| { - Arc::new(CardBlack { - text: card.text.clone(), - pick: card.pick.clone(), - pack: card.pack.clone(), - }) - }) - .collect(), - ); + let mut black_buf = vec![]; + for card in sets.black.unwrap() { + black_buf.push(Arc::new(CardBlackWithID { + uuid: Uuid::now_v7(), + text: card.text, + pick: card.pick, + })); + } + newset.black = Some(black_buf); } } // Start repackaging let meta = CardPackMeta { - name: set.name, + name: sets.name, pack: pack.expect("No card pack number!"), num_white, num_black, }; - if set.official { + if sets.official { official_meta.push(meta); official.insert(pack.unwrap(), newset); } else { @@ -456,7 +476,6 @@ pub fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> { official_meta.shrink_to_fit(); unofficial_meta.shrink_to_fit(); - // Package for export let packs = CardPacks { official,