From d15c9d08f88932287b234fe7c9ae6196a7e42b78 Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Tue, 9 Apr 2024 04:51:30 -0400 Subject: [PATCH] start cleaning up by making a mess again --- Cargo.lock | 4 +- parse.js | 19 +++++ src/CAHd_game.rs | 141 +++++++++++++++++++++++++++++++++ src/main.rs | 199 +++++++++-------------------------------------- 4 files changed, 200 insertions(+), 163 deletions(-) create mode 100644 parse.js create mode 100644 src/CAHd_game.rs diff --git a/Cargo.lock b/Cargo.lock index 46810a0..acd9c95 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,9 +19,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "getrandom" -version = "0.2.12" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" dependencies = [ "cfg-if", "libc", diff --git a/parse.js b/parse.js new file mode 100644 index 0000000..bba4a22 --- /dev/null +++ b/parse.js @@ -0,0 +1,19 @@ +const data = require("./data/cah-cards-full.json"); + +let whites = []; +let blacks = []; + +for (pack of data) { + console.log(pack.name); + console.log("white cards: " + pack.white.length); + if (pack.white) { + whites.concat(pack.white); + } + console.log("black cards: " + pack.black.length); + if (pack.white) { + blacks.concat(pack.black); + } +} + +console.log(whites.length); +console.log(blacks.length); diff --git a/src/CAHd_game.rs b/src/CAHd_game.rs new file mode 100644 index 0000000..dfc3b52 --- /dev/null +++ b/src/CAHd_game.rs @@ -0,0 +1,141 @@ +use rand::prelude::IteratorRandom; +use rand::thread_rng; +use serde::{Deserialize, Serialize}; +use serde_json::Result; + +/// A CAH white card +#[derive(Serialize, Deserialize)] +struct CAHCardWhite { + /// Card text + text: String, + /// ID of the pack it came from + pack: u8, +} + +/// A CAH black card +#[derive(Serialize, Deserialize)] +struct CAHCardBlack { + /// Card text + text: String, + /// Amount of cards to submit for judging + pick: u8, + /// ID of the pack it came from + pack: u8, +} + +/// A CAH pack +#[derive(Serialize, Deserialize)] +pub struct CAHCardSet { + /// Name of the pack + name: String, + /// Pack Description + description: Option, + /// Whether or not this is an official card pack + official: bool, + /// White card data + white: Option>, + /// Black card data + black: Option>, + } + +/// A struct that represents a player +#[derive(Default)] +pub struct CAHPlayer { + /// Is this player host? + pub host: bool, + /// The player's hand + pub white: Vec, + /// The player's wins + pub black: Vec, +} + +/// The game master +#[derive(Default)] +struct CAHGame { + /// White draw pile + white: Vec, + /// Black draw pile + black: Vec, + /// White discard pile + white_discard: Vec, + /// Black discard pile + black_discard: Vec, + /// Indicates game active/game over + game_active: bool, + /// List of current players + players: Vec, + // /// Reference to current card czar + // czar: &CAHPlayer, + /// Black card for the current round + current_black: Option, +} + +impl CAHGame { + /// Build game decks from input data for game start. + /// This should only run once and at startup. + fn build_decks(&mut self, cards_json: Vec) { + for pack in cards_json { + if let Some(white) = pack.white { + self.white.extend(white) + } + if let Some(black) = pack.black { + self.black.extend(black) + } + } + } + + /// Log counts of current drawable cards + /// For testing + fn deck_counts(&self) { + println!( + "Deck Counts:\n {} White cards\n {} Black cards", + self.white.len(), + self.black.len() + ); + } + + /// Draw one white card at random from play deck. + fn draw_one_white(&mut self) -> Result { + let deck = &mut self.white; + + // this feels sloppy + if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) { + Ok(deck.swap_remove(index)) + } else { + Ok(CAHCardWhite { + text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(), + pack: 0, + }) + } + } + + /// Draw one black card at random from play deck. + fn draw_one_black(&mut self) -> Result { + let deck = &mut self.black; + + // this feels sloppy + if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) { + Ok(deck.swap_remove(index)) + } else { + Ok(CAHCardBlack { + text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(), + pick: 0, + pack: 0, + }) + } + } + + /// Deal a black card and use it for the current round + /// Returns a reference to the card + fn deal_black(&mut self) -> Result<&Option> { + self.current_black = Some(self.draw_one_black()?); + + Ok(&self.current_black) + } + + /// Create a new player and add them to the game. + fn create_player(&mut self, player: CAHPlayer) { + self.players.push(player); + println!("Player Created!"); + } +} diff --git a/src/main.rs b/src/main.rs index 0017b85..987f7f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,143 +1,9 @@ -use rand::prelude::IteratorRandom; -use rand::thread_rng; -use serde::{Deserialize, Serialize}; use serde_json::Result; use std::fs; -/// A CAH white card -#[derive(Serialize, Deserialize)] -struct CAHCardWhite { - /// Card text - text: String, - /// ID of the pack it came from - pack: u8, -} - -/// A CAH black card -#[derive(Serialize, Deserialize)] -struct CAHCardBlack { - /// Card text - text: String, - /// Amount of cards to submit for judging - pick: u8, - /// ID of the pack it came from - pack: u8, -} - -/// A CAH pack -#[derive(Serialize, Deserialize)] -struct CAHCardSet { - /// Name of the pack - name: String, - /// Pack Description - description: Option, - /// Whether or not this is an official card pack - official: bool, - /// White card data - white: Option>, - /// Black card data - black: Option>, -} - -/// A struct that represents a player -#[derive(Default)] -struct CAHPlayer { - /// The player's hand - white: Vec, - /// The player's wins - black: Vec, -} - -/// The game master -#[derive(Default)] -struct CAHGame { - /// White draw pile - white: Vec, - /// Black draw pile - black: Vec, - /// White discard pile - white_discard: Vec, - /// Black discard pile - black_discard: Vec, - /// Indicates game active/game over - game_active: bool, - /// List of current players - players: Vec, - // /// Reference to current card czar - // czar: &CAHPlayer, - /// Black card for the current round - current_black: Option, -} - -impl CAHGame { - /// Build game decks from input data for game start. - /// This should only run once and at startup. - fn build_decks(&mut self, cards_json: Vec) { - for pack in cards_json { - if let Some(white) = pack.white { - self.white.extend(white) - } - if let Some(black) = pack.black { - self.black.extend(black) - } - } - } - - /// Log counts of current drawable cards - /// For testing - fn deck_counts(&self) { - println!( - "Deck Counts:\n {} White cards\n {} Black cards", - self.white.len(), - self.black.len() - ); - } - - /// Draw one white card at random from play deck. - fn draw_one_white(&mut self) -> Result { - let deck = &mut self.white; - - // this feels sloppy - if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) { - Ok(deck.swap_remove(index)) - } else { - Ok(CAHCardWhite { - text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(), - pack: 0, - }) - } - } - - /// Draw one black card at random from play deck. - fn draw_one_black(&mut self) -> Result { - let deck = &mut self.black; - - // this feels sloppy - if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) { - Ok(deck.swap_remove(index)) - } else { - Ok(CAHCardBlack { - text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(), - pick: 0, - pack: 0, - }) - } - } - - /// Deal a black card and use it for the current round - /// Returns a reference to the card - fn deal_black(&mut self) -> Result<&Option> { - self.current_black = Some(self.draw_one_black()?); - - Ok(&self.current_black) - } - - /// Create a new player and add them to the game. - fn create_player(&mut self, player: CAHPlayer) { - self.players.push(player); - println!("Player Created!"); - } -} +#[allow(non_snake_case)] +pub mod CAHd_game; +use crate::CAHd_game::*; /// Parse json for card data fn load_json(path: &str) -> Result> { @@ -152,38 +18,49 @@ fn main() -> Result<()> { let cards_input_path: &str = "data/cah-cards-full.json"; let cards_json: Vec = load_json(cards_input_path)?; - // setup - let mut cah_game = CAHGame { + // player 0 (host) + let adam: CAHPlayer = CAHPlayer { + host: true, ..Default::default() }; - cah_game.build_decks(cards_json); - cah_game.deck_counts(); - cah_game.create_player(CAHPlayer { - ..Default::default() - }); + // create game with/for player 0 + + + println!("{:?}", adam.host); + + // setup + // let mut cah_game = CAHGame { + // ..Default::default() + // }; + // cah_game.build_decks(cards_json); + // cah_game.deck_counts(); + // cah_game.create_player(CAHPlayer { + // ..Default::default() + // }); + // // sloppy ui stuff - let div = "--------------------------"; + // let div = "--------------------------"; // deal black - if let Some(black_card) = cah_game.deal_black()? { - println!( - "{}\nPick {}\n {}\n{}", - div, - black_card.pick.to_string(), - black_card.text, - div, - ); - } + // if let Some(black_card) = cah_game.deal_black()? { + // println!( + // "{}\nPick {}\n {}\n{}", + // div, + // black_card.pick.to_string(), + // black_card.text, + // div, + // ); + // } // deal white - for i in 1..11 { - let card = cah_game.draw_one_white()?; - println!("{} {}", i, card.text); - } - println!("{}", div); - - cah_game.deck_counts(); + // for i in 1..11 { + // let card = cah_game.draw_one_white()?; + // println!("{} {}", i, card.text); + // } + // println!("{}", div); + // + // cah_game.deck_counts(); // fin Ok(())