client.clj

Bowmap · bowmap.git · Tickets

Branches > main > e860102 > files / src / net / bowmap / server / protocol / client.clj

(ns net.bowmap.server.protocol.client
  (:require
   [medley.core :refer [assoc-some]]
   [net.bowmap.server.protocol.packet :as packet]
   [net.bowmap.server.protocol.packets :as packets]
   [net.bowmap.server.protocol.phase :as phase]
   [net.bowmap.server.protocol.serverbound-packets :as serverbound]))

(defn phase-transition
  "Returns the phase a client should take on after parsing 'packet', or nil."
  [packet]
  (case (req! packet ::packet/id)
    ::serverbound/handshaking (req! packet ::phase/phase)
    ::serverbound/login-acknowledged ::phase/config
    ::serverbound/finish-config ::phase/play
    nil))

(defn with-state-transition
  "Updates the client with any state transitions caused by 'packet'.
   Serverbound packets may:
    1. Update the connection phase
    2. Update encryption state
    3. Update compression state"
  [client packet]
  (assoc-some client ::phase/phase (phase-transition packet)))

(defn apply-serverbound-frame
  "Decodes 'frame' using the given client state, and returns [client' packet].
   Decoding a frame can update a client's connection phase, encryption state,
   and compression state"
  [client frame]
  (let [packet (packets/decode-frame client frame)]
    [(with-state-transition client packet) packet]))