From b77f46d0146d6b5b9acdb23546db2d57c37ed2af Mon Sep 17 00:00:00 2001 From: David Gay Date: Sun, 1 Oct 2023 02:34:16 -0400 Subject: Draft of random tables --- Cargo.lock | 54 ++++++++++++++++++++++++++ Cargo.toml | 3 +- src/data/random_tables/ua_magic_items.yaml | 24 ++++++++++++ src/dice.rs | 4 +- src/main.rs | 1 + src/random_tables.rs | 62 ++++++++++++++++++++++++++++++ 6 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 src/data/random_tables/ua_magic_items.yaml create mode 100644 src/random_tables.rs diff --git a/Cargo.lock b/Cargo.lock index 462b879..2c0bb18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -96,8 +96,15 @@ dependencies = [ "clap", "rand", "serde", + "serde_yaml", ] +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "getrandom" version = "0.2.10" @@ -109,6 +116,28 @@ dependencies = [ "wasi", ] +[[package]] +name = "hashbrown" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dfda62a12f55daeae5015f81b0baea145391cb4520f86c248fc615d72640d12" + +[[package]] +name = "indexmap" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8adf3ddd720272c6ea8bf59463c04e0f93d0bbf7c5439b691bca2987e0270897" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "itoa" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" + [[package]] name = "libc" version = "0.2.148" @@ -169,6 +198,12 @@ dependencies = [ "getrandom", ] +[[package]] +name = "ryu" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" + [[package]] name = "serde" version = "1.0.188" @@ -189,6 +224,19 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_yaml" +version = "0.9.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a49e178e4452f45cb61d0cd8cebc1b0fafd3e41929e996cef79aa3aca91f574" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "strsim" version = "0.10.0" @@ -212,6 +260,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unsafe-libyaml" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f28467d3e1d3c6586d8f25fa243f544f5800fec42d97032474e17222c2b75cfa" + [[package]] name = "utf8parse" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index f2af371..345e4bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,5 @@ repository = "https://git.aikuro.net/poems/dmn" [dependencies] clap = { version = "^4.4", features = ["cargo"] } rand = "^0.8" -serde = "^1.0" +serde = { version = "^1.0", features = ["derive"] } +serde_yaml = "^0.9" diff --git a/src/data/random_tables/ua_magic_items.yaml b/src/data/random_tables/ua_magic_items.yaml new file mode 100644 index 0000000..50355d2 --- /dev/null +++ b/src/data/random_tables/ua_magic_items.yaml @@ -0,0 +1,24 @@ +ua_magic_items: + formula: d100 + rows: + - roll: 1-20 + results: + - text: "Potion" + - roll: 21-35 + results: + - text: "Scroll" + - roll: 36-40 + results: + - text: "Ring" + - roll: 46-60 + results: + - text: "Miscellaneous Magic" + - roll: 61-75 + results: + - text: "Armor & Shields" + - roll: 76-86 + results: + - text: "Swords" + - roll: 87-100 + results: + - text: "Miscellaneous Weapons" diff --git a/src/dice.rs b/src/dice.rs index ffec3b8..64a4635 100644 --- a/src/dice.rs +++ b/src/dice.rs @@ -21,7 +21,7 @@ pub struct RollResult { } impl RollResult { - fn new() -> Self { + pub fn new() -> Self { RollResult { rolls: Vec::new(), } @@ -31,7 +31,7 @@ impl RollResult { self.rolls.push(roll) } - fn total(&self) -> u32 { + pub fn total(&self) -> u32 { self.rolls.iter().map(|die_roll| die_roll.face).sum() } } diff --git a/src/main.rs b/src/main.rs index c3431cd..c1c7cb2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod cli; mod dice; +mod random_tables; fn main() { let matches = cli::cli().get_matches(); diff --git a/src/random_tables.rs b/src/random_tables.rs new file mode 100644 index 0000000..9f3e435 --- /dev/null +++ b/src/random_tables.rs @@ -0,0 +1,62 @@ +use serde::Deserialize; +use serde_yaml; +use std::collections::HashMap; +use std::error::Error; +use std::string::String; +use crate::dice; + +#[derive(Debug, Deserialize)] +struct RandomTable { + formula: String, + rows: Vec, +} + +#[derive(Debug, Deserialize)] +struct TableRow { + roll: String, + elements: Vec, +} + +#[derive(Debug, Deserialize)] +enum TableRowElement { + Text(String), + Table(String), +} + +pub struct RandomTables { + tables: HashMap, +} + +impl RandomTables { + pub fn new() -> Result> { + let tables_yaml = include_str!("data/random_tables/ua_magic_items.yaml"); + let tables: HashMap = serde_yaml::from_str(tables_yaml)?; + Ok(RandomTables { tables }) + } + + pub fn roll_table(&self, table_name: &str) -> String { + let random_table = self.tables.get(table_name); + if let Some(table) = random_table { + // TODO: Probably return an error instead of using #unwrap. + let roll_result = dice::roll_formula(&table.formula).unwrap(); + let roll = roll_result.total(); + for table_row in &table.rows { + if table_row.roll.contains(&roll.to_string()) { + let mut output_text = String::new(); + for element in &table_row.elements { + match element { + TableRowElement::Text(text) => output_text.push_str(text), + TableRowElement::Table(inner_table) => { + let inner_output = self.roll_table(inner_table); + output_text.push_str(&inner_output); + } + } + output_text.push_str(" "); + } + return output_text.trim().to_string(); + } + } + } + String::new() + } +} -- cgit v1.2.3