<commit_before>(ns darts.core
  (:require [ajax.core :refer [GET POST PUT] :as ajax-core]
            [reagent.core :as reagent :refer [atom]]
            [reagent.session :as session]
            [clojure.walk :as walk]
            [clojure.set :refer [difference]]
            [cognitect.transit :as t]
            [secretary.core :as secretary :include-macros true]
            [goog.events :as events]
            [goog.history.EventType :as EventType]
            [cljsjs.react :as react])
  (:import goog.History))

;; -------------------------
;; Views

(defn redirect-to [path]
  (set! js/window.location.href path))

(defonce players (atom nil))

(defn parse-json [json]
  (walk/keywordize-keys (t/read (t/reader :json) @players)))

(defn load-players []
  (GET "/list-players" {:handler (fn [res]
                                   (reset! players res))}))

(defn list-players []
  (load-players)
  [:ul
   (map (fn [player] ^{:key player} [:li (:name player)]) (parse-json @players))])

(def player1 (atom ""))
(def player2 (atom ""))
(def game-score (atom 310))

(defn both-players-selected? []
  (and (not= @player1 "")
       (not= @player2 "")))

(def game (atom {}))

(defn play-page []
  [:div
   (println @game)
   "HELLO"])

(defn select-player-pane []
  [:div
   (when (both-players-selected?)
     [:div
      [:button {:on-click (fn []
                            (reset! player1 "")
                            (reset! player2 ""))} "Reset"]])
   (let [select-fn (fn [player]
                     [:ul
                      (doall
                       (map (fn [{:keys [name]}]
                              (when (and (not= name @player1)
                                         (not= name @player2))
                                ^{:key name}
                                [:li
                                 [:button {:on-click (fn []
                                                       (reset! player name))}
                                  name]]))
                            (parse-json @players)))]
                     )]
     (if (= "" @player1)
       (select-fn player1)
       (select-fn player2)))
   [:a {:href "#/new-player"} "New player"]])

(defn select-game-pane []
  [:div
   [:p "Starting score: " @game-score]
   (map (fn [score] ^{:key score} [:button {:on-click #(reset! game-score score)} score]) [310 410 510])])

(defn new-game-page []
  (load-players)
  [:div
   [:a {:href "#"} "Home"]
   [:div [:h2 "New game"]
    [:ul
     [:li (str "Player 1: " @player1)]
     [:li (str "Player 2: " @player2)]]
    [select-player-pane]
    [select-game-pane]]
   (when (both-players-selected?)
     [:div
      [:button {:on-click (fn []
                            (reset! player1 "")
                            (reset! game-score 310)
                            (reset! player2 ""))} "Reset"]
      [:button {:on-click (fn []
                            (swap! game #(assoc %1
                                                :player1 %2
                                                :player2 %3
                                                :starting-score %4) @player1 @player2 @game-score)
                            (redirect-to "#/play"))} "Confirm"]])])

(defn new-player-page []
  (let [val (atom "")]
    (fn []
      [:div
       [:a {:href "#"} "Home"]
       [:h3 "New player"]
       [:input {:type :text
                :value @val
                :on-change (fn [e]
                             (reset! val (-> e
                                             .-target
                                             .-value)))<commit_msg>Basic game page<commit_after>(ns darts.core
  (:require [ajax.core :refer [GET POST PUT] :as ajax-core]
            [reagent.core :as reagent :refer [atom]]
            [reagent.session :as session]
            [clojure.walk :as walk]
            [clojure.set :refer [difference]]
            [cognitect.transit :as t]
            [secretary.core :as secretary :include-macros true]
            [goog.events :as events]
            [goog.history.EventType :as EventType]
            [cljsjs.react :as react])
  (:import goog.History))

;; -------------------------
;; Views

(defn redirect-to [path]
  (set! js/window.location.href path))

(defonce players (atom nil))

(defn parse-json [json]
  (walk/keywordize-keys (t/read (t/reader :json) @players)))

(defn load-players []
  (GET "/list-players" {:handler (fn [res]
                                   (reset! players res))}))

(defn list-players []
  (load-players)
  [:ul
   (map (fn [player] ^{:key player} [:li (:name player)]) (parse-json @players))])

(def player1 (atom ""))
(def player2 (atom ""))
(def game-score (atom 310))

(defn both-players-selected? []
  (and (not= @player1 "")
       (not= @player2 "")))

(def example-game-2 (atom {0 {:name "Davide"
                              :rounds []}
                           1 {:name "Steven"
                              :rounds []}
                           :starting-score 310}))

(def example-game (atom {0 {:name "Davide"
                            :rounds [34 50 23 14 0]}
                         1 {:name "Steven"
                            :rounds [23 68 7 13]}
                         :starting-score 310}))

(defn remaining-points [{:keys [rounds]} starting-score]
  (- starting-score (reduce + 0 rounds)))

(def game example-game)

(def score (atom ""))
(def next-player (atom 0))

(defn numpad []
  [:div
   (map (fn [n] ^{:key n}[:button {:on-click #(swap! score str n)}
                          n]) (range 10))
   [:button {:on-click #(reset! score "")} "Clear"]
   [:button {:on-click (fn []
                         (swap! game #(update-in %1 [%2 :rounds] conj (js/parseInt %3))
                                @next-player
                                @score)
                         (reset! score "")
                         (swap! next-player #(mod (inc %) 2)))} "Enter"]])

(defn play-page []
  (let [
        ]
    [:div
     [:div
      [:span "Player 1: " (remaining-points (get @game 0) (:starting-score @game))]
      [:br]
      [:span "Player 2: " (remaining-points (get @game 1) (:starting-score @game))]]
     [:div
      [:br ]
      (str "Player " (+ 1 @next-player) " turn")
      [:br]
      [:input {:type :text
               :disabled true
               :value @score}]
      [:br]
      [numpad]
      (println @game)]]))


(defn select-player-pane []
  [:div
   (when (both-players-selected?)
     [:div
      [:button {:on-click (fn []
                            (reset! player1 "")
                            (reset! player2 ""))} "Reset"]])
   (let [select-fn (fn [player]
                     [:ul
                      (doall
                       (map (fn [{:keys [name]}]
                              (when (and (not= name @player1)
                                         (not= name @player2))
                                ^{:key name}
                                [:li
                                 [:button {:on-click (fn []
                                                       (reset! player name))}
                                  name]]))
                            (parse-json @players)))]
                     )]
     (if (= "" @player1)
       (select-fn player1)
       (select-fn player2)))
   [:a {:href "#/new-player"} "New player"]])

(defn select-game-pane []
  [:div
   [:p "Starting score: " @game-score]
   (map (fn [score] ^{:key score} [:button {:on-click #(reset! game-score score)} score]) [310 410 510])])

(defn new-game-page []
  (load-players)
  [:div
   [:a {:href "#"} "Home"]
   [:div [:h2 "New game"]
    [:ul
     [:li (str "Player 1: " @player1)]
     [:li (str "Player 2: " @player2)]]
    [select-player-pane]
    [select-game-pane]]
   (when (both-players-selected?)
     [:div
      [:button {:on-click (fn []
                            (reset! player1 "")
                            (reset! game-score 310)
                            (reset! player2 ""))} "Reset"]
      [:button {:on-click (fn []
                            (swap! game #(assoc %1
                                                0 {:name %2
                                                   :rounds []}
                                                1 {:name %3
                                                   :rounds []}
                                                :starting-score %4) @player1 @player2 @game-score)
                            (redirect-to "#/play"))} "Confirm"]])])

(defn new-player-page []
  (let [val (atom "")]
    (fn []
      [:div
       [:a {:href "#"} "Home"]
       [:h3 "New player"]
       [:input {:type :text
                :value @val
                :on-change (fn [e]
                             (reset! val (-> e
                                             .-target
                                             .-value)))
{'fraction_non_alphanumeric': 0.16372030176781893, 'fraction_numerical': 0.013962391622565027, 'mean_word_length': 1.232780291603821, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '629221', 'n_tokens_mistral': 2771, 'n_tokens_neox': 2545, 'n_words': 678}