clean up paths
This commit is contained in:
parent
2488332581
commit
51d3fa0dd1
1 changed files with 46 additions and 63 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
(ns com.biffweb.my-project.app
|
(ns com.biffweb.my-project.app
|
||||||
(:require
|
(:require
|
||||||
[clojure.pprint :as pprint]
|
|
||||||
[clojure.pprint :as pp]
|
[clojure.pprint :as pp]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
[com.biffweb :as biff]
|
[com.biffweb :as biff]
|
||||||
|
|
@ -25,7 +24,7 @@
|
||||||
(defn error-style [s]
|
(defn error-style [s]
|
||||||
[:h4.pico-color-red-500 s])
|
[:h4.pico-color-red-500 s])
|
||||||
|
|
||||||
(defn set-bar [{:keys [example/ds session params] :as _ctx}]
|
(defn create-game [{:keys [example/ds session params] :as _ctx}]
|
||||||
(let [players (map str/trim (-> params :players str/split-lines))]
|
(let [players (map str/trim (-> params :players str/split-lines))]
|
||||||
(if (> 2 (count players))
|
(if (> 2 (count players))
|
||||||
(error-style "Need at least two players")
|
(error-style "Need at least two players")
|
||||||
|
|
@ -53,22 +52,23 @@
|
||||||
:game_code code})}))
|
:game_code code})}))
|
||||||
|
|
||||||
{:status 200
|
{:status 200
|
||||||
:headers {"HX-redirect" "/game"}
|
:headers {"HX-redirect" (str/join "/" ["" "game" code "display"])}
|
||||||
:session {:id id}}))))
|
:session {:id id}}))))
|
||||||
|
|
||||||
(defn game [{:keys [session params]
|
(defn display-game [{:keys [session params path-params]
|
||||||
:example/keys [ds]
|
:example/keys [ds]
|
||||||
:as _ctx}]
|
:as _ctx}]
|
||||||
(let [id (:id session)
|
(let [code (:code path-params)
|
||||||
_ (println id)
|
players (into []
|
||||||
_ (println (str id))
|
(sort-by
|
||||||
game (jdbc/execute-one! ds (sql/format {:select :*
|
:player/play_order
|
||||||
:from :game
|
|
||||||
:where [:= :id id]}))
|
|
||||||
players (sort-by :player/play_order
|
|
||||||
(jdbc/execute! ds (sql/format {:select :*
|
(jdbc/execute! ds (sql/format {:select :*
|
||||||
:from :player
|
:from :player
|
||||||
:where [:= :game_code (:game/code game)]})))]
|
:where [:= :game_code code]}))))
|
||||||
|
game (jdbc/execute-one! ds (sql/format {:select :*
|
||||||
|
:from :game
|
||||||
|
:where [:= :code code]}))
|
||||||
|
current-player (:game/current_player game)]
|
||||||
|
|
||||||
(ui/page
|
(ui/page
|
||||||
{}
|
{}
|
||||||
|
|
@ -78,7 +78,7 @@
|
||||||
[:ul [:li (biff/form {:id "reset"
|
[:ul [:li (biff/form {:id "reset"
|
||||||
:hx-get "/reset"}
|
:hx-get "/reset"}
|
||||||
[:button.secondary "Reset"])]]]
|
[:button.secondary "Reset"])]]]
|
||||||
[:h4 id]
|
[:h4 (:game/id game)]
|
||||||
|
|
||||||
(let [player-elements-swap-str (->> players
|
(let [player-elements-swap-str (->> players
|
||||||
(map (fn [{:player/keys [id play_order]}]
|
(map (fn [{:player/keys [id play_order]}]
|
||||||
|
|
@ -99,8 +99,19 @@
|
||||||
: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 score]} p]]
|
||||||
|
|
||||||
[:tr
|
[:tr
|
||||||
[:th {:scope "row"} (:player/name p)]
|
[:th {:scope "row"} [:div
|
||||||
|
(:player/name p)
|
||||||
|
(when (= (:player/play_order p) current-player)
|
||||||
|
[:span.pico-background-jade-600
|
||||||
|
{:style {:text-align "center"
|
||||||
|
:padding "4px 8px"
|
||||||
|
:width "8px"
|
||||||
|
:margin-left "20px"
|
||||||
|
:border-radius "5px"
|
||||||
|
:align-items "center"}} "now playing"])]]
|
||||||
|
|
||||||
[:td [:div
|
[:td [:div
|
||||||
{:id (str "player" id "-" play_order)}
|
{:id (str "player" id "-" play_order)}
|
||||||
|
|
||||||
|
|
@ -161,7 +172,7 @@
|
||||||
:hx-get "/reset"}
|
:hx-get "/reset"}
|
||||||
[:button.secondary "Reset"])]]]
|
[:button.secondary "Reset"])]]]
|
||||||
[:div.grid
|
[:div.grid
|
||||||
[:h4 "Playing next:" (-> (get players current-player) :player/name)]]
|
[:h4 "Now playing: " [:b.pico-color-jade-600 (-> (get players current-player) :player/name)]]]
|
||||||
|
|
||||||
(biff/form {:id ::score-form}
|
(biff/form {:id ::score-form}
|
||||||
[:table
|
[:table
|
||||||
|
|
@ -180,19 +191,6 @@
|
||||||
[: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)"])])))
|
||||||
|
|
||||||
(defn connect
|
|
||||||
[{:keys [session params]
|
|
||||||
:example/keys [ds]
|
|
||||||
:as _ctx}]
|
|
||||||
(let [code (:game-code params)
|
|
||||||
game (delay (jdbc/execute-one! ds (sql/format {:select :* :from :game
|
|
||||||
:where [:= :code code]})))]
|
|
||||||
(if (and code @game)
|
|
||||||
{:status 200
|
|
||||||
:session session
|
|
||||||
:headers {"HX-Redirect" (str "/game/" code)}}
|
|
||||||
(error-style "Code does not exist."))))
|
|
||||||
|
|
||||||
(defn app [{:keys [session params]
|
(defn app [{:keys [session params]
|
||||||
:example/keys [ds]
|
:example/keys [ds]
|
||||||
:as _ctx}]
|
:as _ctx}]
|
||||||
|
|
@ -211,35 +209,22 @@
|
||||||
[: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 "/set-bar"
|
|
||||||
: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
|
||||||
[:input {:type "checkbox", :name "random-player-order", :checked ""}]
|
"Existing game? Enter the code here:"
|
||||||
"Random player order"]
|
|
||||||
;; [:label
|
|
||||||
;; [:input {:type "checkbox", :name "french", :checked ""}]
|
|
||||||
;; "French"]
|
|
||||||
]
|
|
||||||
|
|
||||||
[:button {:type "submit"} "Start"]])
|
|
||||||
(biff/form
|
|
||||||
{:hx-post "/connect"
|
|
||||||
:hx-swap "afterend"
|
|
||||||
:id ::connect-to-game}
|
|
||||||
[:fieldset
|
|
||||||
{:role "group"}
|
|
||||||
[:input
|
[:input
|
||||||
{:name "game-code",
|
{:name "game-code",
|
||||||
:type "text",
|
:placeholder "game code",
|
||||||
:placeholder "Connect to an existing game"}]
|
:type "text"
|
||||||
[:input {:type "submit", :value "Connect"}]])])))
|
:_ "on keyup
|
||||||
|
set #control-existing-game@href to '/game/' + my.value + '/control'
|
||||||
|
then
|
||||||
|
set #display-existing-game@href to '/game/' + my.value + '/display' "}]]
|
||||||
|
[:div {:role "group"}
|
||||||
|
[:a {:id ::display-existing-game
|
||||||
|
:type "button"} "Show scoreboard"]
|
||||||
|
[:a.secondary {:id ::control-existing-game
|
||||||
|
:type "button"} "Start game control"]]]])))
|
||||||
|
|
||||||
(def about-page
|
(def about-page
|
||||||
(ui/page
|
(ui/page
|
||||||
|
|
@ -257,12 +242,10 @@
|
||||||
:routes ["/" {:middleware [wrap-session]}
|
:routes ["/" {:middleware [wrap-session]}
|
||||||
["" {:get app}]
|
["" {:get app}]
|
||||||
["game/"
|
["game/"
|
||||||
["" {:get game}]
|
["create" {:post create-game}]
|
||||||
[":code"
|
[":code"
|
||||||
["/" {:get control-view}]
|
["/display" {:get display-game}]
|
||||||
|
["/control" {:get control-view}]
|
||||||
["/connect" {:get connect-ws}]]]
|
["/connect" {:get connect-ws}]]]
|
||||||
|
["reset" {:get reset}]]
|
||||||
["connect" {:post connect}]
|
|
||||||
["reset" {:get reset}]
|
|
||||||
["set-bar" {:post set-bar}]]
|
|
||||||
:api-routes [["/api/echo" {:post echo}]]})
|
:api-routes [["/api/echo" {:post echo}]]})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue