<commit_before>(ns i18n-word-guess.run
  (:require [i18n-word-guess [game :as impl]
                             [db :as db]]
            [clojure.java.io :as io]))

;;-----------------------------------------------------------------------------
;; Dictionary

(defn- load-words [file-name]
  (with-open [rdr (io/reader file-name)]
    (into [] (line-seq rdr))))

(def questionable-nouns (filter #(<= 5 (count %) 10)
                   (load-words (io/resource "nouns.txt"))))

(def all-nouns (load-words (io/resource "nouns-full.txt")))

;(count questionable-nouns)
;(count all-nouns)

;;-----------------------------------------------------------------------------
;; Game routine

(defn- game-message [{:keys [status guess]}]
  (case status
    :start "Начинайте"
    :ok "Продолжайте"
    :win "Вы победили"
    :repeat (str "Вариант \"" guess "\" уже был")
    :no-match (str "Вариант \"" guess "\" не подходит")
;    :over "Игра уже закончена"
    :not-in-dict  (str "Слова \"" guess "\" нет в словаре")
    ""))

(defn- format-step [{:keys [word mask] :as step}]
  (merge (select-keys step [:game_id
                            :guess
                            :mask
                            :status
                            :creation_date])
         {:code (impl/encode mask word)
          :message (game-message step)}))

(defn- get-game-cur [id]
  (->> (db/get-game-cur id)
       format-step))

(defn get-game-history [id]
  (->> (db/get-game-history id)
       (map format-step)))

(defn new-game! [& [{:keys [session] :as ctx}]]
  (let [word               (rand-nth questionable-nouns)
        [game-rec]         (db/insert-game! {:word word})
        game-id            (:id game-rec)
        [initial-step-rec] (db/insert-step! {:game_id game-id
                                             :session session
                                             :mask    (impl/initial-mask word)
                                             :status  :start})]
    (get-game-cur game-id)))

(defn guess-game! [game-id new-guess
                   & [{:keys [session] :as ctx}]]
  (let [lower-guess (clojure.string/lower-case new-guess)
        prev-guesses (map :guess (db/get-game-history game-id))
        {:keys [word mask status]} (db/get-game-cur game-id)]
    (when-not (= (keyword status) :win)
      (db/insert-step!
       (merge {:game_id   game-id
               :session   session
               :guess     new-guess}
              (cond
               (some #{lower-guess} prev-guesses)  {:status :repeat
                                                    :mask   mask}
               (not-any? #{lower-guess} all-nouns) {:status :not-in-dict
                                                    :mask   mask}
               :else (impl/next-step word mask new-guess)))))
    (get-game-cur game-id)))

;(get-game-cur 26)
;(new-game! {:session "65453"})
;(guess-game! 29 "сифен")
;(get-game-history 29)

(defn get-hints [code]
  (impl/code-hints all-nouns code))

;; (distinct (map impl/word->phon questionable-nouns))<commit_msg>Fix case sensitivity bug<commit_after>(ns i18n-word-guess.run
  (:require [i18n-word-guess [game :as impl]
                             [db :as db]]
            [clojure.java.io :as io]))

;;-----------------------------------------------------------------------------
;; Dictionary

(defn- load-words [file-name]
  (with-open [rdr (io/reader file-name)]
    (into [] (line-seq rdr))))

(def questionable-nouns (filter #(<= 5 (count %) 10)
                   (load-words (io/resource "nouns.txt"))))

(def all-nouns (load-words (io/resource "nouns-full.txt")))

;(count questionable-nouns)
;(count all-nouns)

;;-----------------------------------------------------------------------------
;; Game routine

(defn- game-message [{:keys [status guess]}]
  (case status
    :start "Начинайте"
    :ok "Продолжайте"
    :win "Вы победили"
    :repeat (str "Вариант \"" guess "\" уже был")
    :no-match (str "Вариант \"" guess "\" не подходит")
;    :over "Игра уже закончена"
    :not-in-dict  (str "Слова \"" guess "\" нет в словаре")
    ""))

(defn- format-step [{:keys [word mask] :as step}]
  (merge (select-keys step [:game_id
                            :guess
                            :mask
                            :status
                            :creation_date])
         {:code (impl/encode mask word)
          :message (game-message step)}))

(defn- get-game-cur [id]
  (->> (db/get-game-cur id)
       format-step))

(defn get-game-history [id]
  (->> (db/get-game-history id)
       (map format-step)))

(defn new-game! [& [{:keys [session] :as ctx}]]
  (let [word               (rand-nth questionable-nouns)
        [game-rec]         (db/insert-game! {:word word})
        game-id            (:id game-rec)
        [initial-step-rec] (db/insert-step! {:game_id game-id
                                             :session session
                                             :mask    (impl/initial-mask word)
                                             :status  :start})]
    (get-game-cur game-id)))

(defn guess-game! [game-id new-guess
                   & [{:keys [session] :as ctx}]]
  (let [lower-guess (clojure.string/lower-case new-guess)
        prev-guesses (map :guess (db/get-game-history game-id))
        {:keys [word mask status]} (db/get-game-cur game-id)]
    (when-not (= (keyword status) :win)
      (db/insert-step!
       (merge {:game_id   game-id
               :session   session
               :guess     new-guess}
              (cond
               (some #{lower-guess} prev-guesses)  {:status :repeat
                                                    :mask   mask}
               (not-any? #{lower-guess} all-nouns) {:status :not-in-dict
                                                    :mask   mask}
               :else (impl/next-step word mask lower-guess)))))
    (get-game-cur game-id)))

;(get-game-cur 26)
;(new-game! {:session "65453"})
;(guess-game! 29 "сифен")
;(get-game-history 29)

(defn get-hints [code]
  (impl/code-hints all-nouns code))

;; (distinct (map impl/word->phon questionable-nouns))
{'fraction_non_alphanumeric': 0.20048979591836735, 'fraction_numerical': 0.005877551020408163, 'mean_word_length': 1.4367541766109786, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2984101', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1969, 'n_words': 448}