notes and sanity

This commit is contained in:
Adam 2024-04-12 18:23:33 -04:00
parent 48a968d800
commit 0538932ec8
2 changed files with 42 additions and 35 deletions

View file

@ -3,30 +3,6 @@ use rand::thread_rng;
use serde::{Deserialize, Serialize};
use serde_json::Result;
/// Player roles
#[derive(Debug)]
pub enum PlayerRole {
/// Player is host
Host,
/// Player is a player in a game where another player is host
Player,
/// Player is just spectating
Spectator,
}
/// A struct that represents a player
#[derive(Debug)]
pub struct CAHPlayer {
/// Player's username
pub player_name: String,
/// This player's role
pub role: PlayerRole,
/// The player's hand
pub white: Vec<CAHCardWhite>,
/// The player's wins
pub black: Vec<CAHCardBlack>,
}
/// A CAH white card
#[derive(Debug, Serialize, Deserialize)]
pub struct CAHCardWhite {
@ -62,6 +38,30 @@ pub struct CAHCardSet {
black: Option<Vec<CAHCardBlack>>,
}
/// Player roles
#[derive(Debug)]
pub enum PlayerRole {
/// Player is host
Host,
/// Player is a player in a game where another player is host
Player,
/// Player is just spectating
Spectator,
}
/// A struct that represents a player
#[derive(Debug)]
pub struct CAHPlayer {
/// Player's username
pub player_name: String,
/// This player's role
pub role: PlayerRole,
/// The player's hand
pub white: Vec<CAHCardWhite>,
/// The player's wins
pub black: Vec<CAHCardBlack>,
}
/// The game master
#[derive(Debug, Default)]
pub struct CAHGame {
@ -88,7 +88,7 @@ pub struct 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>) -> Result<()> {
for pack in cards_json {
if let Some(white) = pack.white {
self.white.extend(white)
@ -97,6 +97,8 @@ impl CAHGame {
self.black.extend(black)
}
}
Ok(())
}
pub fn new(name: String, host: CAHPlayer, decks: Vec<CAHCardSet>) -> Result<CAHGame> {
let mut game = CAHGame {
@ -105,9 +107,9 @@ impl CAHGame {
println!("Creating game {}", &name);
game.game_name = name;
game.build_decks(decks);
let _ = game.create_player(host);
let _ = game.deal_black()?;
game.build_decks(decks)?;
game.create_player(host)?;
game.deal_black()?;
Ok(game)
}
@ -154,11 +156,10 @@ 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>> {
fn deal_black(&mut self) -> Result<()> {
self.current_black = Some(self.draw_one_black()?);
Ok(&self.current_black)
Ok(())
}
/// Create a new player and add them to the game.
@ -173,7 +174,7 @@ impl CAHGame {
hand_buf.push(self.draw_one_white()?);
}
println!("Dealing hand for {}", &player.player_name);
let _ = &player.white.extend(hand_buf);
player.white.extend(hand_buf);
self.players.push(player);

View file

@ -16,7 +16,11 @@ fn load_json(path: &str) -> Result<Vec<CAHCardSet>> {
fn main() -> Result<()> {
// choose decks
let cards_input_path: &str = "data/cah-cards-full.json";
// TODO: this should be a master card database and pointers
// to the cards should be passed to the game instead of actual cards
let chosen_packs: Vec<CAHCardSet> = load_json(cards_input_path)?;
println!("{}", &chosen_packs.len());
let test_player0 = CAHPlayer {
player_name: "Adam".to_string(),
@ -33,19 +37,21 @@ fn main() -> Result<()> {
};
// make some games
// use hashmap?
let mut games: Vec<CAHGame> = vec![];
// create game with/for player 0
// TODO: make this a new game request struct
games.push(CAHGame::new(
"Test0".to_string(),
test_player0,
chosen_packs,
)?);
// ready player 1
let _ = games[0].create_player(test_player1);
// a new game request struct but this player is a player
games[0].create_player(test_player1)?;
// start round
let _ = games[0].game_start();
games[0].game_start()?;
Ok(())
}