logic.clj
Branches > main > 239bccb > files / src / net / bowmap / server / logic.clj
(ns net.bowmap.server.logic
(:require
[net.bowmap.server.player :as player]
[net.bowmap.server.position :as position]
[net.bowmap.server.server :as server]
[net.bowmap.stormbow.effect :as effect]
[net.bowmap.stormbow.fact :as fact]))
; TODO should also be able to return new facts :)
(defmulti handle-fact
"Returns a sequence of effects from a fact."
(fn [fact]
(::fact/id fact)))
(defmethod handle-fact ::fact/server-list-requested [_]
(dosync
[{::effect/id ::effect/send-server-list
:server-list-response {:version {:name "26.2"
:protocol 776}
; Gets the number of players playing, which is what you want
; And there are no side-effects!
#_(count (fact ::fact/playing))
:players {:online (count @server/players)
:max server/max-player-count}
:description {:text "Bowmap!"}
:enforcesSecureChat false}}]))
; Want this to be pure, which means you need a way to encode that state change as data?
(defmethod handle-fact ::fact/player-joined [{::player/keys! [player]}]
(dosync
(let [{::player/keys! [uuid]} player]
(if (@server/players uuid)
(throw (Exception. "Player tried to join with an existing UUID"))
(commute server/players assoc uuid (ref player)))
[])))
(defmethod handle-fact ::fact/player-left [{:keys! [::player/uuid]}]
(dosync
{::fact/id ::fact/playing
::fact/mode ::fact/retract
::player/uuid uuid}
(commute server/players dissoc uuid)
[]))
(defmethod handle-fact ::fact/updated-player-movement [{:keys! [::player/uuid ::position/position]}]
(dosync
(alter (@server/players uuid) update ::position/position merge position)
[]))
; Called when the player actually moves, not just when a position is sent
(defmethod handle-fact ::fact/player-moved [_ _])
(defn fact? [x]
(::fact/id x))
(defn apply-rules
"Applies rules recursively, returns a map containing :facts and :effects, the seqs of all facts that were asserted/retracted, and the seq of all effects that are implied from that fact."
([fact] (apply-rules {:facts [fact] :effects []} fact))
([acc fact]
(let [results (handle-fact fact)]
(reduce (fn [acc result]
(if (fact? result)
(apply-rules (update acc :facts conj result) result)
(update acc :effects conj result)))
acc results))))
(comment
(apply-rules {:net.bowmap.stormbow.fact/id :net.bowmap.stormbow.fact/server-list-requested})
(apply-rules
{::fact/id ::fact/server-list-requested}))