8d1b0e2
Branches > main > 8d1b0e2 > files
Add chat
Commit: 8d1b0e299189300484e83d1206e12bb08b392738
Parent: 4c49c77
5 changed files:
src/net/bowmap/protocol/handle.clj view file
@@ -11,7 +11,8 @@ [net.bowmap.server.ping :as ping] [net.bowmap.server.player :as player] [net.bowmap.server.server :as server]- [net.bowmap.stormbow.event :as event]))+ [net.bowmap.stormbow.event :as event]+ [net.bowmap.transport.connection :as connection])) ; Could eventually just be a seq of both packets and events if that would be nicer to read and write. (defmulti interpret-packet@@ -79,10 +80,11 @@ :chunk-x x :chunk-z z)))) -(defmethod interpret-packet ::serverbound/finish-config [{:keys! [::player/uuid]}]+(defmethod interpret-packet ::serverbound/finish-config [{:keys! [::player/uuid ::connection/connection]}] (let [entity-id (entity/next-entity-id)] {:events [{::event/id ::event/player-joined ::player/uuid uuid+ ::connection/connection connection ::entity/id entity-id}] :packets (-> [{::pk/id ::clientbound/login :entity-id entity-id@@ -139,3 +141,8 @@ ; ;(defmethod interpret-packet ::serverbound/set-player-movement-flags [client pk] ; (handle-player-update-movement-packet client pk))++(defmethod interpret-packet ::serverbound/chat-message [{:keys! [message ::player/uuid]}]+ {:events [{::event/id ::event/player-sent-chat-message+ ::player/uuid uuid+ :message message}]})src/net/bowmap/server/logic.clj view file
@@ -3,7 +3,9 @@ [net.bowmap.server.player :as player] [net.bowmap.server.position :as position] [net.bowmap.server.server :as server]- [net.bowmap.stormbow.event :as event]))+ [net.bowmap.stormbow.effect :as effect]+ [net.bowmap.stormbow.event :as event]+ [net.bowmap.transport.connection :as connection])) (defmulti handle-event "Returns a sequence of effects and events yielded from an event."@@ -11,11 +13,12 @@ (::event/id event))) ; Want this to be pure, which means you need a way to encode that state change as data?-(defmethod handle-event ::event/player-joined [{::player/keys! [uuid]}]+(defmethod handle-event ::event/player-joined [{:keys! [::player/uuid ::connection/connection]}] (dosync (if (@server/players uuid) (throw (Exception. "Player tried to join with an existing UUID"))- (commute server/players assoc uuid {::player/uuid uuid}))+ (commute server/players assoc uuid {::player/uuid uuid+ ::connection/connection connection})) [])) (defmethod handle-event ::event/player-left [{:keys! [::player/uuid]}]@@ -29,7 +32,13 @@ [])) ; Called when the player actually moves, not just when a position is sent-(defmethod handle-event ::event/player-moved [_ _])+(defmethod handle-event ::event/player-moved [_])++(defmethod handle-event ::event/player-sent-chat-message [{:keys! [message]}]+ (for [[uuid] @server/players]+ {::effect/id ::effect/send-chat-message+ ::player/uuid uuid+ :message {:text message}})) (defn handle-events "Handles the supplied event, and all events returned recursively from this one.src/net/bowmap/server/text.clj view file
@@ -0,0 +1,7 @@+(ns net.bowmap.server.text+ (:require+ [clojure.spec.alpha :as s]))++(s/def ::text)++[{::text "Foo"}]src/net/bowmap/stormbow/effect_execution.clj view file
@@ -1,10 +1,22 @@ (ns net.bowmap.stormbow.effect-execution (:require- [net.bowmap.stormbow.effect :as effect]))+ [clojure.core.async :refer [put!]]+ [net.bowmap.protocol.packet :as pk]+ [net.bowmap.protocol.packet.clientbound :as clientbound]+ [net.bowmap.server.player :as player]+ [net.bowmap.server.server :as server]+ [net.bowmap.stormbow.effect :as effect]+ [net.bowmap.transport.connection :as connection])) (defmulti execute-effect! (fn [effect] (::effect/id effect))) +(defmethod execute-effect! ::effect/send-chat-message [{::player/keys! [uuid] :keys! [message]}]+ (let [{::connection/keys! [connection]} (@server/players uuid)+ {::connection/keys! [clientbound-packets]} connection]+ (put! clientbound-packets {::pk/id ::clientbound/system-chat-message+ :message message})))+ (defn execute-effects! [effects] (run! execute-effect! effects))src/net/bowmap/transport/connection.clj view file
@@ -14,7 +14,7 @@ [java.net Socket SocketException])) (defn handle-serverbound-frame!- [state frame]+ [state conn frame] (let [{::phase/keys! [phase]} @state {::pk/keys! [id] :as packet} (decode-packet! frame phase)] (case id@@ -25,7 +25,8 @@ (let [{:keys! [username uuid]} packet] (swap! state assoc ::player/username username ::player/uuid uuid)) nil)- (merge packet (select-keys @state [::player/uuid]))))+ ; Just kinda crudely passing the connection into the serverbound packet right now+ (merge packet {::connection conn} (select-keys @state [::player/uuid])))) (defn connection "Creates a connection object from a java.net.Socket@@ -60,7 +61,7 @@ (io-thread (try (loop []- (put! serverbound-packets (handle-serverbound-frame! state (streams/read-frame!! input-stream)))+ (put! serverbound-packets (handle-serverbound-frame! state conn (streams/read-frame!! input-stream))) (recur)) (catch EOFException _) (catch SocketException _)