websockets!

This commit is contained in:
Luciano Laratelli 2025-03-13 12:49:59 -04:00
parent ab57a15c12
commit 2488332581
2 changed files with 55 additions and 20 deletions

View file

@ -2,11 +2,13 @@
(:require
[migratus.core :as migratus]
[clojure.test :as test]
[ring.adapter.jetty9 :as jetty]
;; [clojure.tools.logging :as log]
[clojure.tools.namespace.repl :as tn-repl]
[com.biffweb :as biff]
[com.biffweb.my-project.app :as app]
[com.biffweb.my-project.auth-module :as auth-module]
[rum.core :as rum]
[com.biffweb.my-project.email :as email]
[com.biffweb.my-project.home :as home]
[com.biffweb.my-project.middleware :as mid]
@ -54,7 +56,7 @@
:biff/handler #'handler
:biff.beholder/on-save #'on-save
:biff.middleware/on-error #'ui/on-error
:example/chat-clients (atom #{})})
:example/chat-clients (atom {})})
(defonce system (atom {}))

View file

@ -80,20 +80,52 @@
[:button.secondary "Reset"])]]]
[:h4 id]
[:div
[:h4 "Game code is " (:game/code game)]
[:table
[:thead
[:tr
[:th {:scope "col"} "Player"]
[:th {:scope "col"} "Score"]]]
(let [player-elements-swap-str (->> players
(map (fn [{:player/keys [id play_order]}]
(str "#player" id "-" play_order)))
(str/join ",")
(str "multi:"))]
[:div
[:h4 "Game code is " (:game/code game)]
[:table
[:thead
[:tr
[:th {:scope "col"} "Player"]
[:th {:scope "col"} "Score"]]]
(into [:tbody]
(for [p players]
[:tr
[:th {:scope "row"} (:player/name p)]
[:td (:player/score p)]]))]]])))
(into [:tbody
{:hx-ext "ws,multi-swap"
:ws-connect (str "/game/" (:game/code game) "/connect")
:hx-swap player-elements-swap-str}]
(for [p players
:let [{:player/keys [id play_order score]} p]]
[:tr
[:th {:scope "row"} (:player/name p)]
[:td [:div
{:id (str "player" id "-" play_order)}
score]]]))]])])))
(defn connect-ws [{:keys [session params]
:example/keys [chat-clients ds] :as ctx}]
(let [{:game/keys [code]}
(jdbc/execute-one! ds (sql/format {:select :*
:from :game
:where [:= :id (:id session)]}))]
{:status 101
:headers {"upgrade" "websocket"
"connection" "upgrade"}
:ws {:on-connect (fn [ws]
(swap! chat-clients assoc code ws))
:on-close (fn [ws status-code reason]
(swap! chat-clients
(fn [chat-clients]
(let [chat-clients (update chat-clients code disj ws)]
(cond-> chat-clients
(empty? (get chat-clients code)) (dissoc code))))))}}))
(defn checkbox [n]
[:label
[:input {:type "checkbox", :name n}]
@ -128,7 +160,8 @@
[:ul [:li (biff/form {:id "reset"
:hx-get "/reset"}
[:button.secondary "Reset"])]]]
[:h4 "Playing next:" (-> (get players current-player) :player/name)]
[:div.grid
[:h4 "Playing next:" (-> (get players current-player) :player/name)]]
(biff/form {:id ::score-form}
[:table
@ -138,14 +171,12 @@
[:th {:scope "col"} "Pig 2"]]]
[:tbody
(for [p1 pig-position
;; p2 pig-position
]
(for [p pig-position]
[:tr
[:td
(checkbox p1)]
(checkbox p)]
[:td (checkbox p1)]])]]
[:td (checkbox p)]])]]
[:button {:type "submit"} "Score pigs"]
[:button.contrast {:type "submit"} "Pigs are touching! (Lose all points)"])])))
@ -227,7 +258,9 @@
["" {:get app}]
["game/"
["" {:get game}]
[":code" {:get control-view}]]
[":code"
["/" {:get control-view}]
["/connect" {:get connect-ws}]]]
["connect" {:post connect}]
["reset" {:get reset}]