effect_execution.clj

Branches > facts > latest > files / src / net / bowmap / stormbow / effect_execution.clj

(ns net.bowmap.stormbow.effect-execution
  (:require
   [clojure.core.async :refer [put!]]
   [net.bowmap.protocol.client :as client]
   [net.bowmap.protocol.packet :as pk]
   [net.bowmap.protocol.packet.clientbound :as clientbound]
   [net.bowmap.stormbow.effect :as effect]
   [net.bowmap.stormbow.fact :as fact]))

(defmulti execute-effect!
  (fn [effect]
    (::effect/id effect)))

(defmethod execute-effect! ::effect/send-server-list [effect]
  (let [{:keys! [::client/client server-list-response]} effect
        {::client/keys! [clientbound-packets]} client
        packet {::pk/id ::clientbound/status-response
                :response server-list-response}]
    (put! clientbound-packets packet)))

(defn execute-effects! [{:keys! [facts effects]}]
  (doseq [{::fact/keys [mode attribute entity value]} facts]
    (when (fact/fact attribute)
      (if (= mode ::fact/retract)
        (fact/retract-fact attribute entity)
        (fact/assert-fact attribute entity value))))
  (run! execute-effect! effects))