Skip to content

Commit

Permalink
Make gameboard use edn instead of json
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahTheDuke committed Jul 18, 2024
1 parent 2d7ea55 commit 54de01e
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 345 deletions.
175 changes: 98 additions & 77 deletions src/clj/game/core/commands.clj

Large diffs are not rendered by default.

File renamed without changes.
15 changes: 9 additions & 6 deletions src/clj/game/core/toasts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
([state side message msg-type] (toast state side message msg-type nil))
([state side message msg-type options]
;; Allows passing just the toast msg-type as the options parameter
(when message
;; normal toast - add to list
(swap! state update-in [side :toast] #(conj % {:msg message :type msg-type :options options :id (random-uuid)})))))
(when message
(let [new-toast {:id (random-uuid)
:msg message
:type msg-type
:options options}]
;; normal toast - add to list
(swap! state update-in [side :toast] conj new-toast)))))

(defn ack-toast
([state side {:keys [id]}]
(when-let [id (when (string? id) (parse-uuid id))]
(swap! state update-in [side :toast] (fn [toasts] (remove #(= (:id %) id) toasts))))))
[state side {:keys [id]}]
(swap! state update-in [side :toast] (fn [toasts] (remove #(= (:id %) id) toasts))))

(defn show-error-toast
[state side]
Expand Down
14 changes: 6 additions & 8 deletions src/clj/game/utils.clj → src/clj/game/utils.cljc
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
(ns game.utils
(:require
[jinteki.cards :refer [all-cards]]
[clojure.string :as str]
[clj-uuid :as uuid]))
[jinteki.cards :refer [all-cards]]))

(defn make-cid []
(uuid/to-string (uuid/v4)))
(str (random-uuid)))

(defn server-card
([title] (server-card title true))
Expand All @@ -15,7 +14,8 @@
(and title card) card
(or (= title "Corp Basic Action Card") (= title "Runner Basic Action Card")) {}
:else (when strict?
(throw (Exception. (str "Tried to select server-card for " title))))))))
(throw #?(:clj (Exception. (str "Tried to select server-card for " title))
:cljs (js/Error. (str "Tried to select server-card for " title)))))))))

(defn server-cards
[]
Expand Down Expand Up @@ -46,10 +46,8 @@
(step coll #{})))

(defn string->num [s]
(try
(let [num (bigdec s)]
(if (and (> num Integer/MIN_VALUE) (< num Integer/MAX_VALUE)) (int num) num))
(catch Exception _ nil)))
(when s
(parse-long s)))

(def safe-split (fnil str/split ""))

Expand Down
30 changes: 20 additions & 10 deletions src/clj/web/game.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns web.game
(:require
[cheshire.core :as json]
[cljc.java-time.instant :as inst]
[clojure.stacktrace :as stacktrace]
[clojure.walk :refer [postwalk]]
[cond-plus.core :refer [cond+]]
[game.core :as core]
[game.core.diffs :as diffs]
Expand All @@ -14,17 +14,26 @@
[web.stats :as stats]
[web.ws :as ws]))

(defn serialize-edn [obj]
(cond (record? obj) (into {} obj)
(instance? org.bson.types.ObjectId obj) (str obj)
(instance? java.time.Instant obj) (str obj)
(instance? clojure.lang.LazySeq obj) (into [] obj)
(fn? obj) (str obj)
:else
obj))

(defn game-diff-json
"Converts the appropriate diff to json"
[gameid side {:keys [runner-diff corp-diff spect-diff]}]
(json/generate-string {:gameid gameid
:diff (cond
(= side "Corp")
corp-diff
(= side "Runner")
runner-diff
:else
spect-diff)}))
(postwalk
serialize-edn
{:gameid gameid
:diff (cond
(= side "Corp") corp-diff
(= side "Runner") runner-diff
:else spect-diff)}))


(defn send-state-diffs
"Sends diffs generated by public-diffs to all connected clients."
Expand All @@ -45,7 +54,8 @@
(send-state-diffs lobby diffs))))

(defn select-state [side {:keys [runner-state corp-state spect-state]}]
(json/generate-string
(postwalk
serialize-edn
(case side
"Corp" corp-state
"Runner" runner-state
Expand Down
62 changes: 21 additions & 41 deletions src/cljc/game/core/card.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -87,41 +87,34 @@
(defn in-server?
"Checks if the specified card is installed in -- and not PROTECTING -- a server"
[card]
(= (last (get-zone card)) #?(:clj :content
:cljs "content")))
(= (last (get-zone card)) :content))

(defn in-hand?
"Checks if the specified card is in the hand."
[card]
(= (get-zone card) #?(:clj [:hand]
:cljs ["hand"])))
(= (get-zone card) [:hand]))

(defn in-discard?
"Checks if the specified card is in the discard pile."
[card]
(= (get-zone card) #?(:clj [:discard]
:cljs ["discard"])))
(= (get-zone card) [:discard]))

(defn in-deck?
"Checks if the specified card is in the draw deck."
[card]
(= (get-zone card) #?(:clj [:deck]
:cljs ["deck"])))
(= (get-zone card) [:deck]))

(defn in-archives-root?
[card]
(= (get-zone card) #?(:clj [:servers :archives :content]
:cljs ["servers" "archives" "content"])))
(= (get-zone card) [:servers :archives :content]))

(defn in-hq-root?
[card]
(= (get-zone card) #?(:clj [:servers :hq :content]
:cljs ["servers" "hq" "content"])))
(= (get-zone card) [:servers :hq :content]))

(defn in-rd-root?
[card]
(= (get-zone card) #?(:clj [:servers :rd :content]
:cljs ["servers" "rd" "content"])))
(= (get-zone card) [:servers :rd :content]))

(defn in-root?
[card]
Expand All @@ -131,18 +124,15 @@

(defn protecting-archives?
[card]
(= (get-zone card) #?(:clj [:servers :archives :ices]
:cljs ["servers" "archives" "ices"])))
(= (get-zone card) [:servers :archives :ices]))

(defn protecting-hq?
[card]
(= (get-zone card) #?(:clj [:servers :hq :ices]
:cljs ["servers" "hq" "ices"])))
(= (get-zone card) [:servers :hq :ices]))

(defn protecting-rd?
[card]
(= (get-zone card) #?(:clj [:servers :rd :ices]
:cljs ["servers" "rd" "ices"])))
(= (get-zone card) [:servers :rd :ices]))

(defn protecting-a-central?
[card]
Expand All @@ -153,14 +143,12 @@
(defn in-play-area?
"Checks if the specified card is in the play area."
[card]
(= (get-zone card) #?(:clj [:play-area]
:cljs ["play-area"])))
(= (get-zone card) [:play-area]))

(defn in-set-aside?
"Checks if the specific card is in a set-aside area."
[card]
(= (get-zone card) #?(:clj [:set-aside]
:cljs ["set-aside"])))
(= (get-zone card) [:set-aside]))

(defn set-aside-visible?
"Checks if the specific card is in set aside and visible to this side"
Expand All @@ -173,20 +161,17 @@
(defn in-current?
"Checks if the specified card is in the 'current' zone."
[card]
(= (get-zone card) #?(:clj [:current]
:cljs ["current"])))
(= (get-zone card) [:current]))

(defn in-scored?
"Checks if the specified card is in _a_ score area (don't know which one)."
[card]
(= (get-zone card) #?(:clj [:scored]
:cljs ["scored"])))
(= (get-zone card) [:scored]))

(defn in-rfg?
"Checks if the specified card is in the 'remove from game' zone"
[card]
(= (get-zone card) #?(:clj [:rfg]
:cljs ["rfg"])))
(= (get-zone card) [:rfg]))

(defn- card-is?
"Checks the property of the card to see if it is equal to the given value,
Expand Down Expand Up @@ -315,15 +300,13 @@
(defn installed?
[card]
(or (:installed card)
(= (first (get-zone card)) #?(:clj :servers
:cljs "servers"))))
(= (first (get-zone card)) :servers)))

(defn facedown?
"Checks if the specified card is facedown."
[card]
(or (when-not (condition-counter? card)
(= (get-zone card) #?(:clj [:rig :facedown]
:cljs ["rig" "facedown"])))
(= (get-zone card) [:rig :facedown]))
(:facedown card)))

(defn active?
Expand Down Expand Up @@ -362,17 +345,14 @@
(defn can-be-advanced?
"Returns true if the card can be advanced"
([card]
(or (card-is? card :advanceable #?(:clj :always
:cljs "always"))
(or (card-is? card :advanceable :always)
;; e.g. Tyrant, Woodcutter
(and (card-is? card :advanceable #?(:clj :while-rezzed
:cljs "while-rezzed"))
(and (card-is? card :advanceable :while-rezzed)
(rezzed? card))
;; e.g. Haas Arcology AI
(and (card-is? card :advanceable #?(:clj :while-unrezzed
:cljs "while-unrezzed"))
(and (card-is? card :advanceable :while-unrezzed)
(not (rezzed? card)))
(and (is-type? card "Agenda")
(and (agenda? card)
(installed? card))))
([state card]
(and (can-be-advanced? card)
Expand Down
21 changes: 7 additions & 14 deletions src/cljs/nr/gameboard/actions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
[nr.angel-arena.lobby :as angel-arena]
[nr.appstate :refer [app-state current-gameid]]
[nr.gameboard.replay :refer [init-replay]]
[nr.gameboard.state :refer [check-lock? game-state get-side last-state
parse-state]]
[nr.gameboard.state :refer [check-lock? game-state get-side last-state]]
[nr.translations :refer [tr]]
[nr.utils :refer [toastr-options]]
[nr.ws :as ws]
Expand Down Expand Up @@ -42,7 +41,7 @@
(-> "#gamelobby" js/$ .fadeIn))

(defn handle-diff! [{:keys [gameid diff]}]
(when (= gameid (str (current-gameid app-state)))
(when (= gameid (current-gameid app-state))
(reset! game-state (differ/patch @last-state diff))
(check-lock?)
(reset! last-state @game-state)))
Expand All @@ -62,9 +61,9 @@

(defmethod ws/event-msg-handler :game/start [{data :?data}]
(reset! angel-arena/queueing false)
(launch-game! (parse-state data)))
(defmethod ws/event-msg-handler :game/resync [{data :?data}] (reset-game! (parse-state data)))
(defmethod ws/event-msg-handler :game/diff [{data :?data}] (handle-diff! (parse-state data)))
(launch-game! data))
(defmethod ws/event-msg-handler :game/resync [{data :?data}] (reset-game! data))
(defmethod ws/event-msg-handler :game/diff [{data :?data}] (handle-diff! data))
(defmethod ws/event-msg-handler :game/timeout [{data :?data}] (handle-timeout data))
(defmethod ws/event-msg-handler :game/error [_] (handle-error))

Expand All @@ -84,13 +83,6 @@
(when (not (:replay @game-state))
(ws/ws-send! [:game/mute-spectators {:gameid (current-gameid app-state)}])))

(defn stack-cards []
(swap! app-state update-in [:options :stacked-cards] not))

; (defn flip-runner-board []
; (let [layout (if (= "irl" (get-in @app-state [:options :runner-board-order])) "jnet" "irl")]
; (swap! app-state assoc-in [:options :runner-board-order] layout)))

(defn concede []
(when (not (:replay @game-state))
(ws/ws-send! [:game/concede {:gameid (current-gameid app-state)}])))
Expand All @@ -108,7 +100,8 @@
(build-report-url error)
"');\">Report on GitHub</button></div>")))

(defn ack-toast ([id] (send-command "toast" {:id id})))
(defn ack-toast [id]
(send-command "toast" {:id id}))

(defn toast
"Display a toast warning with the specified message.
Expand Down
Loading

0 comments on commit 54de01e

Please sign in to comment.