Change database format for storing spells and items.
Additionally, use a single O(N) lookup on the merged spells/items list.
... | ... |
@@ -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 |
... | ... |
@@ -22,29 +18,21 @@ |
22 | 18 |
[x] |
23 | 19 |
(assoc x :license-notice ogl-notice)) |
24 | 20 |
|
25 |
-(def spells |
|
26 |
- (-> "5e-database/5e-SRD-Spells.json" |
|
27 |
- rc/inline |
|
28 |
- (#(.parse js/JSON %)) |
|
29 |
- (js->clj :keywordize-keys true) |
|
30 |
- (as-> s (map add-ogl-notice s)) |
|
31 |
- (hashmap-by :index))) |
|
32 |
- |
|
21 |
+(def srd-data {:spells (rc/inline "5e-database/5e-SRD-Spells.json") |
|
22 |
+ :equipment (rc/inline "5e-database/5e-SRD-Equipment.json")}) |
|
33 | 23 |
|
34 |
-(def equipment |
|
35 |
- (-> "5e-database/5e-SRD-Equipment.json" |
|
36 |
- rc/inline |
|
37 |
- (#(.parse js/JSON %)) |
|
38 |
- (js->clj :keywordize-keys true) |
|
39 |
- (as-> s (map add-ogl-notice s)) |
|
40 |
- (hashmap-by :index))) |
|
24 |
+(defn- parse-srd-data [raw-data] |
|
25 |
+ (as-> raw-data x |
|
26 |
+ (.parse js/JSON x) |
|
27 |
+ (js->clj x :keywordize-keys true) |
|
28 |
+ (map add-ogl-notice x))) |
|
41 | 29 |
|
30 |
+(defn- fmap [f m] (zipmap (keys m) (map f (vals m)))) |
|
42 | 31 |
|
43 | 32 |
(def default-db |
44 | 33 |
{:name "DMR" |
45 | 34 |
:cmdline {:current "" |
46 | 35 |
:selected-history nil |
47 | 36 |
:history []} |
48 |
- :spells spells |
|
49 |
- :equipment equipment |
|
37 |
+ :entities (fmap parse-srd-data srd-data) |
|
50 | 38 |
:license open-game-license}) |
... | ... |
@@ -1,6 +1,6 @@ |
1 | 1 |
(ns dmr.events |
2 | 2 |
(:require |
3 |
- [clojure.string :refer [trim]] |
|
3 |
+ [clojure.string :refer [lower-case split join]] |
|
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,40 @@ |
11 | 11 |
(fn-traced [_ _] |
12 | 12 |
db/default-db)) |
13 | 13 |
|
14 |
+(defn single-space [s] |
|
15 |
+ (as-> s x |
|
16 |
+ (split x " ") |
|
17 |
+ (filter not-empty x) |
|
18 |
+ (join " " x))) |
|
19 |
+ |
|
20 |
+(defn normalize-cmdline [cmdline] |
|
21 |
+ (-> cmdline single-space lower-case)) |
|
22 |
+ |
|
14 | 23 |
(re-frame/reg-event-db |
15 | 24 |
::update-cmdline |
16 | 25 |
(fn-traced [db [_ current]] |
17 | 26 |
(assoc-in db [:cmdline :current] current))) |
18 | 27 |
|
28 |
+(defn get-using [f coll] |
|
29 |
+ (->> coll (filter f) first)) |
|
30 |
+ |
|
31 |
+(defn- concatv [m] |
|
32 |
+ (->> m vals (apply concat))) |
|
33 |
+ |
|
19 | 34 |
(defn execute [db cmdline] |
20 | 35 |
(let [append-history #(update-in db [:cmdline :history] conj {:input cmdline :output %}) |
21 |
- spells (db :spells) |
|
22 |
- equipment (db :equipment) |
|
23 |
- cmd (keyword cmdline)] |
|
36 |
+ entities (db :entities) |
|
37 |
+ lcc (normalize-cmdline cmdline) |
|
38 |
+ get-using-name (partial get-using #(some-> % :name normalize-cmdline (= lcc)))] |
|
24 | 39 |
; TODO: Replace with the 'match' macro |
25 | 40 |
(cond |
26 | 41 |
(empty? cmdline) db |
27 | 42 |
(= "clear" cmdline) (assoc-in db [:cmdline :history] []) |
28 | 43 |
(= "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)) |
|
44 |
+ (= "spells" cmdline) (-> entities :spells append-history) |
|
45 |
+ (= "items" cmdline) (-> entities :equipment append-history) |
|
46 |
+ ; TODO: refactor this to avoid 2 lookups (cond -> or) |
|
47 |
+ (-> entities concatv (get-using-name)) (-> entities concatv get-using-name append-history) |
|
33 | 48 |
:else (append-history {:err (str "Unknown command '" cmdline "'")})))) |
34 | 49 |
|
35 | 50 |
(re-frame/reg-fx |
... | ... |
@@ -84,7 +99,7 @@ |
84 | 99 |
::submit-cmd |
85 | 100 |
(fn [cofx [_ prompt]] |
86 | 101 |
(let [db (cofx :db) |
87 |
- cmdline (some-> db :cmdline :current trim)] |
|
102 |
+ cmdline (some-> db :cmdline :current)] |
|
88 | 103 |
{:db |
89 | 104 |
(-> db |
90 | 105 |
(update-in [:cmdline :current] (constantly nil)) |