package ws import ( "git.reya.zone/reya/hexmap/server/action" "git.reya.zone/reya/hexmap/server/state" "go.uber.org/zap/zapcore" ) // ServerMessageType is an enum type for the server's messages. type ServerMessageType string // ServerCommand s are sent by the server to the client. type ServerCommand interface { zapcore.ObjectMarshaler // ToServerPB converts the command to a server protocol buffer which will be sent on the wire. ToServerPB() *ServerCommandPB } // ServerHello is the command sent to establish the current state of the server when a new client connects. type ServerHello struct { // Version is the protocol version the server is running. Version uint32 `json:"version"` // State is the complete state of the server as of when the client joined. State *state.Synced `json:"state"` } func (s ServerHello) MarshalLogObject(encoder zapcore.ObjectEncoder) error { encoder.AddString("type", "Hello") encoder.AddUint32("version", s.Version) return encoder.AddObject("state", s.State) } // ServerRefresh is the command sent to reestablish the current state of the server in response to ClientRefresh. type ServerRefresh struct { // State is the complete state of the server as of when the corresponding ClientRefresh was processed. State *state.Synced `json:"state"` } func (s ServerRefresh) MarshalLogObject(encoder zapcore.ObjectEncoder) error { encoder.AddString("type", "Refresh") return encoder.AddObject("state", s.State) } type IDSlice []uint32 func (i IDSlice) MarshalLogArray(encoder zapcore.ArrayEncoder) error { for _, v := range i { encoder.AppendUint32(v) } return nil } // ServerOK is the command sent when one or more client actions have been accepted and applied. type ServerOK struct { // IDs contains the IDs of the actions which were accepted and applied, in the order they were accepted and applied. // This is the same as the order they were received, though other actions may have been between these that were // rejected. IDs IDSlice `json:"ids"` } func (s ServerOK) MarshalLogObject(encoder zapcore.ObjectEncoder) error { encoder.AddString("type", "OK") return encoder.AddArray("ids", s.IDs) } // ServerFailed is the command sent when one or more client actions have been rejected. type ServerFailed struct { // IDs contains the IDs of the actions which were rejected, in the order they were rejected. // This is the same as the order they were received, though other actions may have been between these that were // accepted and applied. IDs IDSlice `json:"ids"` // Error contains the error text sent from the server about why these actions failed. Error string `json:"error"` } func (s ServerFailed) MarshalLogObject(encoder zapcore.ObjectEncoder) error { encoder.AddString("type", "Failed") err := encoder.AddArray("ids", s.IDs) encoder.AddString("error", s.Error) return err } // ServerAct is the command sent when one or more client actions from other clients have been accepted and applied. // The client's own actions will never be included in this command. type ServerAct struct { // Actions contains the actions that are now being applied. Actions action.ServerSlice `json:"actions"` } func (s ServerAct) MarshalLogObject(encoder zapcore.ObjectEncoder) error { encoder.AddString("type", "Act") return encoder.AddArray("actions", s.Actions) }