aboutsummaryrefslogtreecommitdiff
path: root/src/app/scoreboard.cljs
blob: b58e49eaf9dd1948759e12057e73d5d827754341 (plain)
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
(ns app.scoreboard
  (:require [hoplon.core :refer [table tbody td th thead tr]])
  (:require-macros [hoplon.core :refer [defelem loop-tpl]]
                   [javelin.core :refer [cell=]]))

(defelem scoreboard
  [{:keys [scoreboard] :as attr} kids]
  (let [scoreboard (cell= (merge (sorted-map) scoreboard))
        problems (cell= (mapcat (fn [[id person]]
                                  (map (partial vector id) (:problems person)))
                                scoreboard))
        scores (cell= (reverse (sort
         (map (fn [[id person]]
                [(apply
                   +
                   (concat
                     (map (fn [[prob state]]
                            (if (and
                                  (= :solved state)
                                  (not (contains? (:problems person) prob)))
                              1
                              0))
                          (:scores person))
                     (map (fn [prob]
                            (if (some (fn [[id2 person2]]
                                        (and
                                          (= :solved
                                             (get (:scores person) prob))
                                          (= :solved
                                             (get (:scores person2) prob))
                                          (not= id id2)))
                                      scoreboard)
                              1
                              0))
                          (:problems person))))
                 id])
              scoreboard))))]
    (table
      (thead
        (tr
          (th :colspan 2
              :style "border:none")
          (loop-tpl :bindings [probs (cell= (partition-by first problems))]
                    (th :text (cell= (:name (get scoreboard
                                                 (first (first probs)))))
                        :colspan (cell= (count probs)))))
        (tr
          (th "Player")
          (th "Score")
          (loop-tpl :bindings [[owner name] problems]
                    (th :text name))))
      (tbody
        (loop-tpl :bindings [[score id] scores]
                  (let [player (cell= (get scoreboard id))]
                    (tr
                      (td :text (cell= (:name player)))
                      (td :text score)
                      (loop-tpl :bindings [[owner _name] problems]
                                (td :text (cell=
                                            (name (get (:scores player)
                                                       _name
                                                       :unsolved))))))))))))

;; vim: set expandtab ts=2 sw=2 filetype=clojure :