handle.clj

Branches > main > 083e2e7 > files / src / net / bowmap / network / handle.clj

(ns net.bowmap.network.handle
  (:require
   [net.bowmap.network.client :refer [send-packet!]]
   [net.bowmap.network.packet :as pk]
   [net.bowmap.network.packets.clientbound :as clientbound]
   [net.bowmap.network.packets.serverbound :as serverbound]
   [net.bowmap.network.state :as state]
   [net.bowmap.server.server :as server]))

(defmulti handle-packet
  (fn [client packet]
    (::pk/id packet)))

(defmethod handle-packet :default [client _]
  {:client client})

(defmethod handle-packet ::serverbound/handshaking [client pk]
  (let [{:keys! [intent protocol]} pk]
    {:client (assoc client
                    ::state/connection-state intent
                    :protocol-id protocol)}))

; TODO move to a fact and the game logic handler :)
(defmethod handle-packet ::serverbound/status-request [client _]
  {:client client
   :packets [{::pk/id ::clientbound/status-response
              ;; Should be un-bundled yeah.
              :response {:version {:name "26.2"
                                   :protocol 776}
                         :players {:online (count @server/players)
                                   :max server/max-player-count}
                         ; Is this a text component? Could make that interface
                         :description {:text "Bowmap!"}
                         :enforcesSecureChat false}}]})

(defn handle!! [client-ref packet]
  (let [{:keys [client packets]} (handle-packet @client-ref packet)]
    (dosync
      (ref-set client-ref client))
    (doseq [pk packets]
      (send-packet! client-ref pk))
    client))