generated from PEZ/rn-rf-shadow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencumbrance_table.cljs
113 lines (101 loc) · 5.13 KB
/
encumbrance_table.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(ns gurps.pages.character.widgets.encumbrance-table
(:require ["twrnc" :refer [style] :rename {style tw}]
[re-frame.core :as rf]
[gurps.widgets.base :refer [text view]]
[gurps.utils.helpers :refer [->int positions]]
[gurps.utils.i18n :as i18n]))
;; TODO: use gurps.widgets.underlined-row/labelled-underlined-input widget instead
(defn labeled-underlined-row
[^js {:keys [label value style]}]
[:> view {:style #js [(tw "flex flex-row gap-1 justify-center"), style]}
[:> text {:style (tw "flex-grow")} label]
[:> text {:style (tw "underline grow-0 text-center")} value]]) ;; TODO: width should be 3 digits wide
(defn- column
[label row1 row2 row3 row4 row5]
[:> view {:style (tw "flex flex-col gap-1")}
[:> text {:style (tw "uppercase text-center font-bold")} label]
[labeled-underlined-row row1]
[labeled-underlined-row row2]
[labeled-underlined-row row3]
[labeled-underlined-row row4]
[labeled-underlined-row row5]])
(rf/reg-sub
:attributes/emcumbrance-weight-class
:<- [:items/weight]
:<- [:attributes/basic-lift]
(fn [[weight lift]]
(first (positions #{weight} (sort [weight lift (* 2 lift) (* 3 lift) (* 6 lift) (* 10 lift)])))))
(def highlight-style (tw "bg-green-100"))
(defn encumbrance-column []
(let [weight-class (some-> (rf/subscribe [:attributes/emcumbrance-weight-class]) deref)
basic-lift (some-> (rf/subscribe [:attributes/basic-lift]) deref)
light-lift (* 2 basic-lift)
medium-lift (* 3 basic-lift)
heavy-lift (* 6 basic-lift)
x-heavy-lift (* 10 basic-lift)]
[column
(i18n/label :t/encumbrance)
{:label (i18n/label :t/encumbrance-none) :value basic-lift :style (when (= 0 weight-class) highlight-style)}
{:label (i18n/label :t/encumbrance-light) :value light-lift :style (when (= 1 weight-class) highlight-style)}
{:label (i18n/label :t/encumbrance-medium) :value medium-lift :style (when (= 2 weight-class) highlight-style)}
{:label (i18n/label :t/encumbrance-heavy) :value heavy-lift :style (when (= 3 weight-class) highlight-style)}
{:label (i18n/label :t/encumbrance-x-heavy) :value x-heavy-lift :style (when (>= weight-class 4) highlight-style)}]))
(rf/reg-sub
:attributes/basic-lift
:<- [:attributes/str]
(fn [str]
(js/Math.floor (/ (* str str) 5))))
(defn move-column []
(let [weight-class (some-> (rf/subscribe [:attributes/emcumbrance-weight-class]) deref)
basic-move (some-> (rf/subscribe [:attributes/basic-move]) deref)
light-move (- basic-move 1)
medium-move (- basic-move 2)
heavy-move (- basic-move 3)
x-heavy-move (- basic-move 4)]
[column
(i18n/label :t/move)
{:label (i18n/label :t/move-encumbrance-none) :value basic-move :style (when (= 0 weight-class) highlight-style)}
{:label (i18n/label :t/move-encumbrance-light) :value light-move :style (when (= 1 weight-class) highlight-style)}
{:label (i18n/label :t/move-encumbrance-medium) :value medium-move :style (when (= 2 weight-class) highlight-style)}
{:label (i18n/label :t/move-encumbrance-heavy) :value heavy-move :style (when (= 3 weight-class) highlight-style)}
{:label (i18n/label :t/move-encumbrance-x-heavy) :value x-heavy-move :style (when (>= weight-class 4) highlight-style)}]))
(rf/reg-sub
:attributes/basic-move
:<- [:attributes/ht]
:<- [:attributes/dex]
(fn [[ht dx]]
(js/Math.floor (/ (+ ht dx) 4))))
(defn dodge-column []
(let [weight-class (some-> (rf/subscribe [:attributes/emcumbrance-weight-class]) deref)
basic-dodge (some-> (rf/subscribe [:attributes/basic-dodge]) deref)
light-dodge (- basic-dodge 1)
medium-dodge (- basic-dodge 2)
heavy-dodge (- basic-dodge 3)
x-heavy-dodge (- basic-dodge 4)]
[column (i18n/label :t/dodge)
{:label (i18n/label :t/dodge-encumbrance-none) :value basic-dodge :style (when (= 0 weight-class) highlight-style)}
{:label (i18n/label :t/dodge-encumbrance-light) :value light-dodge :style (when (= 1 weight-class) highlight-style)}
{:label (i18n/label :t/dodge-encumbrance-medium) :value medium-dodge :style (when (= 2 weight-class) highlight-style)}
{:label (i18n/label :t/dodge-encumbrance-heavy) :value heavy-dodge :style (when (= 3 weight-class) highlight-style)}
{:label (i18n/label :t/dodge-encumbrance-x-heavy) :value x-heavy-dodge :style (when (>= weight-class 4) highlight-style)}]))
(rf/reg-sub
:attributes/basic-dodge
:<- [:attributes/basic-move]
(fn [bm]
(+ 3 bm)))
(defn encumbrance-table []
[:> view {:style (tw "flex flex-row w-full px-2 py-1 gap-2 border-2 border-black justify-between")}
[encumbrance-column]
[move-column]
[dodge-column]])
(rf/reg-sub
:items/weight
:<- [:items/possessions]
:<- [:items/melee-weapons]
:<- [:items/ranged-weapons]
(fn [[possessions melee-weapons ranged-weapons]]
;; NOTE: melee weapons sometimes have a :var weight, so we need to convert it to an integer
(let [melee-weapons' (map (fn [item] (assoc item :weight (->int (:weight item)))) melee-weapons)]
(reduce (fn [acc item] (+ acc (:weight item)))
0
(concat possessions melee-weapons' ranged-weapons)))))