Permalink
Browse files

Managing game list in lobby using ajax calls

Should probably use ws and push the updated game list when necessary.
Oh well
  • Loading branch information...
tskardal committed Jan 19, 2015
1 parent fd5349d commit f7f47ffd1114cf939af9e0e9a847a4fc8afeed22
Showing with 82 additions and 27 deletions.
  1. +4 −2 project.clj
  2. +30 −8 src/clj/battlesnake/server.clj
  3. +34 −4 src/cljs/battlesnake/lobby.cljs
  4. +14 −13 src/cljs/battlesnake/snake.cljs
@@ -8,11 +8,13 @@
[jarohen/chord "0.4.2" :exclusions [org.clojure/clojure]]
[javax.servlet/servlet-api "2.5"]
[org.clojure/clojure "1.6.0"]
[org.clojure/clojurescript "0.0-2496"]
[org.clojure/clojurescript "0.0-2496"]
[cljs-ajax "0.3.9"]
[org.clojure/core.async "0.1.346.0-17112a-alpha"]
[reagent "0.5.0-alpha"]
[ring/ring-core "1.3.2"]
[ring/ring-devel "1.3.2"]]
[ring/ring-devel "1.3.2"]
[ring-middleware-format "0.4.0"]]
:plugins [[lein-cljsbuild "1.0.4"]]
:hooks [leiningen.cljsbuild]
:cljsbuild {:builds
@@ -1,13 +1,19 @@
(ns battlesnake.server
(:gen-class)
(:require [clojure.java.io :as io]
[compojure.core :refer [defroutes GET]]
(:require [chord.http-kit :refer [wrap-websocket-handler]]
[clojure.core.async :refer [<! >! put! close! go-loop]]
[clojure.java.io :as io]
[compojure.core :refer [defroutes GET POST]]
[compojure.handler :refer [site]]
[compojure.route :refer [resources]]
[org.httpkit.server :refer [run-server]]
[ring.middleware.reload :as reload]
[chord.http-kit :refer [wrap-websocket-handler]]
[clojure.core.async :refer [<! >! put! close! go-loop]]))
[ring.middleware.format :refer [wrap-restful-format]]
[ring.middleware.reload :as reload]))
(def idseq (java.util.concurrent.atomic.AtomicInteger.))
(defn next-id [] (.getAndIncrement idseq))
(def games (atom {}))
(defn ws-handler [{:keys [ws-channel] :as req}]
(println "Connection from " (:remote-addr req))
@@ -19,15 +25,31 @@
{:received (format "You passed: '%s' at %s." (pr-str message) (java.util.Date.))}))
(recur))))
(defn active-games []
(filter #(= :active (:status %))(vals @games)))
(defn game-info [id]
(get @games id))
(defn create-game [name]
(let [id (next-id)]
(println "creating " name)
(swap! games assoc id {:id id :name name :status :active})))
(defroutes app
(GET "/" [] (io/resource "public/index.html"))
(GET "/ws" [] (-> ws-handler wrap-websocket-handler))
(GET "/games" [] (active-games))
(GET "/games/:id" [id] (game-info id))
(POST "/games" {{name :game-name} :body-params} (create-game name))
(resources "/"))
(defn start-server []
(let [handler (reload/wrap-reload (site #'app))]
(run-server handler {:port 3000})
(println "Server running!")))
(-> (site #'app)
(reload/wrap-reload)
(wrap-restful-format)
(run-server {:port 3000}))
(println "Server running!"))
(defn -main [& args]
(start-server))
@@ -1,26 +1,49 @@
(ns battlesnake.lobby
(:require [chord.client :refer [ws-ch]]
[cljs.core.async :refer [chan <! >! put! close! timeout]]
[reagent.core :as reagent :refer [atom]])
[reagent.core :as reagent :refer [atom]]
[ajax.core :refer [GET POST]])
(:require-macros [cljs.core.async.macros :refer [go go-loop]]))
(def game-name (atom ""))
(def all-games (atom [{:name "Awesome game"} {:name "Less awesome game"}]))
(def all-games (atom []))
(defn handler [response]
(.log js/console (str response)))
(defn error-handler [{:keys [status status-text]}]
(.log js/console (str "something bad happened: " status " " status-text)))
(defn- on-create-game [name]
(js/console.log (str "name: " name))
(reset! game-name "")
(POST "/games"
{:params {:game-name name
:created-by "Bob"}
:format :edn
:handler handler
:error-handler error-handler}))
(defn- update-game-list [fetched-games]
(reset! all-games fetched-games))
(defn fetch-games []
(GET "/games" {:handler update-game-list :response-format :edn}))
(defn create-game []
[:div
[:h3 "Create a game"]
[:input {:type "text" :value @game-name :placeholder "Name the game!"
:on-change #(reset! game-name (-> % .-target .-value))}]
[:input {:type "button" :value "Start" :on-click #(reset! game-name "")}]])
[:input {:type "button" :value "Start" :on-click #(on-create-game @game-name)}]])
(defn game-list []
[:ul
(for [game @all-games]
[:li (game :name)])])
(defn lobby []
[:div
[:div
[create-game]
[game-list]])
@@ -30,8 +53,15 @@
(js/console.log (str "msg: " message))
(recur))))
(defn update-games []
(go-loop []
(fetch-games)
(<! (timeout 5000))
(recur)))
(defn ^:export init [parent]
(go (let [{:keys [ws-channel]} (<! (ws-ch "ws://localhost:3000/ws"))]
(listen ws-channel)
(>! ws-channel "Hoist")))
(update-games)
(reagent/render-component [lobby] parent))
@@ -77,16 +77,17 @@
(reset! snake (create-snake))
(let [body (aget js/document "body")]
(.addEventListener body "keydown" handle-input))
(js/setInterval move-snake 60)
(js/setInterval (fn []
(when (< 2 (count @edibles))
(swap! edibles butlast))
(swap! edibles conj (create-edible)) ; TODO ensure no duplicates
(js/console.log (str "edibles: " @edibles))) 5000)
(let [ctx (.getContext canvas "2d")]
(letfn [(render []
(.clearRect ctx 0 0 500 500)
(render-snake ctx @snake)
(render-edibles ctx @edibles)
(.requestAnimationFrame js/window render))]
(.requestAnimationFrame js/window render))))
(comment
(js/setInterval move-snake 60)
(js/setInterval (fn []
(when (< 2 (count @edibles))
(swap! edibles butlast))
(swap! edibles conj (create-edible)) ; TODO ensure no duplicates
(js/console.log (str "edibles: " @edibles))) 5000)
(let [ctx (.getContext canvas "2d")]
(letfn [(render []
(.clearRect ctx 0 0 500 500)
(render-snake ctx @snake)
(render-edibles ctx @edibles)
(.requestAnimationFrame js/window render))]
(.requestAnimationFrame js/window render)))))

0 comments on commit f7f47ff

Please sign in to comment.