docs n stuff

This commit is contained in:
Adam 2024-04-06 22:38:00 -04:00
parent 7c2c258014
commit fd30fe07cd

View file

@ -4,47 +4,74 @@ use serde::{Deserialize, Serialize};
use serde_json::Result; use serde_json::Result;
use std::fs; use std::fs;
/// A CAH white card
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct CAHCardWhite { struct CAHCardWhite {
/// Card text
text: String, text: String,
/// ID of the pack it came from
pack: u8, pack: u8,
} }
/// A CAH black card
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct CAHCardBlack { struct CAHCardBlack {
/// Card text
text: String, text: String,
/// Amount of cards to submit for judging
pick: u8, pick: u8,
/// ID of the pack it came from
pack: u8, pack: u8,
} }
/// A CAH pack
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct CAHCardSet { struct CAHCardSet {
/// Name of the pack
name: String, name: String,
/// Pack Description
description: Option<String>, description: Option<String>,
/// Whether or not this is an official card pack
official: bool, official: bool,
/// White card data
white: Option<Vec<CAHCardWhite>>, white: Option<Vec<CAHCardWhite>>,
/// Black card data
black: Option<Vec<CAHCardBlack>>, black: Option<Vec<CAHCardBlack>>,
} }
/// A struct that represents a player
#[derive(Default)] #[derive(Default)]
struct CAHPlayer { struct CAHPlayer {
// white: Vec<CAHCardWhite>, /// The player's hand
// black: Vec<CAHCardBlack>, white: Vec<CAHCardWhite>,
/// The player's wins
black: Vec<CAHCardBlack>,
} }
/// The game master
#[derive(Default)] #[derive(Default)]
struct CAHGame { struct CAHGame {
/// White draw pile
white: Vec<CAHCardWhite>, white: Vec<CAHCardWhite>,
/// Black draw pile
black: Vec<CAHCardBlack>, black: Vec<CAHCardBlack>,
// white_discard: Vec<CAHCardWhite>, /// White discard pile
// black_discard: Vec<CAHCardBlack>, white_discard: Vec<CAHCardWhite>,
// game_active: bool, /// Black discard pile
black_discard: Vec<CAHCardBlack>,
/// Indicates game active/game over
game_active: bool,
/// List of current players
players: Vec<CAHPlayer>, players: Vec<CAHPlayer>,
// /// Reference to current card czar
// czar: &CAHPlayer, // czar: &CAHPlayer,
// current_black: Option<CAHCardBlack>, /// Black card for the current round
current_black: Option<CAHCardBlack>,
} }
impl CAHGame { 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<CAHCardSet>) { fn build_decks(&mut self, cards_json: Vec<CAHCardSet>) {
for pack in cards_json { for pack in cards_json {
if let Some(white) = pack.white { if let Some(white) = pack.white {
@ -56,6 +83,8 @@ impl CAHGame {
} }
} }
/// Log counts of current drawable cards
/// For testing
fn deck_counts(&self) { fn deck_counts(&self) {
println!( println!(
"Deck Counts:\n {} White cards\n {} Black cards", "Deck Counts:\n {} White cards\n {} Black cards",
@ -64,6 +93,7 @@ impl CAHGame {
); );
} }
/// Draw one white card at random from play deck.
fn draw_one_white(&mut self) -> Result<CAHCardWhite> { fn draw_one_white(&mut self) -> Result<CAHCardWhite> {
let deck = &mut self.white; let deck = &mut self.white;
@ -78,6 +108,7 @@ impl CAHGame {
} }
} }
/// Draw one black card at random from play deck.
fn draw_one_black(&mut self) -> Result<CAHCardBlack> { fn draw_one_black(&mut self) -> Result<CAHCardBlack> {
let deck = &mut self.black; let deck = &mut self.black;
@ -92,13 +123,23 @@ impl CAHGame {
}) })
} }
} }
/// Deal a black card and use it for the current round
/// Returns a reference to the card
fn deal_black(&mut self) -> Result<&Option<CAHCardBlack>> {
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) { fn create_player(&mut self, player: CAHPlayer) {
self.players.push(player); self.players.push(player);
println!("Player Created!"); println!("Player Created!");
} }
} }
/// Parse json for card data
fn load_json(path: &str) -> Result<Vec<CAHCardSet>> { fn load_json(path: &str) -> Result<Vec<CAHCardSet>> {
let data: String = fs::read_to_string(path).expect("Error reading file"); let data: String = fs::read_to_string(path).expect("Error reading file");
let jayson: Vec<CAHCardSet> = serde_json::from_str(&data)?; let jayson: Vec<CAHCardSet> = serde_json::from_str(&data)?;
@ -107,37 +148,43 @@ fn load_json(path: &str) -> Result<Vec<CAHCardSet>> {
} }
fn main() -> Result<()> { fn main() -> Result<()> {
// let cards_input_path: &str = "data/test.json"; // choose decks
let cards_input_path: &str = "data/cah-cards-full.json"; let cards_input_path: &str = "data/cah-cards-full.json";
let cards_json: Vec<CAHCardSet> = load_json(cards_input_path)?; let cards_json: Vec<CAHCardSet> = load_json(cards_input_path)?;
// setup
let mut cah_game = CAHGame { let mut cah_game = CAHGame {
..Default::default() ..Default::default()
}; };
cah_game.build_decks(cards_json); cah_game.build_decks(cards_json);
cah_game.deck_counts(); cah_game.deck_counts();
cah_game.create_player(CAHPlayer { cah_game.create_player(CAHPlayer {
..Default::default() ..Default::default()
}); });
// deal black // sloppy ui stuff
let black_card = cah_game.draw_one_black()?;
let div = "--------------------------"; let div = "--------------------------";
println!(
"{}\nPick {}\n {}\n{}", // deal black
div, if let Some(black_card) = cah_game.deal_black()? {
black_card.pick.to_string(), println!(
black_card.text, "{}\nPick {}\n {}\n{}",
div, div,
); black_card.pick.to_string(),
black_card.text,
div,
);
}
// deal white // deal white
for i in 1..11 { for i in 1..11 {
let card = cah_game.draw_one_white()?; let card = cah_game.draw_one_white()?;
println!("{} {}", i, card.text); println!("{} {}", i, card.text);
} }
println!("{}", div);
cah_game.deck_counts(); cah_game.deck_counts();
// fin
Ok(()) Ok(())
} }