Browse code

Store spells and items in a vector, rather than hashmap

A consequence is that we have to do an O(N) lookup each time
(e.g. by name), however this could be improved in the future
by adding indexing datastructures (or maybe using a datascript
database). For the moment the performance is not a problem.

Joseph Weston authored on 12/12/2021 19:58:07
Showing 2 changed files
... ...
@@ -4,10 +4,6 @@
4 4
 ; TODO remove this duplication
5 5
 ; TODO find a way to load this data at runtime
6 6
 
7
-(defn- hashmap-by
8
-    [vec k]
9
-    (zipmap (map #(-> % k keyword) vec) vec))
10
-
11 7
 (def open-game-license
12 8
   (-> "5e-database/5e-SRD-License.json"
13 9
       rc/inline
... ...
@@ -27,8 +23,7 @@
27 23
       rc/inline
28 24
       (#(.parse js/JSON %))
29 25
       (js->clj :keywordize-keys true)
30
-      (as-> s (map add-ogl-notice s))
31
-      (hashmap-by :index)))
26
+      (as-> s (map add-ogl-notice s))))
32 27
 
33 28
 
34 29
 (def equipment
... ...
@@ -36,8 +31,7 @@
36 31
       rc/inline
37 32
       (#(.parse js/JSON %))
38 33
       (js->clj :keywordize-keys true)
39
-      (as-> s (map add-ogl-notice s))
40
-      (hashmap-by :index)))
34
+      (as-> s (map add-ogl-notice s))))
41 35
 
42 36
 
43 37
 (def default-db
... ...
@@ -1,6 +1,6 @@
1 1
 (ns dmr.events
2 2
   (:require
3
-   [clojure.string :refer [trim]]
3
+   [clojure.string :refer [trim lower-case]]
4 4
    [re-frame.core :as re-frame]
5 5
    [dmr.db :as db]
6 6
    [day8.re-frame.tracing :refer-macros [fn-traced]]))
... ...
@@ -11,25 +11,33 @@
11 11
  (fn-traced [_ _]
12 12
    db/default-db))
13 13
 
14
+(defn normalize-cmdline [x]
15
+    (-> x lower-case))
16
+
14 17
 (re-frame/reg-event-db
15 18
  ::update-cmdline
16 19
  (fn-traced [db [_ current]]
17 20
   (assoc-in db [:cmdline :current] current)))
18 21
 
22
+(defn get-using [f coll]
23
+  (->> coll (filter f) first))
24
+
19 25
 (defn execute [db cmdline]
20 26
  (let [append-history #(update-in db [:cmdline :history] conj {:input cmdline :output %})
21 27
        spells (db :spells)
22 28
        equipment (db :equipment)
23
-       cmd (keyword cmdline)]
29
+       lcc (normalize-cmdline cmdline)
30
+       get-using-name (partial get-using #(some-> % :name normalize-cmdline (= lcc)))]
24 31
   ; TODO: Replace with the 'match' macro
25 32
   (cond
26 33
    (empty? cmdline) db
27 34
    (= "clear" cmdline) (assoc-in db [:cmdline :history] [])
28 35
    (= "Open Game License" cmdline) (append-history (db :license))
29
-   (= "spells" cmdline) (append-history (vals spells))
30
-   (= "items" cmdline) (append-history (vals equipment))
31
-   (contains? spells cmd) (append-history (spells cmd))
32
-   (contains? equipment cmd) (append-history (equipment cmd))
36
+   (= "spells" cmdline) (append-history spells)
37
+   (= "items" cmdline) (append-history equipment)
38
+   ; TODO: refactor this to avoid 2 lookups (cond -> or)
39
+   (get-using-name spells) (-> spells get-using-name append-history)
40
+   (get-using-name equipment) (append-history (get-using-name equipment))
33 41
    :else (append-history {:err (str "Unknown command '" cmdline "'")}))))
34 42
 
35 43
 (re-frame/reg-fx