From 3da1174673379b71d04734e42ec6ad1150bdd128 Mon Sep 17 00:00:00 2001 From: Howard Abrams Date: Sat, 27 Feb 2021 00:21:19 -0800 Subject: [PATCH] Fix table to return nil if table not found This will make it easier for me to write functions to return values on tables that may or may not exist. --- rpgdm-tables.el | 28 ++++++++++++------------- tables/npc/name/half-elf/{mail => male} | 0 tables/npc/occupations.org | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) rename tables/npc/name/half-elf/{mail => male} (100%) diff --git a/rpgdm-tables.el b/rpgdm-tables.el index ecd9662..87c7238 100644 --- a/rpgdm-tables.el +++ b/rpgdm-tables.el @@ -76,23 +76,23 @@ value returned is a _table_ of sorts. It could be a list, which would be easy (see `rpgdm-tables--choose-list'), or it is either a freq-uency table (see `rpgdm-tables--choose-freq-table') or a dice table (see `rpgdm-tables--choose-dice-table')." - (interactive (list (completing-read "Choose from Table: " (sort (hash-table-keys rpgdm-tables) #'string-lessp)))) - - (let* ((table (gethash table-name rpgdm-tables)) - (result (cond ((dice-table-p table) (rpgdm-tables--choose-dice-table table)) - ((hash-table-p table) (rpgdm-tables--choose-freq-table table)) - ((listp table) (rpgdm-tables--choose-list table)) - (t "Error: Could choose anything from %s (internal bug?)" table-name))) - ;; Replace any dice expression in the message with an roll: - (dice-sum (lambda (dice-exp) (number-to-string (rpgdm-roll-sum dice-exp)))) - (no-dice-nums (replace-regexp-in-string rpgdm-roll-regexp dice-sum result)) - (no-alt-words (rpgdm-tables--choose-string-list no-dice-nums))) - (rpgdm-message "%s" no-alt-words))) - + (interactive (list (completing-read "Choose from Table: " + (sort (hash-table-keys rpgdm-tables) #'string-lessp)))) + (when-let ((table (gethash table-name rpgdm-tables))) + (let* ((result (cond ((dice-table-p table) (rpgdm-tables--choose-dice-table table)) + ((hash-table-p table) (rpgdm-tables--choose-freq-table table)) + ((listp table) (rpgdm-tables--choose-list table)) + (t "Error: Could choose anything from %s (internal bug?)" table-name))) + ;; Replace any dice expression in the message with an roll: + (dice-sum (lambda (dice-exp) (number-to-string (rpgdm-roll-sum dice-exp)))) + (no-dice-nums (replace-regexp-in-string rpgdm-roll-regexp dice-sum result)) + (no-alt-words (rpgdm-tables--choose-string-list no-dice-nums))) + (rpgdm-message "%s" no-alt-words)))) (defun rpgdm-tables--choose-list (lst) "Randomly choose (equal chance for any) element in LST." - (seq-random-elt lst)) + (when lst + (seq-random-elt lst))) (defun rpgdm-tables--choose-string-list (str) "Return a substituted item from _string-list_ in STR. diff --git a/tables/npc/name/half-elf/mail b/tables/npc/name/half-elf/male similarity index 100% rename from tables/npc/name/half-elf/mail rename to tables/npc/name/half-elf/male diff --git a/tables/npc/occupations.org b/tables/npc/occupations.org index d0d3e90..032f006 100644 --- a/tables/npc/occupations.org +++ b/tables/npc/occupations.org @@ -76,7 +76,7 @@ | Sailor | seldom | | Scribe | seldom | | Servant | often | - | Shepperd | scarce | + | Shepherd | scarce | | Slave | seldom | | Soldier | seldom | | Thief | seldom |