logic.clj

Branches > main > 8c25299 > files / src / net / bowmap / server / logic.clj

(ns net.bowmap.server.logic
  (:require
   [net.bowmap.network.client :as client]
   [net.bowmap.server.entity :as entity]
   [net.bowmap.server.player :as player]
   [net.bowmap.server.server :as server]
   [net.bowmap.stormbow.fact :as fact]
   [net.bowmap.stormbow.response :as response]))

(defmulti handler
  (fn [fact]
    (::fact/id fact)))

(defmethod handler ::fact/server-list-requested [{:keys! [::client/client]}]
  [{::response/id ::response/send-server-list
    ::client/client client
    :server-list-response {:version {:name "26.2"
                                     :protocol 776}
                           :players {:online (count @server/players)
                                     :max server/max-player-count}
                           :description {:text "Bowmap!"}
                           :enforcesSecureChat false}}])

::entity/uuid ::player/player

; TODO start keepalive
(defmethod handler ::fact/player-joined [{:keys! [::player/player]}]
  (let [{:keys! [::entity/uuid]} player]
    (if (@server/players uuid)
      (throw (Exception. "Player tried to join with an existing UUID"))
      (commute server/players assoc uuid player))
    []))

; Returns a seq of responses
; And document that this is not called synchronously (hence the transactions as well)
(defn handle-fact [fact]
  ; TODO I think this is fine because it has to be run within a transaction anyway?
  ; All though I suppose not all fact handling needs to be in a transaction some is trivial so idk.
  (dosync (handler fact)))