~bzg/choices

661bbdf9fdc9f04b9d01f0c9c69e0fca8e44c064 — Bastien 2 months ago 77d9d47
Allow to display scores in the final message as percentage

Score variables can now have a "max" value used to express the final
score value as a percentage.
2 files changed, 19 insertions(+), 8 deletions(-)

M config.yml
M src/cljs/choices/core.cljs
M config.yml => config.yml +9 -5
@@ 39,7 39,7 @@ display-summary: true
display-score: true

# Display each score?
display-score-details: false
display-score-details: true

# Only the score with the highest value?
display-score-top-result: true


@@ 67,6 67,7 @@ score-variables:
  score-2:
    display: "Score for 2"
    value: 0
    max: 10
    as-top-result-display: "2 wins :("

# Set conditional output depending on scores.


@@ 81,19 82,22 @@ conditional-score-outputs:
  cas-1: # free-form
    notification: "is-success"
    priority: 1
    node: "end"
    # Final score message supports using formatting strings for
    # scores.  Strings starting with "%" and matching a score name
    # will be interpolated: e.g. here the message will display the
    # value of "score-1".
    message: "1 wins! :)  Here is the score 1: %score-1"
    condition-1: #free-form
      # cas-1 is the case when score-& >= 1
      score-1: 1
  cas-1: # free-form
  cas-2: # free-form
    notification: "is-danger"
    message: "2 wins! :("
    message: "2 wins! :(  Here is the score 2: %score-2"
    # When max is set as below, %score-2 above will be formatted as a
    # percentage: if score 2 is 10 and max is 10, the percent is 100.
    condition-1: #free-form
      score-2: 1
      # cas-2 is the case when score-2 >= 9
      score-2: 9

# Mandatory configuration option
tree:

M src/cljs/choices/core.cljs => src/cljs/choices/core.cljs +10 -3
@@ 29,6 29,7 @@
(def show-summary (:display-summary config))
(def conditional-score-outputs (:conditional-score-outputs config))
(def sticky-help (reagent/atom ""))
(def score-variables (:score-variables config))

;; home-page and start-page
(def home-page


@@ 42,7 43,7 @@
  (-> s (md/md->hiccup) (md/component)))

;; History-handling variables
(def history (reagent/atom [{:score (:score-variables config)}]))
(def history (reagent/atom [{:score score-variables}]))
(def hist-to-redo (reagent/atom {}))
(def hist-to-add (reagent/atom {}))



@@ 216,7 217,13 @@
     :node         @node}))

(defn format-score-output-string [output scores]
  (let [scores (map (fn [[k v]] [(str "%" (name k)) v]) scores)]
  (let [scores
        (map (fn [[k v]]
               [(str "%" (name k))
                (if-let [max (:max (get score-variables k))]
                  (fmt/format "%.0f" (/ (* v 100) max))
                  v)])
             scores)]
    (reduce-kv string/replace output (into {} scores))))

(defn scores-result [scores]


@@ 419,7 426,7 @@
    (cond
      ;; Reset history?
      (= target-page start-page)
      (do (reset! history [{:score (:score-variables config)}])
      (do (reset! history [{:score score-variables}])
          (reset! hist-to-redo {})
          (reset! hist-to-add {}))
      ;; History backward?