<commit_before>(ns isla.story
  (:use [clojure.pprint])
  (:require [clojure.string :as str])
  (:require [isla.parser :as parser])
  (:require [isla.interpreter :as interpreter])
  (:require [isla.story-utils :as story-utils])
  (:require [mrc.utils :as utils])
  (:require [isla.library :as library]))

(declare types name-into-objs extract-by-class get-story-ctx seq-to-hash resolve-)

(defprotocol Queryable
  (get-all-items [this])
  (get-item [this name]))

(defprotocol Playable
  ;; (move [this arguments])
  (look [this arguments]))

(defrecord Story [player rooms]
  Queryable
  (get-all-items [this]
    (concat (map (fn [x] (:items x)) rooms)))
  (get-item [this name]
    (if-let [item (first (filter (fn [y] (= name (:name y))) (get-all-items this)))]
      item ;; item getter untested because didn't have items when wrote it
      (if (> (.indexOf ["myself" "me"] name) -1)
        player
        nil)))

  Playable
  ;; (move [this arguments]
  ;;   (println "move!"))
  (look [this arguments]
    (if (nil? arguments)
      (:summary (get player :room))
      (let [arguments-vec (str/split arguments #" ")]
        (if (= "at" (first arguments-vec))
          (if-let [item (get-item this (second arguments-vec))]
            (:summary item))
          nil)))))


(defrecord Monster [name summary])
(def monster-defaults ["" ""])

(defrecord Player [name summary room])
(def player-defaults ["" "" :undefined])

(defrecord Room [name summary items exit])
(def room-defaults ["" "" [] :undefined])

(defn init-story [story-str]
  (let [env (interpreter/interpret
             (parser/parse story-str)
             (library/get-initial-env types (get-story-ctx)))
        ctx (interpreter/resolve- (:ctx env) env)

        rooms (seq-to-hash (name-into-objs
                            (extract-by-class ctx (:type (get types "room")))) :name)
        player (val (first (extract-by-class ctx (:type (get types "_player")))))]
    (Story. player rooms)))

(defn run-command [story command-str]
  (let [command (first (str/split command-str #" "))
        arguments-str (second (str/split command-str #" " 2))
        arguments-vec (if (nil? arguments-str) [nil] [arguments-str])]
    (utils/run-method story command arguments-vec)))


(defn get-story-ctx []
  {"my" (story-utils/instantiate-type (get types "_player"))})

;; keys+vals -> vals
(defn name-into-objs [objs]
  (map (fn [x] (assoc (val x) :name (key x)))
       objs))

(defn extract-by-class [ctx clazz]
  (filter
   (fn [x] (= clazz (class (val x))))
   ctx))

(defn seq-to-hash [seq- key-]
  (reduce (fn [hash el] (assoc hash (key- el) el)) {} seq-))
(defn tuples-to-hash [seq-]
  (reduce (fn [hash el] (assoc hash (get el 0) (get el 1)))
          {}
          seq-))<commit_msg>Replace code to tuples-to-hash call.<commit_after>(ns isla.story
  (:use [clojure.pprint])
  (:require [clojure.string :as str])
  (:require [isla.parser :as parser])
  (:require [isla.interpreter :as interpreter])
  (:require [isla.story-utils :as story-utils])
  (:require [mrc.utils :as utils])
  (:require [isla.library :as library]))

(declare types name-into-objs extract-by-class get-story-ctx seq-to-hash resolve-)

(defprotocol Queryable
  (get-all-items [this])
  (get-item [this name]))

(defprotocol Playable
  ;; (move [this arguments])
  (look [this arguments]))

(defrecord Story [player rooms]
  Queryable
  (get-all-items [this]
    (concat (map (fn [x] (:items x)) rooms)))
  (get-item [this name]
    (if-let [item (first (filter (fn [y] (= name (:name y))) (get-all-items this)))]
      item ;; item getter untested because didn't have items when wrote it
      (if (> (.indexOf ["myself" "me"] name) -1)
        player
        nil)))

  Playable
  ;; (move [this arguments]
  ;;   (println "move!"))
  (look [this arguments]
    (if (nil? arguments)
      (:summary (get player :room))
      (let [arguments-vec (str/split arguments #" ")]
        (if (= "at" (first arguments-vec))
          (if-let [item (get-item this (second arguments-vec))]
            (:summary item))
          nil)))))


(defrecord Monster [name summary])
(def monster-defaults ["" ""])

(defrecord Player [name summary room])
(def player-defaults ["" "" :undefined])

(defrecord Room [name summary items exit])
(def room-defaults ["" "" [] :undefined])

(defn init-story [story-str]
  (let [env (interpreter/interpret
             (parser/parse story-str)
             (library/get-initial-env types (get-story-ctx)))
        ctx (interpreter/resolve- (:ctx env) env)

        rooms (seq-to-hash (name-into-objs
                            (extract-by-class ctx (:type (get types "room")))) :name)
        player (val (first (extract-by-class ctx (:type (get types "_player")))))]
    (Story. player rooms)))

(defn run-command [story command-str]
  (let [command (first (str/split command-str #" "))
        arguments-str (second (str/split command-str #" " 2))
        arguments-vec (if (nil? arguments-str) [nil] [arguments-str])]
    (utils/run-method story command arguments-vec)))


(defn get-story-ctx []
  {"my" (story-utils/instantiate-type (get types "_player"))})

(defn name-into-objs [objs]
  (tuples-to-hash (map (fn [{k 0 v 1}]
                         [k (if (and (contains? v :name) (str/blank? (:name v)))
                              (assoc v :name k)
                              v)])
                       objs)))

(defn extract-by-class [ctx clazz]
  (tuples-to-hash (filter
                   (fn [x] (= clazz (class (val x))))
                   ctx)))

(defn tuples-to-hash [seq-]
  (reduce (fn [hash el] (assoc hash (get el 0) (get el 1)))
          {}
          seq-))
{'fraction_non_alphanumeric': 0.172322375397667, 'fraction_numerical': 0.0017674089784376105, 'mean_word_length': 2.730388925510877, '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': '1157625', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1870, 'n_words': 558}