<commit_before>(ns chessdojo.views.board
  (:require
    [reagent.core :as reagent]
    [chessdojo.state :as cst]
    [chessdojo.fen :as cf]
    [chessdojo.rules :as cr]
    [clojure.zip :as zip]))

(enable-console-print!)

(defn move-destinations [game]
  (let [position (:position (zip/node game))
        valid-moves (cr/valid-moves position)
        from-to (map #(select-keys % [:from :to]) valid-moves)
        grouped-by-from (group-by :from from-to)
        ]
    (reduce-kv #(assoc %1 (cr/to-sqr %2) (map cr/to-sqr (map :to %3))) {} grouped-by-from)

    )
  )

(defn create-chessground-options []
  (let [buffer @cst/main-buffer
        game (:game buffer)]
    {:fen         (cf/fen game)
     :orientation "white"
     :movable     {
                   :free       false
                   :dests      (move-destinations game)
                   :show-dests true
                   }
     }
    )
  )

(defn render-chessground []
  [:div#chessground.chessground.merida])

(def chessground-board
  (reagent/atom nil))

(defn chessground-mounted [this]
  (reset! chessground-board
    (js/Chessground
      (reagent/dom-node this)
      (clj->js (create-chessground-options)))))

(defn chessground []
  (reagent/create-class
    {:render              render-chessground
     :component-did-mount chessground-mounted}))<commit_msg>handle chessground move callback to insert move<commit_after>(ns chessdojo.views.board
  (:require
    [reagent.core :as reagent]
    [chessdojo.state :as cst]
    [chessdojo.fen :as cf]
    [chessdojo.rules :as cr]
    [clojure.zip :as zip]
    [chessdojo.game :as cg]))

(enable-console-print!)

(defn move-destinations [game]
  (let [position (:position (zip/node game))
        valid-moves (cr/valid-moves position)
        from-to (map #(select-keys % [:from :to]) valid-moves)
        grouped-by-from (group-by :from from-to)]
    (reduce-kv #(assoc %1 (cr/to-sqr %2) (map cr/to-sqr (map :to %3))) {} grouped-by-from)))

(defn insert-move [from to meta]
  (do
    (println (str "insert move: " from " " to " " (js->clj meta)))
    (let [buffer @cst/main-buffer
          game (:game buffer)
          move-coords {:from (cr/to-idx (keyword from)) :to (cr/to-idx (keyword to))}
          new-game (cg/insert-move game move-coords)]
      (swap! cst/main-buffer assoc :game new-game))))

;(defn ^:export set-comment [comment]
;  (reset! current-game (cg/set-comment @current-game comment)))

(defn create-chessground-options []
  (let [buffer @cst/main-buffer
        game (:game buffer)]
    {:fen         (cf/fen game)
     :orientation "white"
     :movable     {
                   :free       false
                   :dests      (move-destinations game)
                   :show-dests true
                   :events     {
                                :after insert-move
                                }
                   }
     }
    )
  )

(defn render-chessground []
  [:div#chessground.chessground.merida])

(def chessground-board
  (reagent/atom nil))

(defn chessground-mounted [this]
  (reset! chessground-board
    (js/Chessground
      (reagent/dom-node this)
      (clj->js (create-chessground-options)))))

(defn chessground []
  (reagent/create-class
    {:render              render-chessground
     :component-did-mount chessground-mounted}))
{'fraction_non_alphanumeric': 0.14255511929930534, 'fraction_numerical': 0.001812141347025068, 'mean_word_length': 2.169377990430622, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '6122254', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1130, 'n_words': 250}