diff --git a/deps-lock.json b/deps-lock.json index e656227..a014731 100644 --- a/deps-lock.json +++ b/deps-lock.json @@ -1,6 +1,14 @@ { "lock-version": 4, - "git-deps": [], + "git-deps": [ + { + "lib": "io.github.anteoas/hawkeye", + "url": "https://github.com/anteoas/hawkeye.git", + "rev": "5b3f5a99e9edfe482174258a864b577f7a4eeb47", + "git-dir": "https/github.com/anteoas/hawkeye", + "hash": "sha256-ghKXjf4e3U8rlWCpffPRHqvf367RBs+VnSpRF6tI0xk=" + } + ], "mvn-deps": [ { "mvn-path": "aleph/aleph/0.4.1/aleph-0.4.1.jar", @@ -52,6 +60,31 @@ "mvn-repo": "https://repo.clojars.org/", "hash": "sha256-LVp6g6ymqHa1ZOMHYD0CRaNxZ4osT0xoEH7dC5sLv7s=" }, + { + "mvn-path": "ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar", + "mvn-repo": "https://repo1.maven.org/maven2/", + "hash": "sha256-mMPxj10NZCzV8yfMckVmzRlklibH2I9wFDvXBMlBV9U=" + }, + { + "mvn-path": "ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.pom", + "mvn-repo": "https://repo1.maven.org/maven2/", + "hash": "sha256-Ond3RjIf1uIuOMzf1PY0vKNysA7mIAPlJ/aqRkkF8+g=" + }, + { + "mvn-path": "ch/qos/logback/logback-core/1.1.3/logback-core-1.1.3.jar", + "mvn-repo": "https://repo1.maven.org/maven2/", + "hash": "sha256-R8D9NCmV0zFbj6zKzDJLKnYUOyfEMNSy1qKeq8MfXBQ=" + }, + { + "mvn-path": "ch/qos/logback/logback-core/1.1.3/logback-core-1.1.3.pom", + "mvn-repo": "https://repo1.maven.org/maven2/", + "hash": "sha256-ziBX5rWqOWLWyNtAxJ6LzhFQqaobNhXtS6b1I9O5pwA=" + }, + { + "mvn-path": "ch/qos/logback/logback-parent/1.1.3/logback-parent-1.1.3.pom", + "mvn-repo": "https://repo1.maven.org/maven2/", + "hash": "sha256-ag4n7GeP9dkLjMZku0UZifXitbMAamW95Lld7BmHOHE=" + }, { "mvn-path": "cheshire/cheshire/6.1.0/cheshire-6.1.0.jar", "mvn-repo": "https://repo.clojars.org/", @@ -147,6 +180,16 @@ "mvn-repo": "https://repo.clojars.org/", "hash": "sha256-dXkdZHooK97QEyeXodFSC942EiB9B8MlHUNZlbH8nvI=" }, + { + "mvn-path": "hawk/hawk/0.2.11/hawk-0.2.11.jar", + "mvn-repo": "https://repo.clojars.org/", + "hash": "sha256-6UPy8MSHxsWmsg5wzpHdXzHkBIlXCRe7oT/OpzyaekM=" + }, + { + "mvn-path": "hawk/hawk/0.2.11/hawk-0.2.11.pom", + "mvn-repo": "https://repo.clojars.org/", + "hash": "sha256-q4PzoWHUY53W2TZWihPpw+qXB4QWWVnS1iW3WlvIxFg=" + }, { "mvn-path": "io/aleph/dirigiste/0.1.3/dirigiste-0.1.3.jar", "mvn-repo": "https://repo1.maven.org/maven2/", @@ -182,6 +225,26 @@ "mvn-repo": "https://repo.clojars.org/", "hash": "sha256-4DHE4IZS3RIfRjqvxEB3M0wsptJJJaP4VFtY+4yKoro=" }, + { + "mvn-path": "net/incongru/watchservice/barbary-watchservice/1.0/barbary-watchservice-1.0.jar", + "mvn-repo": "https://repo1.maven.org/maven2/", + "hash": "sha256-VMbKvYIJPIUV9uFOQ9lL/n6fx5XRV4nzlDoUGNgmOrU=" + }, + { + "mvn-path": "net/incongru/watchservice/barbary-watchservice/1.0/barbary-watchservice-1.0.pom", + "mvn-repo": "https://repo1.maven.org/maven2/", + "hash": "sha256-k7OxHaltUXiIDfjFBT8Yz8eByv8Nnd9LPGRyRKnRws8=" + }, + { + "mvn-path": "net/java/dev/jna/jna/5.14.0/jna-5.14.0.jar", + "mvn-repo": "https://repo1.maven.org/maven2/", + "hash": "sha256-NO0eHyf6iWvKUNvE6ZzzcylnzsOHp6DV40hsCWc/6MY=" + }, + { + "mvn-path": "net/java/dev/jna/jna/5.14.0/jna-5.14.0.pom", + "mvn-repo": "https://repo1.maven.org/maven2/", + "hash": "sha256-4E4llRUB3yWtx7Hc22xTNzyUiXuE0+FJISknY+4Hrj0=" + }, { "mvn-path": "org/clojure/clojure/1.10.3/clojure-1.10.3.jar", "mvn-repo": "https://repo1.maven.org/maven2/", @@ -407,6 +470,21 @@ "mvn-repo": "https://repo1.maven.org/maven2/", "hash": "sha256-16CKmbJQLwu2jNTh+YTwv2kySqogi9D3M2bAP8NUikI=" }, + { + "mvn-path": "org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar", + "mvn-repo": "https://repo1.maven.org/maven2/", + "hash": "sha256-aZgMA4yhsTGSZWFZFhfZwl+r/Hspgor5FZfKhXDPNf4=" + }, + { + "mvn-path": "org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.pom", + "mvn-repo": "https://repo1.maven.org/maven2/", + "hash": "sha256-NTkEp6bCgwTQ3KyA+tMMSM2Jj22wO5PwWtbJDdQtmK0=" + }, + { + "mvn-path": "org/slf4j/slf4j-parent/1.7.7/slf4j-parent-1.7.7.pom", + "mvn-repo": "https://repo1.maven.org/maven2/", + "hash": "sha256-Hf+uPOdo0FR+JhyWiYz12dGUv/1WAPWXyXUcxqc9M9Q=" + }, { "mvn-path": "org/sonatype/oss/oss-parent/7/oss-parent-7.pom", "mvn-repo": "https://repo1.maven.org/maven2/", diff --git a/deps.edn b/deps.edn index bf3dd45..1218c50 100644 --- a/deps.edn +++ b/deps.edn @@ -6,5 +6,9 @@ cheshire/cheshire {:mvn/version "6.1.0"} progrock/progrock {:mvn/version "1.0.0"} babashka/process {:mvn/version "0.6.25"} - clj-rcon/clj-rcon {:mvn/version "0.1.1"}} + clj-rcon/clj-rcon {:mvn/version "0.1.1"} + hawk/hawk {:mvn/version "0.2.11"} + ch.qos.logback/logback-classic #:mvn{:version "1.1.3"} + io.github.anteoas/hawkeye + {:git/sha "5b3f5a99e9edfe482174258a864b577f7a4eeb47"}} :paths ["src" "resources"]} diff --git a/src/wiktionary_tf2/main.clj b/src/wiktionary_tf2/main.clj index 9b092a0..3d5411e 100644 --- a/src/wiktionary_tf2/main.clj +++ b/src/wiktionary_tf2/main.clj @@ -4,7 +4,14 @@ [babashka.process :as p] [clj-rcon.core :as rcon] [clojure.tools.logging :as l] - [clojure.string :as str])) + [clojure.string :as str] + [hawkeye.core :as hawk]) + (: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") @@ -23,7 +30,8 @@ first)) (defn parse-chat-message [x] - (when-let [[_ author body] (re-matches #"(.*?) : (.*)" x)] + (when-let [[_ _dead? author body] + (re-matches #"(\*사망\* )?(.*?) : (.*)" x)] {:author author :body body})) (defn say-word [word] @@ -32,20 +40,70 @@ (take 128) (apply str) (format "say \"%s\"") - (rcon/exec @*rcon*))) + (rcon/exec *rcon*))) -(defn try-bracketed-words [s] - (when-let [word (some-> (re-find #"\[\[([^]]+)]]" s) second)] - (l/infof "bracked word: %s" word) - (Thread/sleep 500) - (say-word word))) +(defn find-word [s] + (some-> (or (re-find #"\[\[([^]]+)]]" s) + (re-find #"define\s+(\w+)" s)) + second)) + +(defn do-definition [s] + (when-some [w (find-word s)] + (l/infof "looking up word: %s" w) + (say-word w))) + +(defn rcon-connect [host port password] + (l/info "attempting rcon connection...") + (or (try (let [c @(rcon/connect host port password)] + @(rcon/exec c "echo \"WIKTIONARY CONNECTED!!!\"") + (l/info "connected to rcon!") + c) + (catch java.net.ConnectException e + (l/info (ex-message e)) + nil)) + (do (Thread/sleep 5000) + (recur host port password)))) + +(defn handle-console-event [s] + (l/infof "! %s" s) + (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))) + +(defn handler [] + (let [prev @prev-size + content (slurp log-file) + size (count content)] + (try (cond (< prev size) (-> content + (subs prev) + str/trim-newline + handle-console-event) + (< size prev) (l/warn "log file shrunk?") + :else nil) + (finally (reset! prev-size size))))) + +(defn start-watcher! [] + (hawk/watch (str (fs/parent log-file)) + (bound-fn* (fn [ev] + (when (= (str (fs/file-name log-file)) + (:file ev)) + (handler)))) + (fn [e ctx] + (l/error e "error in watcher")))) + +(defn start-logger! [] + @(rcon/exec *rcon* (format "con_logfile %s" + (fs/file-name log-file))) + @(rcon/exec *rcon* "echo \"wiktionary logging now\"") + (Thread/sleep 400) + (l/info "logfile has been set up!")) (defn -main [] - (binding [*rcon* (rcon/connect "127.0.0.1" 27015 "monitor")] - (l/info "ready!") - (loop [x (read-line)] - (l/infof "! %s" x) - (when-let [{:keys [author body]} (parse-chat-message x)] - (try-bracketed-words body)) - (flush) - (recur (read-line))))) + (binding [*rcon* (rcon-connect "127.0.0.1" 27015 "monitor")] + (start-logger!) + (start-watcher!) + (read-line)))