use rand::prelude::IteratorRandom; use rand::thread_rng; use serde::{Deserialize, Serialize}; use serde_json::Result; use std::fs; #[derive(Serialize, Deserialize)] struct CAHCardWhite { text: String, pack: u8, } #[derive(Serialize, Deserialize)] struct CAHCardBlack { text: String, pick: u8, pack: u8, } #[derive(Serialize, Deserialize)] struct CAHCardSet { name: String, description: Option, official: bool, white: Option>, black: Option>, } #[derive(Default)] struct CAHPlayer { // white: Vec, // black: Vec, } #[derive(Default)] struct CAHGame { white: Vec, black: Vec, // white_discard: Vec, // black_discard: Vec, // game_active: bool, players: Vec, // czar: &CAHPlayer, // current_black: Option, } impl CAHGame { 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) } } } fn deck_counts(&self) { println!( "Deck Counts:\n {} White cards\n {} Black cards", self.white.len(), self.black.len() ); } 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, }) } } 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, }) } } fn create_player(&mut self, player: CAHPlayer) { self.players.push(player); println!("Player Created!"); } } 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 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)?; let mut cah_game = CAHGame { ..Default::default() }; cah_game.build_decks(cards_json); cah_game.deck_counts(); cah_game.create_player(CAHPlayer { ..Default::default() }); // deal black let black_card = cah_game.draw_one_black()?; let div = "--------------------------"; 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); } cah_game.deck_counts(); Ok(()) }