cards/src/main.rs

144 lines
3.5 KiB
Rust
Raw Normal View History

2024-04-06 21:28:25 -04:00
use rand::prelude::IteratorRandom;
use rand::thread_rng;
2024-04-05 22:38:41 -04:00
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>>,
}
2024-04-06 21:28:25 -04:00
#[derive(Default)]
struct CAHPlayer {
// white: Vec<CAHCardWhite>,
// black: Vec<CAHCardBlack>,
}
2024-04-05 22:38:41 -04:00
#[derive(Default)]
struct CAHGame {
2024-04-06 21:28:25 -04:00
white: Vec<CAHCardWhite>,
black: Vec<CAHCardBlack>,
2024-04-05 22:38:41 -04:00
// white_discard: Vec<CAHCardWhite>,
// black_discard: Vec<CAHCardBlack>,
// game_active: bool,
2024-04-06 21:28:25 -04:00
players: Vec<CAHPlayer>,
// czar: &CAHPlayer,
// current_black: Option<CAHCardBlack>,
2024-04-05 22:38:41 -04:00
}
impl CAHGame {
fn build_decks(&mut self, cards_json: Vec<CAHCardSet>) {
for pack in cards_json {
2024-04-06 21:28:25 -04:00
if let Some(white) = pack.white {
self.white.extend(white)
2024-04-05 22:38:41 -04:00
}
2024-04-06 21:28:25 -04:00
if let Some(black) = pack.black {
self.black.extend(black)
2024-04-05 22:38:41 -04:00
}
}
}
fn deck_counts(&self) {
println!(
"Deck Counts:\n {} White cards\n {} Black cards",
2024-04-06 21:28:25 -04:00
self.white.len(),
self.black.len()
2024-04-05 22:38:41 -04:00
);
}
2024-04-06 21:28:25 -04:00
fn draw_one_white(&mut self) -> Result<CAHCardWhite> {
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,
})
}
2024-04-05 22:38:41 -04:00
}
2024-04-06 21:28:25 -04:00
fn draw_one_black(&mut self) -> Result<CAHCardBlack> {
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!");
2024-04-05 22:38:41 -04:00
}
}
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 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)?;
let mut cah_game = CAHGame {
..Default::default()
};
cah_game.build_decks(cards_json);
cah_game.deck_counts();
2024-04-06 21:28:25 -04:00
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);
}
2024-04-05 22:38:41 -04:00
2024-04-06 21:28:25 -04:00
cah_game.deck_counts();
2024-04-05 22:38:41 -04:00
Ok(())
}