<commit_before>(ns penjat.core
  (:require-macros [reagent.ratom :refer [reaction]])  
  (:require [reagent.core :as reagent :refer [atom]]
            [re-frame.core :refer [register-handler
                                   path
                                   register-sub
                                   dispatch
                                   dispatch-sync
                                   subscribe]]
            [clojure.string :refer [join
                                    blank?]]))

;; trigger a dispatch every second
(defonce time-updater (js/setInterval
                        #(dispatch [:timer (js/Date.)]) 1000))

(def initial-state
  {:attempts 0
   :word "portatil"
   :key ""
   :guessed-letters #{}
   :failed-letters #{}})



;; Helper functions

(defn contains-char? [s c]
  (some #(= c %) s))

;; -- Event Handlers ----------------------------------------------------------


(register-handler                 ;; setup initial state
  :initialize                     ;; usage:  (submit [:initialize])
  (fn 
    [db _]
    (merge db initial-state)))    ;; what it returns becomes the new state


(register-handler
  :time-color                     ;; usage:  (submit [:time-color 34562])
  (path [:time-color])            ;; this is middleware
  (fn
    [time-color [_ value]]        ;; path middleware adjusts the first parameter
    value))


(register-handler
  :timer
  (fn
    ;; the first item in the second argument is :timer the second is the 
    ;; new value
    [db [_ value]]
    (assoc db :timer value)))    ;; return the new version of db


(register-handler
  :key       
  (fn
    [db [_ key]]
    (let [word (:word db)]
        (if (not (blank? key))
          (let [letter (.charAt key 0)
                db' (assoc db :key letter)]
            (.log js/console  (str "log:" letter word db))
            (if  (contains-char? word letter)
              (update-in db' [:guessed-letters] #(conj % letter))
              (update-in db' [:failed-letters] #(conj % letter))))
          (assoc db :key "")))))


(register-handler
  :inc-attempts
  (path [:attempts])
  (fn
    [attempts [_]]
    (.log js/console (str "inc-attempts handler:"  attempts))
    (inc attempts))) ;; return the new version of db

;; -- Subscription Handlers ---------------------------------------------------


(register-sub
  :timer
  (fn 
    [db _]                       ;; db is the app-db atom
    (reaction (:timer @db))))    ;; wrap the compitation in a reaction


(register-sub
  :time-color
  (fn 
    [db _]
    (reaction (:time-color @db))))


(register-sub
 :word
 (fn
   [db _]
   (reaction (:word @db))))


(register-sub
 :key
 (fn
   [db _]
   (reaction (:key @db))))


(register-sub
 :attempts
 (fn
   [db _]
   (reaction (:attempts @db))))


(register-sub
 :guessed-letters
 (fn
   [db _]
   (reaction (:guessed-letters @db))))


;; -- View Components ---------------------------------------------------------<commit_msg>calculated attempts based on failed letters<commit_after>(ns penjat.core
  (:require-macros [reagent.ratom :refer [reaction]])  
  (:require [reagent.core :as reagent :refer [atom]]
            [re-frame.core :refer [register-handler
                                   path
                                   register-sub
                                   dispatch
                                   dispatch-sync
                                   subscribe]]
            [clojure.string :refer [join
                                    blank?]]))

;; trigger a dispatch every second
(defonce time-updater (js/setInterval
                        #(dispatch [:timer (js/Date.)]) 1000))


(def initial-state
  {:word "portatil"
   :key ""
   :guessed-letters #{}
   :failed-letters #{}})



;; Helper functions

(defn contains-char? [s c]
  (some #(= c %) s))

;; -- Event Handlers ----------------------------------------------------------


(register-handler                 ;; setup initial state
  :initialize                     ;; usage:  (submit [:initialize])
  (fn 
    [db _]
    (merge db initial-state)))    ;; what it returns becomes the new state


(register-handler
  :time-color                     ;; usage:  (submit [:time-color 34562])
  (path [:time-color])            ;; this is middleware
  (fn
    [time-color [_ value]]        ;; path middleware adjusts the first parameter
    value))


(register-handler
  :timer
  (fn
    ;; the first item in the second argument is :timer the second is the 
    ;; new value
    [db [_ value]]
    (assoc db :timer value)))    ;; return the new version of db





(register-handler
  :key       
  (fn
    [db [_ key]]
    (let [word (:word db)]
        (if (not (blank? key))
          (let [letter (.charAt key 0)
                db' (assoc db :key letter)]
            (.log js/console  (str "log:" letter word db))
            (if  (contains-char? word letter)
              (update-in db' [:guessed-letters] #(conj % letter))
              (update-in db' [:failed-letters] #(conj % letter))))
          (assoc db :key "")))))



;; -- Subscription Handlers ---------------------------------------------------


(register-sub
  :timer
  (fn 
    [db _]                       ;; db is the app-db atom
    (reaction (:timer @db))))    ;; wrap the compitation in a reaction


(register-sub
  :time-color
  (fn 
    [db _]
    (reaction (:time-color @db))))


(register-sub
 :word
 (fn
   [db _]
   (reaction (:word @db))))


(register-sub
 :key
 (fn
   [db _]
   (reaction (:key @db))))


(register-sub
 :attempts
 (fn
   [db _]
   (let [failed-letters (reaction (:failed-letters @db))]
     (reaction (count @failed-letters)))))


(register-sub
 :guessed-letters
 (fn
   [db _]
   (reaction (:guessed-letters @db))))


;; -- View Components ---------------------------------------------------------
{'fraction_non_alphanumeric': 0.18613827414650883, 'fraction_numerical': 0.003602676273803397, 'mean_word_length': 1.5064488392089423, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3918301', 'n_tokens_mistral': 1820, 'n_tokens_neox': 1652, 'n_words': 490}