package websocket import ( "git.reya.zone/reya/hexmap/server/action" "go.uber.org/zap/zapcore" ) // ClientMessageType is an enum type for the client's protocol messages. type ClientMessageType string const ( ClientHelloType ClientMessageType = "HELLO" ClientRefreshType ClientMessageType = "REFRESH" ClientActType ClientMessageType = "ACT" ClientGoodbyeType ClientMessageType = GoodbyeType ) // ClientMessage s are those sent by the client. type ClientMessage interface { zapcore.ObjectMarshaler // ClientType gives the type constant that will be sent on or read from the wire. ClientType() ClientMessageType } // ClientHello is the action sent by the client when it first establishes the connection. type ClientHello struct { // Version is the protocol version the client is running. Version int `json:"version"` } func (c ClientHello) ClientType() ClientMessageType { return ClientHelloType } func (c ClientHello) MarshalLogObject(encoder zapcore.ObjectEncoder) error { encoder.AddString("type", string(ClientHelloType)) encoder.AddInt("version", c.Version) return nil } // ClientRefresh is the action sent by the client when it needs the full state re-sent. type ClientRefresh struct { } func (c ClientRefresh) ClientType() ClientMessageType { return ClientRefreshType } func (c ClientRefresh) MarshalLogObject(encoder zapcore.ObjectEncoder) error { encoder.AddString("type", string(ClientRefreshType)) 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 int `json:"id"` // Action contains the action that was actually being sent. Action action.Syncable `json:"action"` } func (i IDed) MarshalLogObject(encoder zapcore.ObjectEncoder) error { encoder.AddInt("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 an action sent in order to deliver one or more Syncable 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) ClientType() ClientMessageType { return ClientActType } func (c ClientAct) MarshalLogObject(encoder zapcore.ObjectEncoder) error { encoder.AddString("type", string(ClientActType)) return encoder.AddArray("actions", c.Actions) }