This commit is contained in:
Adam 2024-04-05 22:38:41 -04:00
parent de9c32f776
commit 5b1475e84b
8 changed files with 334 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
target/

155
Cargo.lock generated Normal file
View file

@ -0,0 +1,155 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cards"
version = "0.1.0"
dependencies = [
"rand",
"serde",
"serde_json",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "itoa"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro2"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "ryu"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1"
[[package]]
name = "serde"
version = "1.0.197"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.197"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.115"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "2.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

9
Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "cards"
version = "0.1.0"
edition = "2021"
[dependencies]
rand = "0.8.5"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.115"

View file

1
data/cah-cards-full.json Normal file

File diff suppressed because one or more lines are too long

1
data/test.json Normal file

File diff suppressed because one or more lines are too long

17
readme.md Normal file
View file

@ -0,0 +1,17 @@
# Cards
I guess I'm making an API for card games that can exist in a chat-bot type frontend or a web page frontend
I aim to support Cards Against Humanity and Blackjack on Discord.
## TODO:
* finish CAH
* create and maintain own CAH card db to support card editing.. hash every card on the way in and use that as a unique id to prevent duplicating edited cards on a future merge?
* ratatui
* serenity
* make a web ui when discord can't display a hand privately without a DM
* telegram?
* Blackjack
* ...
* a lot

150
src/main.rs Normal file
View file

@ -0,0 +1,150 @@
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(())
}