e32a8c9
Branches > main > e32a8c9 > files / src
Rename facts to events
Commit: e32a8c93c62c7a6d436de7ff88d30f4fca648dfc
Parent: 239bccb
6 changed files:
src/net/bowmap/protocol/handle.clj view file
@@ -13,7 +13,7 @@ [net.bowmap.server.player :as player] [net.bowmap.server.position :as position] [net.bowmap.server.server :as server]- [net.bowmap.stormbow.fact :as fact]))+ [net.bowmap.stormbow.event :as event])) (defmulti handle-packet (fn [client packet]@@ -29,7 +29,7 @@ :protocol-id protocol)})) (defmethod handle-packet ::serverbound/status-request [_ _]- {:facts [{::fact/id ::fact/server-list-requested}]})+ {:events [{::event/id ::event/server-list-requested}]}) (defmethod handle-packet ::serverbound/ping-request [_ pk] {:packets [{::pk/id ::clientbound/pong-response@@ -95,7 +95,7 @@ player {::entity/id entity-id ::player/uuid uuid}] {:client client- :facts [{::fact/id ::fact/player-joined+ :events [{::event/id ::event/player-joined ::player/player player}] :packets (-> [{::pk/id ::clientbound/login :entity-id entity-id@@ -139,7 +139,7 @@ {:keys [& ::position/x ::position/y ::position/z ::position/yaw ::position/pitch] :as packet :select movement}]- {:facts [(merge {::fact/id ::fact/updated-player-movement+ {:events [(merge {::event/id ::event/updated-player-movement ::player/uuid uuid ::position/position movement})]}) src/net/bowmap/protocol/server.clj view file
@@ -1,6 +1,6 @@ (ns net.bowmap.protocol.server (:require- [clojure.core.async :refer [<!! >!! alt!! chan io-thread put! timeout]]+ [clojure.core.async :refer [<!! >!! alt!! io-thread put!]] [net.bowmap.protocol.client :as client] [net.bowmap.protocol.handle :as handle] [net.bowmap.protocol.keepalive :as keepalive]@@ -12,8 +12,8 @@ [net.bowmap.transport.connection :as connection] [net.bowmap.transport.server :as mc])) -(defn adorn-effects-with-client [client fact-seq]- (mapv #(assoc % ::client/client client) fact-seq))+(defn adorn-effects-with-client [client events]+ (mapv #(assoc % ::client/client client) events)) (defn handle-connection [connection] (io-thread@@ -26,12 +26,12 @@ serverbound-frames ([frame] (when (some? frame) (let [packet (decode-packet! frame (::state/connection-state client))- {:keys [client packets facts keep-alive?] :or {:client client}} (handle/handle-packet client packet)]+ {:keys [client packets events keep-alive?] :or {:client client}} (handle/handle-packet client packet)] ; TODO probably want to clean up this side effecting stuff (doseq [pk packets] (put! clientbound-packets pk))- (doseq [fact facts]- (execute-effects! (update (logic/apply-rules fact) :effects #(adorn-effects-with-client client %))))+ (doseq [event events]+ (execute-effects! (adorn-effects-with-client client (logic/handle-events event)))) (when keep-alive? (keepalive/keep-alive! client)) (recur client))))@@ -41,7 +41,7 @@ (recur client)) (throw (Exception. "Clientbound packets was closed idk why")))))) (finally- ; TODO Send player disconnected fact, and update the player list from that+ ; TODO Send player disconnected event, and update the player list from that (close-fn)))))) (defn start-server! [port]src/net/bowmap/server/logic.clj view file
@@ -4,29 +4,27 @@ [net.bowmap.server.position :as position] [net.bowmap.server.server :as server] [net.bowmap.stormbow.effect :as effect]- [net.bowmap.stormbow.fact :as fact]))+ [net.bowmap.stormbow.event :as event])) -; 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)))+(defmulti handle-event+ "Returns a sequence of effects and events yielded from an event."+ (fn [event]+ (::event/id event))) -(defmethod handle-fact ::fact/server-list-requested [_]+(defmethod handle-event ::event/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]}]+(defmethod handle-event ::event/player-joined [{::player/keys! [player]}] (dosync (let [{::player/keys! [uuid]} player] (if (@server/players uuid)@@ -34,37 +32,28 @@ (commute server/players assoc uuid (ref player))) []))) -(defmethod handle-fact ::fact/player-left [{:keys! [::player/uuid]}]+(defmethod handle-event ::event/player-left [{:keys! [::player/uuid]}] (dosync- {::fact/id ::fact/playing- ::fact/mode ::fact/retract+ {::event/id ::event/playing+ ::event/mode ::event/retract ::player/uuid uuid} (commute server/players dissoc uuid) [])) -(defmethod handle-fact ::fact/updated-player-movement [{:keys! [::player/uuid ::position/position]}]+(defmethod handle-event ::event/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 [_ _])+(defmethod handle-event ::event/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}))+(defn handle-events+ "Handles the supplied event, and all events returned recursively from this one.+ Returns a seq of all effects that are yielded from handling all events recursively."+ [event]+ (reduce (fn [effects result]+ (if (::event/id result)+ (into effects (handle-events event))+ (conj effects result)))+ [] (handle-event event)))src/net/bowmap/stormbow/effect_execution.clj view file
@@ -17,7 +17,5 @@ :response server-list-response}] (put! clientbound-packets packet))) -; TODO this now also has :facts [...]-; So you can update state based on that :) (defn execute-effects! [effects]- (run! execute-effect! (:effects effects)))+ (run! execute-effect! effects))src/net/bowmap/stormbow/event.clj view file
@@ -0,0 +1,6 @@+(ns net.bowmap.stormbow.event+ (:require+ [clojure.spec.alpha :as s]))++(s/def ::event (s/keys :opt [::id]))+(s/def ::id qualified-keyword?)src/net/bowmap/stormbow/fact.clj view file
@@ -1,6 +0,0 @@-(ns net.bowmap.stormbow.fact- (:require- [clojure.spec.alpha :as s]))--(s/def ::fact (s/keys :opt [::id]))-(s/def ::id qualified-keyword?) ; Could use a set of all facts