Browse code

Replace single dice roller with dice roll expression evaluator

Joseph Weston authored on 24/10/2022 00:06:47
Showing 1 changed files
... ...
@@ -53,19 +53,9 @@
53 53
 (defn get-using [f coll]
54 54
   (->> coll (filter f) first))
55 55
 
56
+(defn roll-die [nsides] (-> nsides (+ 1) rand-int (+ 1)))
56 57
 
57
-(defn parse-dice-roll [roll]
58
-  (let [dice-regex #"([1-9][0-9]*)*d([1-9][0-9]*)"
59
-        match (re-matches dice-regex roll)]
60
-    (when match
61
-      (let [[_ ndice nsides] match]
62
-        {:ndice (-> ndice (or "1") read-string)
63
-         :nsides (-> nsides read-string)}))))
64
-
65
-
66
-(defn roll-die [n-sides] (-> n-sides (+ 1) rand-int (+ 1)))
67
-
68
-(defn roll-dice [{:keys [ndice nsides]}]
58
+(defn roll-dice [ndice nsides]
69 59
   (vec (repeatedly ndice (partial roll-die nsides))))
70 60
 
71 61
 (def dice-regex #"\b([1-9][0-9]*)*d([1-9][0-9]*)\b")
... ...
@@ -116,7 +106,7 @@
116 106
    (= "Open Game License" cmdline) (append-history (db :license))
117 107
    (= "spells" cmdline) (-> entities :spells append-history)
118 108
    (= "items" cmdline)  (-> entities :equipment append-history)
119
-   (parse-dice-roll cmdline) (->> cmdline parse-dice-roll roll-dice (hash-map :rolls) append-history)
109
+   (contains-dice-roll? cmdline) (->> cmdline safely-evaluate-roll-expression append-history)
120 110
    ; TODO: refactor this to avoid 2 lookups (cond -> or)
121 111
    (-> entities concatv (get-using-name)) (-> entities concatv get-using-name append-history)
122 112
    :else (append-history {:err (str "Unknown command '" cmdline "'")}))))