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.
87 lines
2.6 KiB
87 lines
2.6 KiB
package websocket
|
|
|
|
import (
|
|
"git.reya.zone/reya/hexmap/server/action"
|
|
"go.uber.org/zap/zapcore"
|
|
)
|
|
|
|
// ClientCommandType is an enum type for the client's protocol messages.
|
|
type ClientCommandType string
|
|
|
|
// ClientCommand s are those sent by the client.
|
|
type ClientCommand interface {
|
|
zapcore.ObjectMarshaler
|
|
// ToClientPB converts the command to a client protocol buffer which will be sent on the wire.
|
|
ToClientPB() *ClientCommandPB
|
|
}
|
|
|
|
// ClientHello is the command sent by the client when it first establishes the connection.
|
|
type ClientHello struct {
|
|
// Version is the protocol version the client is running.
|
|
Version uint32 `json:"version"`
|
|
}
|
|
|
|
func (c ClientHello) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
|
|
encoder.AddString("type", "Hello")
|
|
encoder.AddUint32("version", c.Version)
|
|
return nil
|
|
}
|
|
|
|
// ClientRefresh is the command sent by the client when it needs the full state re-sent.
|
|
type ClientRefresh struct {
|
|
}
|
|
|
|
func (c ClientRefresh) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
|
|
encoder.AddString("type", "Refresh")
|
|
return nil
|
|
}
|
|
|
|
// IDed contains a pair of ID and Action, as sent by the client.
|
|
type IDed struct {
|
|
// ID contains the arbitrary ID that was sent by the client, for identifying the action in future messages.
|
|
ID uint32 `json:"id"`
|
|
// Action contains the action that was actually being sent.
|
|
Action action.Client `json:"action"`
|
|
}
|
|
|
|
func (i IDed) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
|
|
encoder.AddUint32("id", i.ID)
|
|
return encoder.AddObject("action", i.Action)
|
|
}
|
|
|
|
type IDPairs []IDed
|
|
|
|
func (a IDPairs) MarshalLogArray(encoder zapcore.ArrayEncoder) error {
|
|
var finalErr error = nil
|
|
for _, v := range a {
|
|
err := encoder.AppendObject(v)
|
|
if err != nil && finalErr == nil {
|
|
finalErr = err
|
|
}
|
|
}
|
|
return finalErr
|
|
}
|
|
|
|
// ClientAct is a command sent in order to deliver one or more Action actions to the server.
|
|
type ClientAct struct {
|
|
// Actions contains the actions the client wants to apply, in the order they should be applied.
|
|
Actions IDPairs `json:"actions"`
|
|
}
|
|
|
|
func (c ClientAct) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
|
|
encoder.AddString("type", "Act")
|
|
return encoder.AddArray("actions", c.Actions)
|
|
}
|
|
|
|
// ClientMalformed is synthesized by the reader when it has read a command that does not appear to match the
|
|
// protocol.
|
|
type ClientMalformed struct {
|
|
// Error is the error in parse that caused the reader to be unable to read the message.
|
|
Error error
|
|
}
|
|
|
|
func (c ClientMalformed) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
|
|
encoder.AddString("type", "(malformed command)")
|
|
encoder.AddString("error", c.Error.Error())
|
|
return nil
|
|
}
|
|
|