You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hexmap/server/websocket/server.go

123 lines
4.0 KiB

package websocket
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
const (
ServerHelloType ServerMessageType = "HELLO"
ServerRefreshType ServerMessageType = "REFRESH"
ServerOKType ServerMessageType = "OK"
ServerFailedType ServerMessageType = "FAILED"
ServerActType ServerMessageType = "ACT"
ServerGoodbyeType ServerMessageType = GoodbyeType
)
// ServerCommand s are sent by the server to the client.
type ServerCommand interface {
zapcore.ObjectMarshaler
// ServerType returns the type constant that will be sent on the wire.
ServerType() ServerMessageType
}
// 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 int `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", string(ServerHelloType))
encoder.AddInt("version", s.Version)
return encoder.AddObject("state", s.State)
}
func (s ServerHello) ServerType() ServerMessageType {
return ServerHelloType
}
// 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", string(ServerRefreshType))
return encoder.AddObject("state", s.State)
}
func (s ServerRefresh) ServerType() ServerMessageType {
return ServerRefreshType
}
type IDSlice []int
func (i IDSlice) MarshalLogArray(encoder zapcore.ArrayEncoder) error {
for _, v := range i {
encoder.AppendInt(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", string(ServerOKType))
return encoder.AddArray("ids", s.IDs)
}
func (s ServerOK) ServerType() ServerMessageType {
return ServerOKType
}
// 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", string(ServerFailedType))
err := encoder.AddArray("ids", s.IDs)
encoder.AddString("error", s.Error)
return err
}
func (s ServerFailed) ServerType() ServerMessageType {
return ServerFailedType
}
// 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.Slice `json:"actions"`
}
func (s ServerAct) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("type", string(ServerActType))
return encoder.AddArray("actions", s.Actions)
}
func (s ServerAct) ServerType() ServerMessageType {
return ServerActType
}