From b0d5fa74b616e5144be8e92653084bbfa73bc8a3 Mon Sep 17 00:00:00 2001 From: David Gay Date: Sun, 1 Oct 2023 23:08:55 -0400 Subject: Use "step" instead of "element", and get tables working --- src/data/random_tables/ua_magic_items.yaml | 14 +++++++------- src/random_tables.rs | 26 ++++++++++++++------------ 2 files changed, 21 insertions(+), 19 deletions(-) (limited to 'src') 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, + steps: Vec, } #[derive(Debug, Deserialize)] -enum TableRowElement { - Text(String), - Table(String), +struct TableRowStep { + text: Option, + table: Option, } 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(); } -- cgit v1.2.3