summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gay <eapoems@riseup.net>2023-10-01 23:08:55 -0400
committerDavid Gay <eapoems@riseup.net>2023-10-01 23:08:55 -0400
commitb0d5fa74b616e5144be8e92653084bbfa73bc8a3 (patch)
treed29ac6d377676a105366cd43b576f6c92110620e
parent3252e2622fddff6a74bf02d161b1110eb88a2b41 (diff)
Use "step" instead of "element", and get tables working
-rw-r--r--src/data/random_tables/ua_magic_items.yaml14
-rw-r--r--src/random_tables.rs26
2 files changed, 21 insertions, 19 deletions
diff --git a/src/data/random_tables/ua_magic_items.yaml b/src/data/random_tables/ua_magic_items.yaml
index 98df6ef..f0f9030 100644
--- a/src/data/random_tables/ua_magic_items.yaml
+++ b/src/data/random_tables/ua_magic_items.yaml
@@ -2,23 +2,23 @@ ua_magic_items:
formula: d100
rows:
- roll: 1-20
- elements:
+ steps:
- text: "Potion"
- roll: 21-35
- elements:
+ steps:
- text: "Scroll"
- roll: 36-40
- elements:
+ steps:
- text: "Ring"
- roll: 46-60
- elements:
+ steps:
- text: "Miscellaneous Magic"
- roll: 61-75
- elements:
+ steps:
- text: "Armor & Shields"
- roll: 76-86
- elements:
+ steps:
- text: "Swords"
- roll: 87-100
- elements:
+ steps:
- text: "Miscellaneous Weapons"
diff --git a/src/random_tables.rs b/src/random_tables.rs
index f801fc9..c1ffdcb 100644
--- a/src/random_tables.rs
+++ b/src/random_tables.rs
@@ -14,13 +14,13 @@ struct RandomTable {
#[derive(Debug, Deserialize)]
struct TableRow {
roll: String,
- elements: Vec<TableRowElement>,
+ steps: Vec<TableRowStep>,
}
#[derive(Debug, Deserialize)]
-enum TableRowElement {
- Text(String),
- Table(String),
+struct TableRowStep {
+ text: Option<String>,
+ table: Option<String>,
}
pub struct RandomTables {
@@ -43,15 +43,17 @@ impl RandomTables {
if let Some((start, end)) = RandomTables::parse_roll(&table_row.roll) {
if start <= roll_result.total() && roll_result.total() <= end {
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);
- }
+ for step in &table_row.steps {
+ if let Some(text) = &step.text {
+ output_text.push_str(text);
}
- output_text.push_str(" ");
+
+ if let Some(table) = &step.table {
+ let inner_output = self.roll_table(table);
+ output_text.push_str(&inner_output);
+ }
+
+ output_text.push_str("\n");
}
return output_text.trim().to_string();
}