<commit_before>(ns chessdojo.core
  (:require
    [clojure.zip :as zip :refer [up down left lefts right rights rightmost insert-right branch? node]]
    [reagent.core :as reagent :refer [atom]]
    [reagent.session :as session]
    [secretary.core :as secretary :include-macros true]
    [accountant.core :as accountant]
    [chessdojo.game :as cg]
    [chessdojo.fen :as cf]
    [chessdojo.rules :as cr]
    [chessdojo.data :as cd]))

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

; samples

;(defn simple-component []
;  [:div
;   [:p "I am a component!"]
;   [:p.someclass
;    "I have " [:strong "bold"]
;    [:span {:style {:color "red"}} " and red "] "text."]])
;
;(defn lister [items]
;  [:ul
;   (for [item items]
;     ^{:key item} [:li "Item " item])])
;
;(def click-count (reagent/atom 0))
;
;(defn counting-component []
;  [:div
;   "The atom " [:code "click-count"] " has value: "
;   @click-count ". "
;   [:input {:type "button" :value "Click me!"
;            :on-click #(swap! click-count inc)}]])

(enable-console-print!)

(defn get-data
  []
  (let [deflated-game (cljs.reader/read-string (.getAttribute (.getElementById js/document "game-data") "dgn"))]
    (cd/load-game deflated-game)))

(def state
  (reagent/atom
    (get-data)))

(defn update-board [path]
  (let [game @state new-game (cg/jump game path) new-fen (cf/position->fen (:position (node new-game)))]
    (reset! state new-game)
    (js/updateBoard new-fen)))

(defn ^:export insert-move [move]
  (let [move-info (js->clj move)
        move-coords {:from (cr/to-idx (keyword (get move-info "from"))) :to (cr/to-idx (keyword (get move-info "to"))) :piece (keyword (clojure.string/upper-case (get move-info "piece")))}
        new-game (cg/insert-move @state move-coords)
        new-fen (cf/position->fen (:position (node new-game)))]
    (reset! state new-game)
    (js/updateBoard new-fen)))

(defn move-no [ply]
  (when (odd? ply) (str (inc (quot ply 2)) ".")))

(defn move-view [move path focus]
  [:span {:className (str "move" (when focus " focus")) :on-click #(update-board path)}
   (str (move-no (first path)) (cg/move->long-str move))])

(defn comment-view [comment]
  [:span comment])

(defn variation-view [nodes current-path depth]
  [:div (when (> depth 0) {:className "variation"})
   (for [node nodes]
     (if (vector? node)
       ^{:key (:path (meta node))} [variation-view node current-path (inc depth)]
       (let [move (:move node) path (:path (meta node)) comment (:comment node)]
         ^{:key path} [:span
                       [move-view move path (= current-path path)]
                       [comment-view comment]
                       ]
         )
       )
     )
   ]
  )

(defn game-view []
  (let [game @state current-path (cg/game-path game)]
    [:div {:className "game-view"}
     [variation-view (rest (zip/root game)) current-path 0] ; skip the start-node
     ])
  )

(defn buttons []
  [:div
   [:input {:type "button" :value "Down" :on-click #(reset! state (down @state))}]
   [:input {:type "button" :value "Right" :on-click #(reset! state (right @state))}]]
  )

(defn home-page []
  [:div
   [game-view]
   [buttons]
   [:div [:a {:href "/about"} "go to about page"]]<commit_msg>preliminary variation numbering<commit_after>(ns chessdojo.core
  (:require
    [clojure.zip :as zip :refer [up down left lefts right rights rightmost insert-right branch? node]]
    [reagent.core :as reagent :refer [atom]]
    [reagent.session :as session]
    [secretary.core :as secretary :include-macros true]
    [accountant.core :as accountant]
    [chessdojo.game :as cg]
    [chessdojo.fen :as cf]
    [chessdojo.rules :as cr]
    [chessdojo.data :as cd]))

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

; samples

;(defn simple-component []
;  [:div
;   [:p "I am a component!"]
;   [:p.someclass
;    "I have " [:strong "bold"]
;    [:span {:style {:color "red"}} " and red "] "text."]])
;
;(defn lister [items]
;  [:ul
;   (for [item items]
;     ^{:key item} [:li "Item " item])])
;
;(def click-count (reagent/atom 0))
;
;(defn counting-component []
;  [:div
;   "The atom " [:code "click-count"] " has value: "
;   @click-count ". "
;   [:input {:type "button" :value "Click me!"
;            :on-click #(swap! click-count inc)}]])

(enable-console-print!)

(defn get-data
  []
  (let [deflated-game (cljs.reader/read-string (.getAttribute (.getElementById js/document "game-data") "dgn"))]
    (cd/load-game deflated-game)))

(def state
  (reagent/atom
    (get-data)))

(defn update-board [path]
  (let [game @state new-game (cg/jump game path) new-fen (cf/position->fen (:position (node new-game)))]
    (reset! state new-game)
    (js/updateBoard new-fen)))

(defn ^:export insert-move [move]
  (let [move-info (js->clj move)
        move-coords {:from (cr/to-idx (keyword (get move-info "from"))) :to (cr/to-idx (keyword (get move-info "to"))) :piece (keyword (clojure.string/upper-case (get move-info "piece")))}
        new-game (cg/insert-move @state move-coords)
        new-fen (cf/position->fen (:position (node new-game)))]
    (reset! state new-game)
    (js/updateBoard new-fen)))

(defn move-no [ply]
  (when (odd? ply) (str (inc (quot ply 2)) ".")))

(defn move-view [move path focus]
  [:span {:className (str "move" (when focus " focus")) :on-click #(update-board path)}
   (str (move-no (first path)) (cg/move->long-str move))])

(defn comment-view [comment]
  [:span comment])

(defn variation-view [nodes current-path depth]
  [:div (when (> depth 0) {:className "variation"})
   (when (> depth 0) [:span (str (first (:path (meta nodes))) "] ")])
   (for [node nodes]
     (if (vector? node)
       ^{:key (:path (meta node))} [variation-view node current-path (inc depth)]
       (let [move (:move node) path (:path (meta node)) comment (:comment node)]
         ^{:key path} [:span
                       [move-view move path (= current-path path)]
                       [comment-view comment]
                       ]
         )
       )
     )
   ]
  )

(defn game-view []
  (let [game @state current-path (cg/game-path game)]
    [:div {:className "game-view"}
     [variation-view (rest (zip/root game)) current-path 0]])) ; skip the start-node

(defn buttons []
  [:div
   [:input {:type "button" :value "Down" :on-click #(reset! state (down @state))}]
   [:input {:type "button" :value "Right" :on-click #(reset! state (right @state))}]]
  )

(defn home-page []
  [:div
   [game-view]
   [buttons]
   [:div [:a {:href "/about"} "go to about page"]]
{'fraction_non_alphanumeric': 0.19540761288334105, 'fraction_numerical': 0.0013869625520110957, 'mean_word_length': 2.926194797338173, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5981318', 'n_tokens_mistral': 2393, 'n_tokens_neox': 2292, 'n_words': 652}