Skip to content

Commit

Permalink
Merge branch 'clue-db'
Browse files Browse the repository at this point in the history
  • Loading branch information
tkindy committed Jul 22, 2024
2 parents 19e3003 + d17611d commit bf7d08d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
config.edn
target/
jeopardy.db
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
org.clojure/tools.cli {:mvn/version "1.0.214"}
org.jsoup/jsoup {:mvn/version "1.15.3"}
org.postgresql/postgresql {:mvn/version "42.5.1"}
org.xerial/sqlite-jdbc {:mvn/version "3.46.0.0"}
org.slf4j/slf4j-reload4j {:mvn/version "2.0.6"}
ring/ring-defaults {:mvn/version "0.3.4"}}

Expand Down
46 changes: 46 additions & 0 deletions src/com/tylerkindy/jeopardy/clues.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(ns com.tylerkindy.jeopardy.clues
(:require [clojure.string :as str]
[com.tylerkindy.jeopardy.answer :refer [char-pairs normalize-answer]]
[next.jdbc :refer [execute! get-datasource]]
[next.jdbc.result-set :refer [as-unqualified-kebab-maps]])
(:import [java.time LocalDate]))

(def ds (get-datasource {:dbtype "sqlite"
:dbname "jeopardy.db"}))

(defn valid-answer? [answer]
(seq (char-pairs answer)))

(defn valid-clue? [{:keys [question value answer]}]
(let [question (str/lower-case question)]
(and value
(not (str/includes? question "seen here"))
(not (str/includes? question "shown here"))
(not (str/includes? question "heard here"))
(valid-answer? (normalize-answer answer)))))

(defn clean-clue [clue]
(-> clue
(update :airdate #(LocalDate/parse %))))

(def random-clues-q
(str/join "\n" ["select"
" ca.name as category,"
" g.airdate,"
" cl.question,"
" cl.answer,"
" cl.value"
"from clues as cl"
"join categories as ca on cl.category_id = ca.id"
"join games as g on cl.game_id = g.id"
"order by random()"
"limit ?"]))

(defn random-clues [n]
(->> (execute! ds [random-clues-q n]
{:builder-fn as-unqualified-kebab-maps})
(filter valid-clue?)
(map clean-clue)))

(defn random-clue []
(first (random-clues 10)))
3 changes: 1 addition & 2 deletions src/com/tylerkindy/jeopardy/endless/incoming.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[com.tylerkindy.jeopardy.db.players :refer [get-player update-score]]
[com.tylerkindy.jeopardy.endless.live :refer [live-games send-all! transition!]]
[com.tylerkindy.jeopardy.endless.views :refer [answer-card buttons buzz-time-left-view category-reveal-time-left-view endless-container new-question-form players-view]]
[com.tylerkindy.jeopardy.jservice :refer [random-clue]]
[com.tylerkindy.jeopardy.clues :refer [random-clue]]
[com.tylerkindy.jeopardy.time :refer [now]]
[hiccup.util :refer [escape-html]])
(:import [java.util Timer TimerTask]))
Expand Down Expand Up @@ -46,7 +46,6 @@
(defn new-clue! [game-id]
(let [clue (-> (random-clue)
(select-keys [:category :airdate :question :answer :value])
(update :category :title)
(assoc :game-id game-id))]
(insert-clue ds clue)
(reveal-category game-id)
Expand Down
32 changes: 0 additions & 32 deletions src/com/tylerkindy/jeopardy/jservice.clj

This file was deleted.

0 comments on commit bf7d08d

Please sign in to comment.