I'm trying to remake a guessing game engine - changing it from Clojure to ClojureScript.…
- User
I'm trying to remake a guessing game engine - changing it from Clojure to ClojureScript. Here is the original .clj file for backend behaviour (note that this does not handle the actual game logic, that's in a separate file).
(ns reagent-2024.app-server (:require [clojure.pprint] [clojure.string :as str] [compojure.coercions :refer [as-int]] [compojure.core :refer [ANY defroutes GET POST]] [compojure.route :as route] [ring.adapter.jetty :refer [run-jetty]] [ring.middleware.defaults :as middleware] [ring.middleware.cookies :as cookies] [ring.middleware.multipart-params :as multi] [ring.mock.request :as mock] [ring.util.response :as ring :refer [not-found redirect response status]] [simple-server.simple-game :as game] [byte-transforms :as transforms] [hiccup.page :refer [html5 include-js include-css]]))
(defn index-html [] (html5 [:head [:meta {:charset "UTF-8"}] [:meta {:name "viewport" :content "width=device-width, initial-scale=1"}] (include-css "/css/style.css")] [:body [:h2 "Text being served by the app-server Ring handler."] [:div {:id "app"}] (include-js "/cljs-out/dev-main.js")]))
;; (defn random-api [] ;; (println "I've been called!") ;; {:status 200 ;; :body (pr-str {:lucky-number (rand-int 1000) ;; :a-set #{1 "foo" :baz [::a ::b]}}) ;; :headers {"Content-Type" "application/edn"}})
(defn set-session-cookie [response username] (ring/set-cookie response "token" (transforms/hash username :crc32) {:max-age 3600})) ; Set cookie with a maximum age of 1 hour
(defn get-session-cookie [request] (get-in request [:cookies "token" :value]))
(defn new-game-handler [request] (when (game/new-game! (get-session-cookie request)) (response (str "OK - start guessing at /guess/?guess=N"))))
(defn form-sanitizer [input] (str/lower-case (str/trim input)))
(defn valid-login? [token password] ;; placeholder login logic... (or (and (= token "foo") (= password "bar")) (and (= token "admin") (= password "123")) (and (= token "user") (= password "pass")) (and (= token "egg") (= password "man")) (and (= token "test") (= password "test"))))
;; tried to combine these functions but the login page is very fragile (defn login-page-handler [] (response (slurp "resources/public/login.html"))) ;; (defn guess-page-handler [] ;; (response (slurp "res/guess.html")))
(defn login-handler [request] (let [params (:form-params request) username (form-sanitizer (get params "username")) password (get params "password")] (if (valid-login? username password) (-> (redirect "/new-game") (set-session-cookie username)) ;; (redirect (str "/new-game/" token)) (response "Invalid login. Try again."))))
(defn guess-handler [guess user-hash] (condp = (game/guess-answer guess user-hash) nil (-> (response "You need to supply a guess with /guess?guess=N") (status 400)) :game-win (response (str "Congratulations! You win!")) :game-over (response "Too bad! You ran out of tries!") :too-low (response (str "Too low! " (game/get-remaining-tries user-hash) " tries remaining!")) :too-high (response (str "Too high! " (game/get-remaining-tries user-hash) " tries remaining!"))))
- Assistant
- User
Can you make the specific changes you mentioned to the frontend code?
- Assistant
- User
Where should the routing be, and how do I make it work with this?
- Assistant
- User
How about the backend for the login part? I hope you remember the code from earlier.
- Assistant