Created
December 18, 2017 20:14
-
-
Save maruks/952d6753e1296076cc01e2efead0860b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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