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.
75 lines
2.1 KiB
75 lines
2.1 KiB
3 years ago
|
package ws
|
||
3 years ago
|
|
||
|
import (
|
||
3 years ago
|
"git.reya.zone/reya/hexmap/server/action"
|
||
3 years ago
|
"go.uber.org/zap/zapcore"
|
||
|
)
|
||
|
|
||
3 years ago
|
// ClientCommandType is an enum type for the client's protocol messages.
|
||
|
type ClientCommandType string
|
||
3 years ago
|
|
||
3 years ago
|
// ClientCommand s are those sent by the client.
|
||
|
type ClientCommand interface {
|
||
3 years ago
|
zapcore.ObjectMarshaler
|
||
3 years ago
|
// ToClientPB converts the command to a client protocol buffer which will be sent on the wire.
|
||
|
ToClientPB() *ClientCommandPB
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
// ClientHello is the command sent by the client when it first establishes the connection.
|
||
3 years ago
|
type ClientHello struct {
|
||
|
// Version is the protocol version the client is running.
|
||
3 years ago
|
Version uint32 `json:"version"`
|
||
3 years ago
|
}
|
||
|
|
||
|
func (c ClientHello) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
|
||
3 years ago
|
encoder.AddString("type", "Hello")
|
||
|
encoder.AddUint32("version", c.Version)
|
||
3 years ago
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// ClientRefresh is the command sent by the client when it needs the full state re-sent.
|
||
3 years ago
|
type ClientRefresh struct {
|
||
|
}
|
||
|
|
||
|
func (c ClientRefresh) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
|
||
3 years ago
|
encoder.AddString("type", "Refresh")
|
||
3 years ago
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
type IDPairs []action.IDed
|
||
3 years ago
|
|
||
|
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
|
||
|
}
|
||
|
|
||
3 years ago
|
// ClientAct is a command sent in order to deliver one or more Action actions to the server.
|
||
3 years ago
|
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 {
|
||
3 years ago
|
encoder.AddString("type", "Act")
|
||
3 years ago
|
return encoder.AddArray("actions", c.Actions)
|
||
|
}
|
||
3 years ago
|
|
||
|
// 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 {
|
||
3 years ago
|
encoder.AddString("type", "(malformed command)")
|
||
3 years ago
|
encoder.AddString("error", c.Error.Error())
|
||
|
return nil
|
||
|
}
|