From 02ba68e498427fc43c34c48c06876a3971a3ec88 Mon Sep 17 00:00:00 2001 From: Ian Jones Date: Wed, 8 Nov 2023 21:29:23 -0700 Subject: [PATCH] :knife: reps-per-round (#83) --- resources/workouts.edn | 2 -- src/com/spicy/repl.clj | 3 +-- src/com/spicy/results/core.clj | 9 +++----- src/com/spicy/results/score.clj | 34 ++++++++++++---------------- src/com/spicy/results/score_test.clj | 22 ++++++++---------- src/com/spicy/results/ui.clj | 1 + src/com/spicy/schema.clj | 3 ++- src/com/spicy/workouts/core.clj | 17 -------------- src/com/spicy/workouts/ui.clj | 31 +++++++++++-------------- 9 files changed, 45 insertions(+), 77 deletions(-) diff --git a/resources/workouts.edn b/resources/workouts.edn index 9403a24..1fb556b 100644 --- a/resources/workouts.edn +++ b/resources/workouts.edn @@ -39,7 +39,6 @@ :workout/created-at :db/now :workout/description "As many rounds as possible in 20 minutes: 5 pull-ups 10 push-ups 15 squats" :workout/scheme :rounds-reps - :workout/reps-per-round 30 :db/doc-type :workout} {:xt/id :db.id/diane :workout/name "Diane" @@ -94,7 +93,6 @@ :workout/created-at :db/now :workout/description "As many rounds as possible in 20 minutes: 5 handstand push-ups 10 one-legged squats 15 pull-ups" :workout/scheme :rounds-reps - :workout/reps-per-round 30 :db/doc-type :workout} {:xt/id :db.id/nancy :workout/name "Nancy" diff --git a/src/com/spicy/repl.clj b/src/com/spicy/repl.clj index 4480d93..69a7376 100644 --- a/src/com/spicy/repl.clj +++ b/src/com/spicy/repl.clj @@ -159,8 +159,7 @@ (biff/submit-tx (get-context) [{:xt/id cindy :db/doc-type :workout - :db/op :update - :workout/reps-per-round 30}]) + :db/op :update}]) (let [{:keys [biff/db]} (get-context)] diff --git a/src/com/spicy/results/core.clj b/src/com/spicy/results/core.clj index 6342481..cf6e529 100644 --- a/src/com/spicy/results/core.clj +++ b/src/com/spicy/results/core.clj @@ -41,8 +41,7 @@ conj (->scores (merge params - {:reps-per-round (:workout/reps-per-round workout) - :scheme (:workout/scheme workout) + {:scheme (:workout/scheme workout) :rounds-to-score (:workout/rounds-to-score workout)})))] (biff/submit-tx ctx (concat @@ -239,8 +238,7 @@ [{:keys [biff/db session params] :as ctx}] (let [workout-id (parse-uuid (:workout params)) {:workout/keys - [reps-per-round - scheme + [scheme rounds-to-score] :as _workout} (xt/entity db workout-id) result-tx [{:db/op :create :db/doc-type :wod-result @@ -258,8 +256,7 @@ conj (->scores (merge params - {:reps-per-round reps-per-round - :scheme scheme + {:scheme scheme :rounds-to-score rounds-to-score})))] (biff/submit-tx ctx (concat result-tx wod-sets-tx))) diff --git a/src/com/spicy/results/score.clj b/src/com/spicy/results/score.clj index 63d2cfa..dc5974a 100644 --- a/src/com/spicy/results/score.clj +++ b/src/com/spicy/results/score.clj @@ -16,10 +16,12 @@ (or (n/safe-parse-int reps) 0)) +(def ^:const REPS_MULTIPLIER 10000) + + (defn rounds-reps->score - [{:keys [rounds reps-per-round] :as params}] - (+ (reps->score params) - (* reps-per-round (n/parse-int rounds)))) + [{:keys [rounds] :as params}] + (+ (/ (float (reps->score params)) REPS_MULTIPLIER) (n/parse-int rounds))) (defn params->score @@ -27,6 +29,7 @@ (cond (or (some? minutes) (some? seconds)) (time->score params) (and (some? rounds) (some? reps)) (rounds-reps->score params) + (some? rounds) (n/parse-int rounds) (some? reps) (n/parse-int reps) :else nil)) @@ -63,16 +66,13 @@ (defn ->scores - [{:keys [rounds-to-score reps-per-round scheme] + [{:keys [rounds-to-score] :as params}] (->> (range 0 (or rounds-to-score 1)) (mapcat #(->score (assoc params :n %))) (group-by :index) vals - (map #(apply merge %)) - (mapv #(if (= scheme :rounds-reps) - (assoc % :reps-per-round reps-per-round) - %)))) + (map #(apply merge %)))) (defn scores->tx @@ -95,12 +95,14 @@ ;; => 45 (params->score {:minutes "3" :seconds "45"}) ;; => 225 - (params->score {:rounds "20" :rounds-multiplier 30}) - ;; => nil + (params->score {:rounds "20"}) + ;; => 20 + + (params->score {:rounds "20" :reps "5"}) + ;; => 20.005 - (params->score {:rounds "20" :reps "5" :rounds-multiplier 30}) - ;; => 605 (params->score {:reps "42"}) + ;; => 42 (def p {:date "2023-07-14", @@ -124,7 +126,6 @@ (def p-2 {:rounds-to-score 3 - :reps-per-round 100 :date "2023-07-14", :notes-1 "fell off my pace", :scale "rx", @@ -178,9 +179,4 @@ "idW9AN+yI4rPRh40QkJ+Mz0duX5CxJforNpmO/UzS7khGITLpRVx0/ePsk7PD5bUM46u2V5NwQnyYjWO", :notes-0 ""}) - (->scores p-3) - ;; => ({:minutes "16", :index "0", :seconds "33", :notes ""} - ;; {:minutes "102", :index "1", :seconds "2", :notes "fell off my pace"} - ;; {:minutes "96", :index "2", :seconds "1", :notes "couldnt think anymore"}) - ;; - (transduce (scores->tx {:op :create :parent :db.id/wod-result}) conj (->scores x))) + (->scores p-3)) diff --git a/src/com/spicy/results/score_test.clj b/src/com/spicy/results/score_test.clj index d663709..adc4f46 100644 --- a/src/com/spicy/results/score_test.clj +++ b/src/com/spicy/results/score_test.clj @@ -21,7 +21,6 @@ (def params-with-rounds-reps {:rounds-to-score 3 - :reps-per-round 100 :date "2023-07-14" :notes-1 "fell off my pace" :scale "rx" @@ -58,31 +57,30 @@ (deftest ->scores (testing "takes params with reps and outputs formated score" - (is (= [{:reps "116" :index 0 :notes ""} - {:reps "102" :index 1 :notes "fell off my pace"} - {:reps "96" :index 2 :notes "couldnt think anymore"}] + (is (= [{:reps "116" :index 0 :notes "" :id nil} + {:reps "102" :index 1 :notes "fell off my pace" :id nil} + {:reps "96" :index 2 :notes "couldnt think anymore" :id nil}] (sut/->scores params-with-reps)))) (testing "defaults rounds to score to 1" - (is (= [{:reps "116" :index 0 :notes ""}] + (is (= [{:reps "116" :index 0 :notes "" :id nil}] (sut/->scores (dissoc params-with-reps :rounds-to-score))))) (testing "takes params with rounds and reps" - (is (= [{:rounds "3" :index 0 :reps "116" :notes "" :reps-per-round 100} + (is (= [{:rounds "3" :index 0 :reps "116" :notes "" :id nil} {:rounds "2" :index 1 :reps "102" :notes "fell off my pace" - :reps-per-round 100} + :id nil} {:rounds "1" :index 2 :reps "96" - :notes "couldnt think anymore" - :reps-per-round 100}] + :notes "couldnt think anymore" :id nil}] (sut/->scores params-with-rounds-reps)))) (testing "takes params with minutes seconds" - (is (= [{:minutes "16" :index 0 :seconds "33" :notes ""} - {:minutes "102" :index 1 :seconds "2" :notes "fell off my pace"} + (is (= [{:minutes "16" :index 0 :seconds "33" :notes "" :id nil} + {:minutes "102" :index 1 :seconds "2" :notes "fell off my pace" :id nil} {:minutes "96" :index 2 :seconds "1" - :notes "couldnt think anymore"}] + :notes "couldnt think anymore" :id nil}] (sut/->scores params-with-minutes-seconds))))) diff --git a/src/com/spicy/results/ui.clj b/src/com/spicy/results/ui.clj index c78da51..cb19c27 100644 --- a/src/com/spicy/results/ui.clj +++ b/src/com/spicy/results/ui.clj @@ -124,6 +124,7 @@ :placeholder "Reps" :value reps :min 0 + :max 9999 :required true}]]) [:input.w-full.pink-input.teal-focus {:type "number" :name (str "reps-" identifier) diff --git a/src/com/spicy/schema.clj b/src/com/spicy/schema.clj index 0397911..cf55ba1 100644 --- a/src/com/spicy/schema.clj +++ b/src/com/spicy/schema.clj @@ -38,7 +38,7 @@ (def WodSet [:map [:xt/id :wod-set/id] - [:result-set/score :int] + [:result-set/score float?] [:result-set/number :int] [:result-set/parent :wod-result/id]]) @@ -99,6 +99,7 @@ [:workout/reps-per-round {:optional true} :int] [:workout/rounds-to-score {:optional true} :int] [:workout/user {:optional true} :user/id] + [:workout/sugar-id {:optional true} :string] [:workout/tiebreak-scheme {:optional true} [:enum :time :reps]] [:workout/secondary-scheme {:optional true} (into [] (filter #(not (= :time-with-cap %)) workout-types))]] :strength-set/id :uuid diff --git a/src/com/spicy/workouts/core.clj b/src/com/spicy/workouts/core.clj index 29a112e..e98ce39 100644 --- a/src/com/spicy/workouts/core.clj +++ b/src/com/spicy/workouts/core.clj @@ -171,10 +171,6 @@ :workout/name (:name params) :workout/scheme (keyword (:scheme params)) :workout/description (:description params)} - (when - (:reps-per-round params) - {:workout/reps-per-round - (n/safe-parse-int (:reps-per-round params))}) (when (:rounds params) {:workout/rounds-to-score @@ -214,10 +210,6 @@ :workout/scheme (keyword (:scheme params)) :workout/created-at :db/now :workout/description (:description params)} - (when - (:reps-per-round params) - {:workout/reps-per-round - (n/safe-parse-int (:reps-per-round params))}) (when (:rounds params) {:workout/rounds-to-score @@ -337,14 +329,6 @@ :hx-target "#selected-movements"} name]) results)])) -(defn get-scheme-inputs - [{:keys [params] :as _ctx}] - (case (:scheme params) - "rounds-reps" [:div#scheme-inputs [:input.w-full.pink-input.p-2.teal-focus#reps-per-round - {:placeholder "Reps per round" :name "reps-per-round" :required true}]] - [:div.hidden#scheme-inputs])) - - (defn get-score-separately [{:keys [params] :as _ctx}] (if (= "on" (:score-separately params)) @@ -371,7 +355,6 @@ ["/new/selected" {:get show-selected-movement :post set-selected-movement :delete remove-selected-movement}] - ["/new/scheme-inputs" {:get get-scheme-inputs}] ["/new/score-separately" {:get get-score-separately}]]) diff --git a/src/com/spicy/workouts/ui.clj b/src/com/spicy/workouts/ui.clj index 0e37633..36a1e4e 100644 --- a/src/com/spicy/workouts/ui.clj +++ b/src/com/spicy/workouts/ui.clj @@ -1,17 +1,22 @@ (ns com.spicy.workouts.ui (:require - [com.biffweb :as biff :refer [q]] + [com.biffweb :as biff] + [com.spicy.results.score :refer [REPS_MULTIPLIER]] [com.spicy.time :as t] [com.spicy.ui :as ui])) (defn ->rounds-reps - [reps-per-round reps] - (if (> 0 reps) - "0+0" - (str (quot reps reps-per-round) - "+" - (rem reps reps-per-round)))) + [reps-per-round score] + (if (int? score) + (if (> 0 score) + "0+0" + (str (quot score reps-per-round) + "+" + (rem score reps-per-round))) + (let [rounds (.intValue score) + reps (.intValue (* REPS_MULTIPLIER (- (bigdec score) rounds)))] + (str rounds "+" reps)))) (comment @@ -37,7 +42,7 @@ (defn display-summed-score - [{:keys [workout sets] :as a}] + [{:keys [workout sets] :as _a}] (when (and (not-empty workout) (not-empty sets)) (display-score (merge {:workout workout} {:result-set (merge-set-score-with sets +)})))) @@ -124,9 +129,6 @@ [:select.pink-input.teal-focus#scheme {:name "scheme" :required true - :hx-get "/app/workouts/new/scheme-inputs" - :hx-target "#scheme-inputs" - :hx-swap "outerHTML" :value (when (not (nil? workout)) (name (:workout/scheme workout)))} [:option {:value "" :label "--Select a Workout Scheme--"}] [:option {:value "time" @@ -147,13 +149,6 @@ :label "feet"}] [:option {:value "points" :label "points"}]]] - (if (= :rounds-reps (:workout/scheme workout)) - [:div#scheme-inputs - [:div.flex.flex-col.w-full - [:label {:for :reps-per-round} "Reps per round"] - [:input.w-full.pink-input.p-2.teal-focus#reps-per-round - {:placeholder "Reps per round" :name "reps-per-round" :required true}]]] - [:div.hidden#scheme-inputs]) [:div.flex.gap-3.items-center [:label "Score rounds separately?"] [:input#score-separately