diff options
author | David Gay <eapoems@riseup.net> | 2023-10-16 16:26:29 -0400 |
---|---|---|
committer | David Gay <eapoems@riseup.net> | 2023-10-16 16:26:29 -0400 |
commit | 84837a689e71d0e695fa016f1e667bbeb16b515b (patch) | |
tree | c03be7344660aa7db395854ffa6ef44200831108 | |
parent | 5ecd591a49c78f6059fc840ed5551ef7f5cad895 (diff) |
Make random tables a static collection
-rw-r--r-- | src/main.rs | 13 | ||||
-rw-r--r-- | src/random_tables.rs | 10 |
2 files changed, 14 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs index 9fd7963..20e4bcf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use dmn::dice; -use dmn::random_tables::RandomTables; +use dmn::random_tables::RANDOM_TABLES; use dmn::rules::ability_scores::AbilityScore; use dmn::rules::classes::CLASSES; use dmn::rules::npcs::Npc; @@ -12,15 +12,14 @@ fn main() { env_logger::init(); let matches = cli::cli().get_matches(); - let random_tables = RandomTables::new().expect("Failed to load random tables."); match matches.subcommand() { Some(("random", sub_matches)) => { let random_command = sub_matches.subcommand().unwrap(); match random_command { ("henchman", henchman_matches) => { - let class_name = random_tables.roll_table("henchman_class").to_string(); - let race_name = random_tables.roll_table("henchman_race").to_string(); + let class_name = RANDOM_TABLES.roll_table("henchman_class").to_string(); + let race_name = RANDOM_TABLES.roll_table("henchman_race").to_string(); // HACK: Need a proper way to do lookups, shouldn't rely on // downcasing the class name. This whole situation is really // indicative of the need for an architectural improvement. @@ -36,7 +35,7 @@ fn main() { }); let mut npc = Npc::new( - Some(random_tables.roll_table("npc_alignment")), + Some(RANDOM_TABLES.roll_table("npc_alignment")), Some(race_ref), Some(class_ref), None, @@ -89,7 +88,7 @@ fn main() { }; } ("magic", _) => { - let magic = random_tables.roll_table("ua_magic"); + let magic = RANDOM_TABLES.roll_table("ua_magic"); println!("{}", magic); } _ => unreachable!(), @@ -104,7 +103,7 @@ fn main() { } Some(("table", sub_matches)) => { let table_name = sub_matches.get_one::<String>("TABLE").expect("required"); - let output_text = random_tables.roll_table(table_name); + let output_text = RANDOM_TABLES.roll_table(table_name); println!("{}", output_text) } _ => unreachable!(), diff --git a/src/random_tables.rs b/src/random_tables.rs index 831fd86..9116659 100644 --- a/src/random_tables.rs +++ b/src/random_tables.rs @@ -1,6 +1,7 @@ use crate::dice; use crate::rules::classes::CLASSES; use include_dir::{include_dir, Dir}; +use lazy_static::lazy_static; use serde::Deserialize; use serde_yaml; use std::collections::HashMap; @@ -40,12 +41,17 @@ struct TableRowStep { // } // } -pub struct RandomTables { - tables: HashMap<String, RandomTable>, +lazy_static! { + pub static ref RANDOM_TABLES: RandomTables = + RandomTables::new().expect("Failed to load random tables."); } const YAML_DIR: Dir = include_dir!("src/data/random_tables/"); +pub struct RandomTables { + tables: HashMap<String, RandomTable>, +} + impl RandomTables { pub fn new() -> Result<Self, Box<dyn Error>> { let mut tables_yaml = String::new(); |