summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gay <eapoems@riseup.net>2023-10-01 02:34:16 -0400
committerDavid Gay <eapoems@riseup.net>2023-10-01 02:34:16 -0400
commitb77f46d0146d6b5b9acdb23546db2d57c37ed2af (patch)
treead13966380074bd581ce9efac956f65a38393095
parent8bc79457594a01f22f7146d3e1855de54558c531 (diff)
Draft of random tables
-rw-r--r--Cargo.lock54
-rw-r--r--Cargo.toml3
-rw-r--r--src/data/random_tables/ua_magic_items.yaml24
-rw-r--r--src/dice.rs4
-rw-r--r--src/main.rs1
-rw-r--r--src/random_tables.rs62
6 files changed, 145 insertions, 3 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 462b879..2c0bb18 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -96,9 +96,16 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -110,6 +117,28 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -170,6 +199,12 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -190,6 +225,19 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -213,6 +261,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"
source = "registry+https://github.com/rust-lang/crates.io-index"
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<TableRow>,
+}
+
+#[derive(Debug, Deserialize)]
+struct TableRow {
+ roll: String,
+ elements: Vec<TableRowElement>,
+}
+
+#[derive(Debug, Deserialize)]
+enum TableRowElement {
+ Text(String),
+ Table(String),
+}
+
+pub struct RandomTables {
+ tables: HashMap<String, RandomTable>,
+}
+
+impl RandomTables {
+ pub fn new() -> Result<Self, Box<dyn Error>> {
+ let tables_yaml = include_str!("data/random_tables/ua_magic_items.yaml");
+ let tables: HashMap<String, RandomTable> = 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()
+ }
+}