ok I think that cleans everything up

This commit is contained in:
Luciano Laratelli 2025-03-14 11:04:10 -04:00
parent 51d3fa0dd1
commit b06e47eba3
3 changed files with 122 additions and 60 deletions

View file

@ -1,6 +1,7 @@
CREATE TABLE IF NOT EXISTS player ( CREATE TABLE IF NOT EXISTS player (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
score INTEGER NOT NULL, game_score INTEGER NOT NULL,
round_score INTEGER NOT NULL,
name text NOT NULL, name text NOT NULL,
game_code text NOT NULL, game_code text NOT NULL,
play_order integer NOT NULL, play_order integer NOT NULL,

View file

@ -9,6 +9,7 @@
[com.biffweb.my-project.app :as app] [com.biffweb.my-project.app :as app]
[com.biffweb.my-project.auth-module :as auth-module] [com.biffweb.my-project.auth-module :as auth-module]
[rum.core :as rum] [rum.core :as rum]
[honey.sql :as sql]
[com.biffweb.my-project.email :as email] [com.biffweb.my-project.email :as email]
[com.biffweb.my-project.home :as home] [com.biffweb.my-project.home :as home]
[com.biffweb.my-project.middleware :as mid] [com.biffweb.my-project.middleware :as mid]

View file

@ -46,13 +46,14 @@
(jdbc/execute! ds (sql/format {:insert-into :player (jdbc/execute! ds (sql/format {:insert-into :player
:values (for [[p o] (partition 2 (interleave players player-order))] :values (for [[p o] (partition 2 (interleave players player-order))]
{:score 0 {:game_score 0
:round_score 0
:name p :name p
:play_order o :play_order o
:game_code code})})) :game_code code})}))
{:status 200 {:status 200
:headers {"HX-redirect" (str/join "/" ["" "game" code "display"])} :headers {"HX-Redirect" (str/join "/" ["" "game" code "display"])}
:session {:id id}})))) :session {:id id}}))))
(defn display-game [{:keys [session params path-params] (defn display-game [{:keys [session params path-params]
@ -69,7 +70,11 @@
:from :game :from :game
:where [:= :code code]})) :where [:= :code code]}))
current-player (:game/current_player game)] current-player (:game/current_player game)]
(println code game)
(if-not (and code game)
{:status 200
:headers {"HX-Redirect" "/?err=no-code"}}
(ui/page (ui/page
{} {}
[:div [:div
@ -91,14 +96,14 @@
[:thead [:thead
[:tr [:tr
[:th {:scope "col"} "Player"] [:th {:scope "col"} "Player"]
[:th {:scope "col"} "Score"]]] [:th {:scope "col"} "Total Score"]]]
(into [:tbody (into [:tbody
{:hx-ext "ws,multi-swap" {:hx-ext "ws,multi-swap"
:ws-connect (str "/game/" (:game/code game) "/connect") :ws-connect (str "/game/" (:game/code game) "/connect")
:hx-swap player-elements-swap-str}] :hx-swap player-elements-swap-str}]
(for [p players (for [p players
:let [{:player/keys [id play_order score]} p]] :let [{:player/keys [id play_order game_score]} p]]
[:tr [:tr
[:th {:scope "row"} [:div [:th {:scope "row"} [:div
@ -110,12 +115,12 @@
:width "8px" :width "8px"
:margin-left "20px" :margin-left "20px"
:border-radius "5px" :border-radius "5px"
:align-items "center"}} "now playing"])]] :align-items "center"}}
"now playing"])]]
[:td [:div [:td [:div
{:id (str "player" id "-" play_order)} {:id (str "player" id "-" play_order "-game-score")}
game_score]]]))]])]))))
score]]]))]])])))
(defn connect-ws [{:keys [session params] (defn connect-ws [{:keys [session params]
:example/keys [chat-clients ds] :as ctx}] :example/keys [chat-clients ds] :as ctx}]
@ -191,9 +196,36 @@
[:button {:type "submit"} "Score pigs"] [:button {:type "submit"} "Score pigs"]
[:button.contrast {:type "submit"} "Pigs are touching! (Lose all points)"])]))) [:button.contrast {:type "submit"} "Pigs are touching! (Lose all points)"])])))
(def game-code-input-attrs
{:name "game-code",
:hx-target "this"
:hx-swap "outerHTML"
:placeholder "game code",
:type "text"})
(defn route-to-game-view
[{:keys [session params]
:example/keys [ds]
:as _ctx}]
(let [code (:game-code params)
view-type (:view-type params)
game (delay (jdbc/execute-one! ds (sql/format {:select :* :from :game :where [:= :code code]})))]
(cond
(not (and code @game))
(error-style "Couldn't find that game.")
(= view-type "show-scoreboard")
{:status 200
:headers {"HX-Redirect" (str/join "/" ["/game" code "display"])}}
(= view-type "show-game-remote")
{:status 200
:headers {"HX-Redirect" (str/join "/" ["/game" code "control"])}})))
(defn app [{:keys [session params] (defn app [{:keys [session params]
:example/keys [ds] :example/keys [ds]
:as _ctx}] :as _ctx}]
(pp/pprint session)
(let [id (:id session)] (let [id (:id session)]
(ui/page (ui/page
@ -209,22 +241,49 @@
[:section [:section
[:button {:_ "on click toggle the *display of #new-game-form"} "New game"]] [:button {:_ "on click toggle the *display of #new-game-form"} "New game"]]
(biff/form
{:hx-post "/game/create"
:style {:display :none}
:hx-swap "afterend"
:id ::new-game-form}
[:div [:div
[:textarea#players {:type "textarea" :rows "8" :name "players"}]
[:fieldset
[:legend "Game options:"]
[:label [:label
"Existing game? Enter the code here:" [:input {:type "checkbox", :name "random-player-order", :checked ""}]
"Random player order"]
;; [:label
;; [:input {:type "checkbox", :name "french", :checked ""}]
;; "French"]
]
[:button {:type "submit"} "Start"]])
(biff/form
{:id ::display-existing-game
:hx-post "/game/route-to-view"
:hx-swap "afterend"}
[:fieldset.grid
[:input [:input
{:name "game-code", {:hidden :true
:placeholder "game code", :name ::view-type
:type "text" :value ::show-scoreboard}]
:_ "on keyup [:input game-code-input-attrs]
set #control-existing-game@href to '/game/' + my.value + '/control'
then [:input {:type :submit :value "Show scoreboard"}]])
set #display-existing-game@href to '/game/' + my.value + '/display' "}]]
[:div {:role "group"} (biff/form
[:a {:id ::display-existing-game {:id ::control-existing-game
:type "button"} "Show scoreboard"] :hx-post "/game/route-to-view"
[:a.secondary {:id ::control-existing-game :hx-swap "afterend"}
:type "button"} "Start game control"]]]]))) [:fieldset.grid
[:input
{:hidden :true
:name ::view-type
:value ::show-game-remote}]
[:input game-code-input-attrs]
[:input.secondary {:type :submit :value "Control an existing game"}]])])))
(def about-page (def about-page
(ui/page (ui/page
@ -243,6 +302,7 @@
["" {:get app}] ["" {:get app}]
["game/" ["game/"
["create" {:post create-game}] ["create" {:post create-game}]
["route-to-view" {:post route-to-game-view}]
[":code" [":code"
["/display" {:get display-game}] ["/display" {:get display-game}]
["/control" {:get control-view}] ["/control" {:get control-view}]