...
|
...
|
@@ -1,5 +1,6 @@
|
1
|
1
|
(ns dmr.events
|
2
|
2
|
(:require
|
|
3
|
+ [cljs.reader :refer [read-string]]
|
3
|
4
|
[clojure.string :refer [lower-case split join starts-with?]]
|
4
|
5
|
[re-frame.core :as re-frame]
|
5
|
6
|
[dmr.db :as db]
|
...
|
...
|
@@ -49,6 +50,22 @@
|
49
|
50
|
(defn get-using [f coll]
|
50
|
51
|
(->> coll (filter f) first))
|
51
|
52
|
|
|
53
|
+
|
|
54
|
+(defn parse-dice-roll [roll]
|
|
55
|
+ (let [dice-regex #"([1-9][0-9]*)*d([1-9][0-9]*)"
|
|
56
|
+ match (re-matches dice-regex roll)]
|
|
57
|
+ (when match
|
|
58
|
+ (let [[_ ndice nsides] match]
|
|
59
|
+ {:ndice (-> ndice (or "1") read-string)
|
|
60
|
+ :nsides (-> nsides read-string)}))))
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+(defn roll-die [n-sides] (-> n-sides (+ 1) rand-int (+ 1)))
|
|
64
|
+
|
|
65
|
+(defn roll-dice [{:keys [ndice nsides]}]
|
|
66
|
+ (vec (repeatedly ndice (partial roll-die nsides))))
|
|
67
|
+
|
|
68
|
+
|
52
|
69
|
(defn execute [db cmdline]
|
53
|
70
|
(let [append-history #(update-in db [:cmdline :history] conj {:input cmdline :output %})
|
54
|
71
|
entities (db :entities)
|
...
|
...
|
@@ -61,6 +78,7 @@
|
61
|
78
|
(= "Open Game License" cmdline) (append-history (db :license))
|
62
|
79
|
(= "spells" cmdline) (-> entities :spells append-history)
|
63
|
80
|
(= "items" cmdline) (-> entities :equipment append-history)
|
|
81
|
+ (parse-dice-roll cmdline) (->> cmdline parse-dice-roll roll-dice (hash-map :rolls) append-history)
|
64
|
82
|
; TODO: refactor this to avoid 2 lookups (cond -> or)
|
65
|
83
|
(-> entities concatv (get-using-name)) (-> entities concatv get-using-name append-history)
|
66
|
84
|
:else (append-history {:err (str "Unknown command '" cmdline "'")}))))
|