Skip to content

Instantly share code, notes, and snippets.

@maruks
Created December 18, 2017 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maruks/952d6753e1296076cc01e2efead0860b to your computer and use it in GitHub Desktop.
Save maruks/952d6753e1296076cc01e2efead0860b to your computer and use it in GitHub Desktop.
(ns prison.core
(:require [gniazdo.core :as ws]
[clojure.data.json :as json]
[clojure.string :as str]))
(defn parse [str]
(json/read-str str :key-fn keyword))
(def score (atom {}))
(def last-round (atom nil))
(def msg-atom (atom nil))
(defn update-last-round [msg]
(let [t (->> msg
:lastRound
; ((juxt :north :south :west :east))
;(into {})
)]
(reset! last-round t))
)
(declare socket)
(defn handle [msg]
(reset! msg-atom msg)
(update-last-round msg)
(ws/send-msg socket (json/write-str {:tag "SetColor" :contents "#0000ff"}))
(doseq [[dir [_ move]] @last-round]
(ws/send-msg socket (json/write-str {:tag "MakeChoice" :contents [move (-> dir name str/capitalize)]})))
(comment(ws/send-msg socket (json/write-str {:tag "MakeChoice" :contents ["Betray" "North"]}))
(ws/send-msg socket (json/write-str {:tag "MakeChoice" :contents ["Betray" "South"]}))
(ws/send-msg socket (json/write-str {:tag "MakeChoice" :contents ["Betray" "West"]}))
(ws/send-msg socket (json/write-str {:tag "MakeChoice" :contents ["Betray" "East"]}))))
(defonce socket
(ws/connect
"ws://game.clearercode.com:8000"
:on-receive #(handle (parse %))))
#_(
(ws/send-msg socket "hello")
(ws/close socket)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment