<commit_before>(ns battlesnake.server
  (:gen-class)
  (: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.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))
  (go-loop []
    (when-let [{:keys [message error] :as msg} (<! ws-channel)]
      (prn "Message received:" msg)
      (>! ws-channel (if error
                       (format "Error: '%s'." (pr-str msg))
                       {:received (format "You passed: '%s' at %s." (pr-str message) (java.util.Date.))}))
      (recur))))

(defn game-states []
  )

(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 :players []})))

(defn join-game [id]
  (let [player-id (java.util.UUID/randomUUID)
        players (get-in @games [id :players])]
    (swap! games assoc-in [id :players] (conj players {:player-id player-id}))
    player-id))

(defroutes app
  (GET "/" [] (io/resource "public/index.html"))
  (GET "/ws" [] (-> ws-handler wrap-websocket-handler))
  (GET "/games" [] (active-games))
  (GET "/games/:id" [id] (io/resource "public/game.html"))
  (POST "/games" {{name :game-name} :body-params} (create-game name))
  (resources "/"))

(defn start-server []
  (-> (site #'app)
      (reload/wrap-reload)
      (wrap-restful-format)      
      (run-server {:port 3000}))
  (println "Server running!"))

(defn -main [& args]
  (start-server))
<commit_msg>Avoid wrapping WS route with middleware<commit_after>(ns battlesnake.server
  (:gen-class)
  (: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.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))
  (go-loop []
    (when-let [{:keys [message error] :as msg} (<! ws-channel)]
      (prn "Message received:" msg)
      (>! ws-channel (if error
                       (format "Error: '%s'." (pr-str msg))
                       {:received (format "You passed: '%s' at %s." (pr-str message) (java.util.Date.))}))
      (recur))))

(defn game-states []
  )

(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 :players []})))

(defn join-game [id]
  (let [player-id (java.util.UUID/randomUUID)
        players (get-in @games [id :players])]
    (swap! games assoc-in [id :players] (conj players {:player-id player-id}))
    player-id))

(defroutes app
  (GET "/" [] (io/resource "public/index.html"))  
  (GET "/games" [] (active-games))
  (GET "/games/:id" [id] (io/resource "public/game.html"))
  (POST "/games" {{name :game-name} :body-params} (create-game name))
  (resources "/"))

(defroutes ws
  (GET "/ws" [] (-> ws-handler wrap-websocket-handler)))

(def handler (routes (site (wrap-restful-format app)) ws))

(defn start-server []
  (-> handler
      (reload/wrap-reload)      
      (run-server {:port 3000}))
  (println "Server running!"))

(defn -main [& args]
  (start-server))

{'fraction_non_alphanumeric': 0.18965916995131, 'fraction_numerical': 0.0018548574078367725, 'mean_word_length': 2.848349687778769, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '220543', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1528, 'n_words': 380}