Browse code

Add event handling and styling for weapons

Joseph Weston authored on 03/12/2021 06:20:34
Showing 3 changed files
... ...
@@ -19,10 +19,13 @@
19 19
 (defn execute
20 20
     [db cmdline]
21 21
  (let [spells (db :spells)
22
+       equipment (db :equipment)
22 23
        cmd (keyword cmdline)]
24
+  ; TODO: Replace with the 'match' macro
23 25
   (cond
24 26
    (contains? spells cmd) {:spell (spells cmd)}
25
-   :else {:err (str "No spell named '" cmdline "'")})))
27
+   (contains? equipment cmd) {:equipment (equipment cmd)}
28
+   :else {:err (str "Unknown command '" cmdline "'")})))
26 29
 
27 30
 (re-frame/reg-event-db
28 31
  ::submit-cmd
... ...
@@ -87,6 +87,13 @@
87 87
 (defn- content-after [x] [(s/& s/after) {:content (str "'" x "'")}])
88 88
 (defn- content-before [x] [(s/& s/before) {:content (str "'" x "'")}])
89 89
 
90
+(def title-style
91
+  [:h1 {:margin-top :0.2rem
92
+        :margin-bottom :0.2rem
93
+        :background-color (colorscheme :dark1)
94
+        :color (colorscheme :light0-soft)
95
+        :padding :0.2rem}])
96
+
90 97
 (defclass spell-panel []
91 98
     {}
92 99
     [:h1 {:margin-top :0.2rem
... ...
@@ -103,6 +110,13 @@
103 110
      [s/first-child {:font-weight :bold}
104 111
       (content-after ": ")]])
105 112
 
113
+(defclass equipment-panel []
114
+    {})
115
+
116
+(defclass weapon-panel []
117
+    {}
118
+    title-style
119
+    [:span.kind {:font-style :italic}])
106 120
 
107 121
 (defclass error-message []
108 122
     {:background-color (colorscheme :faded-red)
... ...
@@ -2,6 +2,7 @@
2 2
   (:require
3 3
    [re-frame.core :as re-frame]
4 4
    [clojure.string :as string]
5
+   [goog.string :as gstr]
5 6
    [dmr.styles :as styles]
6 7
    [dmr.subs :as subs]
7 8
    [dmr.events :as events]))
... ...
@@ -45,6 +46,17 @@
45 46
     [s]
46 47
     (when s (str "(" s ")")))
47 48
 
49
+(defn- property-list    [& props]
50
+    (into
51
+     [:ul {:class (styles/property-list)}]
52
+     (->> props
53
+          (partition 2)
54
+          (filter second)
55
+          (map #(vector
56
+                 :li
57
+                 [:span (-> % first)]
58
+                 [:span (-> % second)])))))
59
+
48 60
 (defn spell-panel [spell]
49 61
       [:article {:class (styles/spell-panel)}
50 62
        [:h1 (:name spell)]
... ...
@@ -73,6 +85,36 @@
73 85
         [:span (-> spell :higher_level join-lines)]]])
74 86
 
75 87
 
88
+(defn equipment-panel [item]
89
+    [:article {:class (styles/equipment-panel)}
90
+     [:h1 (item :name)]])
91
+
92
+(defn weapon-panel [weapon]
93
+ (let [weapon-kind (-> weapon :weapon_range string/lower-case)
94
+       weapon-category (-> weapon :weapon_category string/lower-case)]
95
+  [:article {:class (styles/weapon-panel)}
96
+    [:h1 (weapon :name)]
97
+    [:span.kind (string/join " " [weapon-category weapon-kind "weapon"])]
98
+    (property-list
99
+     ; 'two-handed-damage' is defined for *single-handed* weapons with the "versatile"
100
+     ; property. Actual two-handed weapons have 'damage' only.
101
+     :Damage
102
+       (let [damage-type (-> weapon :damage :damage_type :name string/lower-case)
103
+             damage (-> weapon :damage :damage_dice)
104
+             two-handed-damage (some-> weapon :two_handed_damage :damage_dice)]
105
+        (if two-handed-damage
106
+          (str damage " " (parenthesize two-handed-damage) ", " damage-type)
107
+          (str damage ", " damage-type)))
108
+     :Range
109
+       (when (= weapon-kind "ranged")
110
+         (gstr/format "%d' (%d')" (-> weapon :range :normal) (-> weapon :range :long)))
111
+     :Properties
112
+       (comma-separated (->> weapon :properties (map :name) (map string/lower-case)))
113
+     :Weight
114
+       (spaced (weapon :weight) "lbs")
115
+     :Cost
116
+       (spaced (-> weapon :cost :quantity) (-> weapon :cost :unit)))]))
117
+
76 118
 (defn error-message
77 119
     [e]
78 120
     [:div {:class (styles/error-message)} e])
... ...
@@ -84,8 +126,10 @@
84 126
     [:div.output
85 127
      (cond
86 128
       (contains? x :spell) (spell-panel (x :spell))
129
+      (contains? x :equipment) (weapon-panel (x :equipment))
87 130
       (contains? x :err) (error-message (x :err)))])
88 131
 
132
+
89 133
 (defn history []
90 134
   (let [history (re-frame/subscribe [::subs/cmd-history])]
91 135
     (into