aboutsummaryrefslogtreecommitdiff
path: root/src/index.cljs.hl
blob: 82980ccca66db2fb0a774518e4b5f7b99e2f5045 (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
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
(page "index.html"
  (:require [app.rpc :as rpc]))

(defc= scoreboard (merge (sorted-map) rpc/scoreboard))
(defc= problems (mapcat (fn [[id person]]
                          (map (partial vector id) (:problems person)))
                        scoreboard))
(defc= scores
       (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))))

(defc token nil)
(defc= logged-in? rpc/token-ok)
(defc= error rpc/error)
(defc= error-message (when error (.-message error)))

(rpc/init)

(html
  (head
    (link :rel "stylesheet" :type "text/css" :href "css/main.css")
    (title "Potluck CTF"))
  (body
    (h1 "Potluck CTF")
    (div
      :id "error"
      :click #(reset! rpc/error nil)
      :toggle (cell= (not (nil? rpc/error)))
      (text "Error: ~{error-message}"))
    (let [token-input (input :name "token")]
      (form
        :toggle (cell= (not logged-in?))
        :submit #(do (reset! token (.-value token-input))
                   (rpc/check-token @token)
                   (set! (.-value token-input) nil))
        (text "Registration Token:")
        token-input
        (input :type "submit")))
    (let [name-input (input :name "name")]
      (form
        :toggle logged-in?
        :submit #(do (rpc/set-name! @token (.-value name-input))
                   (set! (.-value name-input) nil))
        (text "Change Name:")
        name-input
        (input :type "submit")))
    (let [flag-input (input :name "flag")]
      (form
        :toggle logged-in?
        :submit #(do (rpc/submit-flag! @token (.-value flag-input))
                   (set! (.-value flag-input) nil))
        (text "Flag:")
        flag-input
        (input :type "submit")))
    (form
      :toggle logged-in?
      :submit #(do (reset! token nil)
                 (reset! rpc/token-ok false))
      (input :type "submit" :value "Logout"))
    (h2 "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 :