From f0dc882945dfeed180505892d94f09901685331e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Madeleine=20Sydney=20=C5=9Alaga?= Date: Fri, 7 Aug 2026 09:03:05 -0600 Subject: [PATCH] wiktlarp --- default.nix | 4 +-- flake.nix | 2 +- src/{wiktionary_tf2 => wiktlarp}/main.clj | 40 ++++++++++++++--------- 3 files changed, 28 insertions(+), 18 deletions(-) rename src/{wiktionary_tf2 => wiktlarp}/main.clj (80%) diff --git a/default.nix b/default.nix index 026eab7..20cb1ca 100644 --- a/default.nix +++ b/default.nix @@ -19,9 +19,9 @@ let # sqlite3 # ]; in mkCljBin' { - name = "wiktionary-tf2"; + name = "wiktlarp"; version = "0.1.0"; projectSrc = lib.cleanSource ./.; lockfile = ./deps-lock.json; - main-ns = "wiktionary-tf2.main"; + main-ns = "wiktlarp.main"; } diff --git a/flake.nix b/flake.nix index 6cf4211..f37d372 100644 --- a/flake.nix +++ b/flake.nix @@ -45,7 +45,7 @@ clojure babashka python3 - sqlite + (sqlite.override { interactive = true; }) python314Packages.wiktextract sqlite-web jq diff --git a/src/wiktionary_tf2/main.clj b/src/wiktlarp/main.clj similarity index 80% rename from src/wiktionary_tf2/main.clj rename to src/wiktlarp/main.clj index 610e433..f0e45ab 100644 --- a/src/wiktionary_tf2/main.clj +++ b/src/wiktlarp/main.clj @@ -1,4 +1,4 @@ -(ns wiktionary-tf2.main +(ns wiktlarp.main (:require [clojure.java.io :as io] [cheshire.core :as json] [babashka.fs :as fs] @@ -16,18 +16,21 @@ (def ^:dynamic *rcon*) -(def ^:dynamic *dictionary-file*) +(def ^:dynamic *db*) -(defn parse-plaintext-entry [s] - (when-some [[_ word gloss] (re-matches #"([^\t]*)\t(.*)\n?" s)] - [word gloss])) +(defmacro with-conn [[conn] & body] + `(let [ds# (sql/get-datasource + {:dbtype "sqlite" + :dbname "db/words.db"})] + (with-open [~conn (sql/get-connection ds#)] + (sql/execute! ~conn ["select load_extension (?)" + (System/getenv "SPELLFIX")]) + ~@body))) (defn query-word [word] - (let [ds (sql/get-datasource {:dbtype "sqlite" :dbname "db/words.db"})] - (with-open [conn (sql/get-connection ds)] - (->> (sql/execute! conn ["select info from words where word = ?" - word]) - (map #(json/parse-string (:words/info %) keyword)))))) + (->> (sql/execute! *db* ["select info from words where word = ?" + word]) + (map #(json/parse-string (:words/info %) keyword)))) (defn gloss-word [word] (->> word @@ -67,7 +70,7 @@ (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!!!\"") + @(rcon/exec c "echo \"wikilarper connected!\"") (l/info "connected to rcon!") c) (catch java.net.ConnectException e @@ -101,6 +104,8 @@ {:rcon/connection {:ip "127.0.0.1" :port 27015 :password "monitor"} + :database/connection {:dbtype "sqlite" + :dbname "db/words.db"} :console/watcher {:log-file (-> (str "~/.local/share/Steam/steamapps/common/Team Fortress 2/" @@ -109,8 +114,7 @@ fs/file) :handler (ig/ref :wikilinker/handler)} :wikilinker/handler {:rcon (ig/ref :rcon/connection) - :dictionary-file "words.gz"}}) - + :db (ig/ref :database/connection)}}) (defmethod ig/init-key :rcon/connection [_ {:keys [ip port password]}] (rcon-connect ip port password)) @@ -118,6 +122,12 @@ (defmethod ig/halt-key! :rcon/connection [_ conn] (.close conn)) +(defmethod ig/init-key :database/connection [_ dsinfo] + (sql/get-datasource dsinfo)) + +(defmethod ig/halt-key! :database/connection [_ conn] + (.close conn)) + (defmethod ig/init-key :console/watcher [_ {:keys [log-file handler]}] (l/infof "watching file %s" log-file) (hawk/watch! @@ -136,9 +146,9 @@ (hawk/stop! watcher)) (defmethod ig/init-key :wikilinker/handler - [_ {:keys [rcon dictionary-file]}] + [_ {:keys [rcon db]}] (binding [*rcon* rcon - *dictionary-file* dictionary-file] + *db* db] (bound-fn [log-file] (console-handler log-file))))