Skip to content

Commit

Permalink
Introduce reg-id-sub that supports passing the sheet-id of interest
Browse files Browse the repository at this point in the history
Refs #69

This is not 100% bullet-proof, but it's relatively painless to use in
place of an existing subscription, and it allows those subscriptions to
*optionally* be used with an arbitrary sheet-id, for use on character
cards for a campaign page.
  • Loading branch information
dhleong committed Nov 17, 2018
1 parent 580a19f commit c1d3979
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 60 deletions.
9 changes: 6 additions & 3 deletions src/cljs/wish/sheets/dnd5e/campaign.cljs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
(ns ^{:author "Daniel Leong"
:doc "Campaign-viewer for D&D 5e"}
wish.sheets.dnd5e.campaign
(:require [wish.views.campaign.chars-carousel :refer [chars-carousel]]))
(:require [wish.views.campaign.chars-carousel :refer [chars-carousel]]
[wish.sheets.dnd5e.subs :as subs]
[wish.util :refer [<sub >evt]]))

(defn char-card [c]
(defn char-card [{:keys [id] :as c}]
[:div.character
(:name c)])
(:name c)
(str (<sub [::subs/hp id]))])

(defn view
[section]
Expand Down
70 changes: 37 additions & 33 deletions src/cljs/wish/sheets/dnd5e/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
mod->str]]
[wish.sheets.dnd5e.builder.data :refer [point-buy-max
score-point-cost]]
[wish.subs-util :refer [reg-id-sub query-vec->preferred-id]]
[wish.util :refer [<sub invoke-callable ->map]]
[wish.util.string :as wstr]))

Expand Down Expand Up @@ -91,7 +92,7 @@
"Convenience for creating a sub that just gets a specific
field from the :sheet key of the sheet-meta"
[id getter]
(reg-sub
(reg-id-sub
id
:<- [:meta/sheet]
(fn [sheet _]
Expand Down Expand Up @@ -143,7 +144,7 @@

; ======= class and level ==================================

(reg-sub
(reg-id-sub
::class->level
:<- [:classes]
(fn [classes _]
Expand All @@ -153,19 +154,19 @@
{}
classes)))

(reg-sub
(reg-id-sub
::class-level
:<- [::class->level]
(fn [classes [_ class-id]]
(get classes class-id)))

(reg-sub
(reg-id-sub
::abilities-raw
:<- [:meta/sheet]
(fn [sheet]
(:abilities sheet)))

(reg-sub
(reg-id-sub
::abilities-improvements
:<- [:classes]
:<- [:races]
Expand All @@ -175,7 +176,7 @@
(map (comp :buffs :attrs))
(apply merge-with +))))

(reg-sub
(reg-id-sub
::abilities-racial
:<- [:race]
(fn [race]
Expand All @@ -188,7 +189,7 @@
; TODO when we do handle equippable item buffs here, we need
; to make sure ::available-classes doesn't use it (only ability
; score improvements and racial bonuses ...)
(reg-sub
(reg-id-sub
::abilities-base
:<- [::abilities-raw]
:<- [::abilities-racial]
Expand All @@ -199,7 +200,7 @@
race
improvements)))

(reg-sub
(reg-id-sub
::abilities
:<- [::abilities-base]
:<- [:meta/sheet]
Expand All @@ -208,7 +209,7 @@
base
(:ability-tmp sheet))))

(reg-sub
(reg-id-sub
::ability-modifiers
:<- [::abilities]
(fn [abilities]
Expand All @@ -218,7 +219,7 @@
{}
abilities)))

(reg-sub
(reg-id-sub
::ability-saves
:<- [::ability-modifiers]
:<- [::proficiency-bonus]
Expand All @@ -238,7 +239,7 @@
{}
modifiers)))

(reg-sub
(reg-id-sub
::ability-info
:<- [::abilities]
:<- [::abilities-base]
Expand All @@ -262,7 +263,7 @@
{}
abilities)))

(reg-sub
(reg-id-sub
::skill-info
:<- [::ability-modifiers]
:<- [::skill-expertise]
Expand Down Expand Up @@ -294,7 +295,7 @@
{}
data/skill-id->ability)))

(reg-sub
(reg-id-sub
::limited-uses
:<- [:limited-uses]
:<- [:total-level]
Expand All @@ -318,7 +319,7 @@

(sort-by :name))))

(reg-sub
(reg-id-sub
::limited-use
:<- [::limited-uses]
:<- [:limited-used]
Expand Down Expand Up @@ -355,13 +356,17 @@
:max-slots (:total input)}
)))

(reg-sub
(reg-id-sub
::rolled-hp
:<- [:meta/sheet]
(fn [sheet [_ ?path]]
(get-in sheet (concat
[:hp-rolled]
?path))))

; NOTE: as an id-sub, we can also be called
; where the var at this position is the sheet id
(when (coll? ?path)
?path)))))

(reg-sheet-sub
::temp-hp
Expand All @@ -372,7 +377,7 @@
:temp-max-hp)

(def ^:private compile-hp-buff (memoize ->callable))
(reg-sub
(reg-id-sub
::max-hp-buffs
:<- [:race]
:<- [:classes]
Expand All @@ -391,7 +396,7 @@
entity)))
(apply +))))

(reg-sub
(reg-id-sub
::max-hp-mode
:<- [:meta/sheet]
(fn [sheet]
Expand All @@ -404,7 +409,7 @@
; default to :average for new users
:average)))

(reg-sub
(reg-id-sub
::max-hp-rolled
:<- [::rolled-hp]
:<- [::class->level]
Expand All @@ -431,7 +436,7 @@

(apply +))))

(reg-sub
(reg-id-sub
::max-hp-average
:<- [:classes]
(fn [classes]
Expand All @@ -456,24 +461,23 @@
0 ; start at 0
classes)))

; NOTE: we use reg-sub-raw here since it feels wrong to
(reg-sub
(reg-id-sub
::max-hp
(fn []
(fn [query-vec]
[; NOTE: this <sub is kinda gross but I *think* it's okay?
; subscriptions are de-dup'd so...?
; The only other way would be to always subscribe to both,
; and that seems worse
(case (<sub [::max-hp-mode])
:manual (subscribe [::max-hp-rolled])
:average (subscribe [::max-hp-average]))

(subscribe [::temp-max-hp])
(subscribe [::abilities])
(subscribe [:total-level])
(subscribe [::max-hp-buffs])
(case (<sub [::max-hp-mode (query-vec->preferred-id query-vec)])
:manual [::max-hp-rolled]
:average [::max-hp-average])

[::temp-max-hp]
[::abilities]
[:total-level]
[::max-hp-buffs]
])
(fn [[base-max temp-max abilities total-level buffs]]
(fn [[base-max temp-max abilities total-level buffs :as in]]
(+ base-max

temp-max
Expand All @@ -485,7 +489,7 @@

buffs)))

(reg-sub
(reg-id-sub
::hp
:<- [::temp-hp]
:<- [::max-hp]
Expand Down
Loading

0 comments on commit c1d3979

Please sign in to comment.