Skip to content

Instantly share code, notes, and snippets.

@bsteuber
Last active March 14, 2020 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsteuber/40ed1e12d53f921c6b89f9fefe4573e2 to your computer and use it in GitHub Desktop.
Save bsteuber/40ed1e12d53f921c6b89f9fefe4573e2 to your computer and use it in GitHub Desktop.
(ns tools.blitz-test
(:require [cljs.test :refer-macros [is testing]]
[devcards.core :refer-macros [defcard-rg deftest]]
[clojure.string :as str]))
(def group-a
{:players [["Lena" 1 "Trix"]
["Klaus" 1 "GoKlaus"]
["Christoph" 2 "Coreon"]
["Martin" 2 "LDD99"]
["Karsten" 2 "jena77"]
["Tobias" 4 "CrowsCrown"]]
:pairings [[[1 2] [6 3] [4 5]]
[[6 1] [3 4] [5 2]]
[[1 5] [2 3] [4 6]]
[[4 1] [3 5] [6 2]]
[[1 3] [2 4] [5 6]]]
:winners [["Lena" "Christoph" "Martin"]
["Lena" "Christoph" "Klaus"]
["Lena" "Klaus" "Martin"]
["Lena" "Karsten" "Klaus"]
["Lena" "Martin" "Karsten"]
["Klaus" "Christoph" "Martin"]
["Lena" "Martin" "Klaus"]
["Lena" "Klaus" "Martin"]
["Lena" "Karsten" "Klaus"]
["Christoph" "Martin" "Karsten"]
["Lena" "Christoph" "Martin"]
["Lena" "Martin" "Klaus"]
["Lena" "Christoph" "Martin"]
["Lena" "Christoph" "Klaus"]
["Lena" "Klaus" "Karsten"]
["Lena" "Christoph" "Martin"]
["Lena" "Martin" "Klaus"]
["Lena" "Christoph" "Martin"]]
:cycles 4
:current-round 20})
(def group-b
{:players [["Marco" 6 "TomaLexi"]
["Robert" 7 "drake164"]
["Markus" 7 "Saiyu"]
["Alexander" 17 "Merowin"]]
:pairings [[[1 2] [4 3]]
[[4 1] [2 3]]
[[1 3] [4 2]]]
:winners [["Robert" "Markus"]
["Marco" "Markus"]
["Marco" "Robert"]
["Marco" "Markus"]
["Marco" "jigo"]
["Marco" "Robert"]
["Marco" "Markus"]
["Marco" "Robert"]
["Marco" "Robert"]
["Robert" "Markus"]
["Marco" "Robert"]
["Marco" "Robert"]
["Marco" "Markus"]
["Marco" "Robert"]
["Marco" "Robert"]
["Robert" "Alexander"]
["Marco" "Markus"]]
:cycles 6
:current-round 18})
(defn id->player [{:keys [players]} id]
(get players (dec id)))
(defn name->player [{:keys [players]} name]
(some (fn [player]
(when (= name (first player))
player))
players))
(defn id->name [group id]
(first (id->player group id)))
(defn all-rounds [{:keys [cycles pairings current-round]}]
(take current-round
(map-indexed (fn [index round]
(assoc round :round-id index))
(mapcat (fn [cycle]
(for [pairing pairings]
{:even-cycle? (even? (inc cycle))
:pairing pairing}))
(range cycles)))))
(defn player-score [{:keys [winners]
:as group}
id
name]
(->> (all-rounds group)
(map (fn [{:keys [round-id pairing]}]
(apply +
(map-indexed (fn [pairing-id [b-id w-id]]
(if (#{b-id w-id} id)
(condp = (get-in winners [round-id pairing-id])
name 1
"jigo" 0.5
0)
0))
pairing))))
(apply +)))
(defn sum-vec [xs]
(if-let [xs (seq xs)]
(apply (partial map +) xs)
[0 0]))
(defn player-score-versus [{:keys [winners]
:as group}
id
opp-id]
(let [name (id->name group id)
opp-name (id->name group opp-id)]
(->> (all-rounds group)
(map (fn [{:keys [round-id pairing]}]
(->> pairing
(map-indexed (fn [pairing-id [b-id w-id]]
(if (every? #{b-id w-id} [id opp-id])
(do
(condp = (get-in winners [round-id pairing-id])
name [1 0]
"jigo" [0.5 0.5]
opp-name [0 1]
[0 0]))
[0 0])))
sum-vec)))
sum-vec)))
(defn td [& [opts & content]]
(let [[opts content] (if (map? opts)
[opts content]
[nil (cons opts content)])]
(into [:td (merge {:style {:padding "4px"}}
opts)]
content)))
(defn direct-comparision [group player-id-1 player-id-2]
[:p (str (player-score-versus group player-id-1 player-id-2))])
(defcard-rg direct-12
[direct-comparision group-a 1 2])
(defn generate-players [{:keys [players pairings winners cycles]
:as group}]
(let [data (sort-by :score >
(map-indexed
(fn [index [name rank kgs]]
{:id (inc index)
:name name
:kgs kgs
:rank rank
:score (player-score group (inc index) name)})
players))]
[:table
[:thead
[:tr
[:th "Name"]
[:th "KGS"]
[:th "Rang"]
[:th "Punkte"]]]
[:tbody
(for [{:keys [id name kgs rank score]} data]
^{:key id}
[:tr
[:td name]
[:td kgs]
[:td rank "k"]
[:td score]])]]))
(defn generate-rounds [{:keys [players pairings winners cycles]
:as group}]
[:table
[:tbody
(for [{:keys [round-id pairing even-cycle?]} (all-rounds group)]
^{:key [round-id]}
[:tr
(map-indexed
(fn [pairing-id [b-id w-id]]
(let [[b-name b-kyu b-kgs] (id->player group b-id)
[w-name w-kyu w-kgs] (id->player group w-id)
winner (get-in winners [round-id pairing-id])
[b-score w-score] (condp = winner
b-name [1 0]
"jigo" [0.5 0.5]
w-name [0 1]
nil [nil nil])
[b-name b-score w-name w-score]
(if even-cycle?
[w-name w-score b-name b-score]
[b-name b-score w-name w-score])]
^{:key pairing-id}
[:tr
(when (zero? pairing-id)
[td {:rowspan (count pairing)}
[:b "Runde " (inc round-id)]])
[td b-name]
[td " " b-score]
[td "-"]
[td w-name]
[td " " w-score]]))
pairing)])]])
(defn rounds-a []
[:div
[:h3 "Gruppe A"]
(generate-players group-a)
[:br]
(generate-rounds group-a)
[:br]
[:h3 "Gruppe B"]
(generate-players group-b)
[:br]
(generate-rounds group-b)])
(defcard-rg group-a
[rounds-a])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment