diff --git a/src/wiktionary_tf2/main.clj b/src/wiktionary_tf2/main.clj index fbf902d..95c982c 100644 --- a/src/wiktionary_tf2/main.clj +++ b/src/wiktionary_tf2/main.clj @@ -9,29 +9,23 @@ [hawk.core :as hawk] [next.jdbc :as sql] [integrant.core :as ig]) - (:import (java.util.zip GZIPInputStream)) (:gen-class)) -(def log-file - (-> "~/.local/share/Steam/steamapps/common/Team Fortress 2/tf/console.log" - fs/expand-home - fs/file)) - -(def words-file "words.gz") - -(def words-db-file "words.db") +(def chat-message-regexp #"(\*사망\* )?(.*?) : (.*)") (def ^:dynamic *rcon*) +(def ^:dynamic *dictionary-file*) + (defn parse-plaintext-entry [s] (when-some [[_ word gloss] (re-matches #"([^\t]*)\t(.*)\n?" s)] [word gloss])) -(defn lookup-word-plaintext [word] +(defn lookup-word [word] (let [r (p/shell {:out :string :continue true} "zgrep" "-m1" (format "^%s\t[^[:space:]]" word) - words-file)] + *dictionary-file*)] (when (zero? (:exit r)) (->> r :out @@ -40,14 +34,9 @@ (filter #(not (empty? %))) ; remove nil and "" first)))) -(defn lookup-word-sql [word]) - -(defn lookup-word [word] - (lookup-word-plaintext word)) - (defn parse-chat-message [x] (when-let [[_ _dead? author body] - (re-matches #"(\*사망\* )?(.*?) : (.*)" x)] + (re-matches chat-message-regexp x)] {:author author :body body})) (defn say [s] @@ -90,13 +79,10 @@ (when-let [{:keys [author body]} (parse-chat-message s)] (do-definition body))) -(defonce prev-size - (atom (if (fs/exists? log-file) - (-> log-file slurp count) - 0))) +(def ^:dynamic *prev-size*) -(defn handler [] - (let [prev @prev-size +(defn console-handler [log-file] + (let [prev @*prev-size* content (slurp log-file) size (count content)] (try (cond (< prev size) (-> content @@ -105,18 +91,7 @@ handle-console-event) (< size prev) (l/warn "log file shrunk?") :else nil) - (finally (reset! prev-size size))))) - -(defn start-watcher! [] - (l/infof "watching file %s" log-file) - (hawk/watch! [{:paths [(str log-file)] - :handler - (bound-fn* - (fn [_ctx ev] - (try (handler) - (catch Exception e - (l/errorf e "exception in handler") - (throw e)))))}])) + (finally (reset! *prev-size* size))))) @@ -142,20 +117,27 @@ (.close conn)) (defmethod ig/init-key :console/watcher [_ {:keys [log-file handler]}] - (hawk/watch! [{:paths [(str log-file)] - :handler - (fn [_ctx _ev] - (try (handler) - (catch Exception e - (l/errorf e "exception in handler") - (throw e))))}])) + (l/infof "watching file %s" log-file) + (hawk/watch! + [{:paths [(str log-file)] + :handler + (binding [*prev-size* (atom (if (fs/exists? log-file) + (-> log-file slurp count) + 0))] + (bound-fn [_ctx ev] + (try (handler log-file) + (catch Exception e + (l/errorf e "exception in console handler") + (throw e)))))}])) (defmethod ig/halt-key! :console/watcher [_ watcher] (hawk/stop! watcher)) -(defmethod ig/init-key :wikilinker/handler [_ {:keys [rcon watcher]}] - (binding [*rcon* rcon] - (bound-fn [] (handler)))) +(defmethod ig/init-key :wikilinker/handler + [_ {:keys [rcon dictionary-file]}] + (binding [*rcon* rcon + *dictionary-file* dictionary-file] + (bound-fn [log-file] (console-handler log-file))))