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.
This commit is contained in:
parent
07717c103d
commit
3da1174673
3 changed files with 15 additions and 15 deletions
|
|
@ -76,10 +76,10 @@ 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
|
would be easy (see `rpgdm-tables--choose-list'), or it is either
|
||||||
a freq-uency table (see `rpgdm-tables--choose-freq-table') or a
|
a freq-uency table (see `rpgdm-tables--choose-freq-table') or a
|
||||||
dice table (see `rpgdm-tables--choose-dice-table')."
|
dice table (see `rpgdm-tables--choose-dice-table')."
|
||||||
(interactive (list (completing-read "Choose from Table: " (sort (hash-table-keys rpgdm-tables) #'string-lessp))))
|
(interactive (list (completing-read "Choose from Table: "
|
||||||
|
(sort (hash-table-keys rpgdm-tables) #'string-lessp))))
|
||||||
(let* ((table (gethash table-name rpgdm-tables))
|
(when-let ((table (gethash table-name rpgdm-tables)))
|
||||||
(result (cond ((dice-table-p table) (rpgdm-tables--choose-dice-table table))
|
(let* ((result (cond ((dice-table-p table) (rpgdm-tables--choose-dice-table table))
|
||||||
((hash-table-p table) (rpgdm-tables--choose-freq-table table))
|
((hash-table-p table) (rpgdm-tables--choose-freq-table table))
|
||||||
((listp table) (rpgdm-tables--choose-list table))
|
((listp table) (rpgdm-tables--choose-list table))
|
||||||
(t "Error: Could choose anything from %s (internal bug?)" table-name)))
|
(t "Error: Could choose anything from %s (internal bug?)" table-name)))
|
||||||
|
|
@ -87,12 +87,12 @@ dice table (see `rpgdm-tables--choose-dice-table')."
|
||||||
(dice-sum (lambda (dice-exp) (number-to-string (rpgdm-roll-sum dice-exp))))
|
(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-dice-nums (replace-regexp-in-string rpgdm-roll-regexp dice-sum result))
|
||||||
(no-alt-words (rpgdm-tables--choose-string-list no-dice-nums)))
|
(no-alt-words (rpgdm-tables--choose-string-list no-dice-nums)))
|
||||||
(rpgdm-message "%s" no-alt-words)))
|
(rpgdm-message "%s" no-alt-words))))
|
||||||
|
|
||||||
|
|
||||||
(defun rpgdm-tables--choose-list (lst)
|
(defun rpgdm-tables--choose-list (lst)
|
||||||
"Randomly choose (equal chance for any) element in 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)
|
(defun rpgdm-tables--choose-string-list (str)
|
||||||
"Return a substituted item from _string-list_ in STR.
|
"Return a substituted item from _string-list_ in STR.
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,7 @@
|
||||||
| Sailor | seldom |
|
| Sailor | seldom |
|
||||||
| Scribe | seldom |
|
| Scribe | seldom |
|
||||||
| Servant | often |
|
| Servant | often |
|
||||||
| Shepperd | scarce |
|
| Shepherd | scarce |
|
||||||
| Slave | seldom |
|
| Slave | seldom |
|
||||||
| Soldier | seldom |
|
| Soldier | seldom |
|
||||||
| Thief | seldom |
|
| Thief | seldom |
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue