use rand::seq::SliceRandom; use serde::{Deserialize, Serialize}; use serde_json::Result; use std::fs; #[derive(Serialize, Deserialize, Clone)] struct CAHCardWhite { text: String, pack: u8, } #[derive(Serialize, Deserialize, Clone)] struct CAHCardBlack { text: String, pick: u8, pack: u8, } #[derive(Serialize, Deserialize, Clone)] struct CAHCardSet { name: String, description: Option, official: bool, white: Option>, black: Option>, } // struct CAHPlayer { // white_cards: Vec, // black_cards: Vec, // is_czar: bool, // } #[derive(Default)] struct CAHGame { white_cards: Vec, black_cards: Vec, // white_discard: Vec, // black_discard: Vec, // game_active: bool, // players: Vec, // czar: Option<*mut CAHPlayer>, } impl CAHGame { fn build_decks(&mut self, cards_json: Vec) { // clear deck before adding cards self.white_cards = Vec::new(); self.black_cards = Vec::new(); for pack in cards_json { if let Some(white_cards) = pack.white { for card in white_cards { self.white_cards.push(card) } } if let Some(black_cards) = pack.black { for card in black_cards { self.black_cards.push(card) } } } } fn deck_counts(&self) { println!( "Deck Counts:\n {} White cards\n {} Black cards", self.white_cards.len(), self.black_cards.len() ); } fn pick_one_white(&self) { let card = self.white_cards.choose(&mut rand::thread_rng()).unwrap(); println!( "{}\n {}", card.pack, card.text, ); } fn pick_one_black(&self) { let card = self.black_cards.choose(&mut rand::thread_rng()).unwrap(); let div = "--------------------------"; println!( "{}\n Pack: {}\n Pick: {}\n Text: {}\n{}", div, card.pack, card.pick.to_string(), card.text, div ); } } fn load_json(path: &str) -> Result> { let data: String = fs::read_to_string(path).expect("Error reading file"); let jayson: Vec = serde_json::from_str(&data)?; Ok(jayson) } fn packs_report(json: Vec) { let line = "---------------------------"; for pack in json { println!("{}", &line); let name = pack.name; println!("Name: {:?}", name); let desc = pack.description; println!("Description: {:?}", desc); let official = pack.official; println!("Official: {:?}", official); let white = pack.white.unwrap().len(); println!("White Cards: {:?}", white); let black = pack.black.unwrap().len(); println!("Black Cards: {:?}", black); } println!("{}", line); } fn main() -> Result<()> { // let cards_input_path: &str = "data/test.json"; let cards_input_path: &str = "data/cah-cards-full.json"; let cards_json: Vec = load_json(cards_input_path)?; packs_report(cards_json.clone()); let mut cah_game = CAHGame { ..Default::default() }; cah_game.build_decks(cards_json); cah_game.deck_counts(); cah_game.pick_one_black(); cah_game.pick_one_white(); cah_game.pick_one_white(); cah_game.pick_one_white(); cah_game.pick_one_white(); cah_game.pick_one_white(); cah_game.pick_one_white(); cah_game.pick_one_white(); cah_game.pick_one_white(); cah_game.pick_one_white(); cah_game.pick_one_white(); Ok(()) }