cards/src/main.rs

151 lines
3.8 KiB
Rust
Raw Normal View History

2024-04-05 22:38:41 -04:00
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<String>,
official: bool,
white: Option<Vec<CAHCardWhite>>,
black: Option<Vec<CAHCardBlack>>,
}
// struct CAHPlayer {
// white_cards: Vec<CAHCardWhite>,
// black_cards: Vec<CAHCardBlack>,
// is_czar: bool,
// }
#[derive(Default)]
struct CAHGame {
white_cards: Vec<CAHCardWhite>,
black_cards: Vec<CAHCardBlack>,
// white_discard: Vec<CAHCardWhite>,
// black_discard: Vec<CAHCardBlack>,
// game_active: bool,
// players: Vec<CAHPlayer>,
// czar: Option<*mut CAHPlayer>,
}
impl CAHGame {
fn build_decks(&mut self, cards_json: Vec<CAHCardSet>) {
// 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<Vec<CAHCardSet>> {
let data: String = fs::read_to_string(path).expect("Error reading file");
let jayson: Vec<CAHCardSet> = serde_json::from_str(&data)?;
Ok(jayson)
}
fn packs_report(json: Vec<CAHCardSet>) {
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<CAHCardSet> = 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(())
}