Add type constants to action.Syncable.

main
Mari 3 years ago
parent ef9e4c9da1
commit 072b410a64
  1. 9
      server/action/map.go
  2. 15
      server/action/syncable.go
  3. 11
      server/action/user.go

@ -5,6 +5,10 @@ import (
"hexmap-server/state"
)
const (
CellColorType SyncableType = "CELL_COLOR"
)
// CellColor is the action sent when a cell of the map has been colored a different color.
type CellColor struct {
// At is the location of the cell in storage coordinates.
@ -14,11 +18,16 @@ type CellColor struct {
}
func (c CellColor) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("type", string(CellColorType))
err := encoder.AddObject("at", c.At)
encoder.AddString("color", c.Color.String())
return err
}
func (c CellColor) Type() SyncableType {
return CellColorType
}
// Apply sets the target cell's color, or returns ErrNoOp if it can't.
func (c CellColor) Apply(s *state.Synced) error {
if c.Color.A < 0xF {

@ -6,11 +6,21 @@ import (
"hexmap-server/state"
)
var ErrNoOp error = errors.New("action's effects were already applied, or it's an empty action")
var (
// ErrNoOp is returned when an action has no effect.
ErrNoOp = errors.New("action's effects were already applied, or it's an empty action")
// ErrNoTransparentColors is returned when a user tries to set their active color or a cell color to transparent.
// Transparent here is defined as having an alpha component of less than 15 (0xF).
ErrNoTransparentColors = errors.New("transparent colors not allowed")
)
type SyncableType string
// Syncable is the interface for action that can be shared.
// Syncable is the interface for actions that can be shared between clients.
type Syncable interface {
zapcore.ObjectMarshaler
// Type gives the Javascript type that is sent over the wire.
Type() SyncableType
// Apply causes the action's effects to be applied to s, mutating it in place.
// All syncable.Actions must conform to the standard that if an action can't be correctly applied, or if it would
// have no effect, it returns an error without changing s.
@ -18,3 +28,4 @@ type Syncable interface {
// If an action is correctly applied and has an effect, it should return nil.
Apply(s *state.Synced) error
}
type SyncableProducer func()

@ -1,14 +1,13 @@
package action
import (
"errors"
"go.uber.org/zap/zapcore"
"hexmap-server/state"
)
// ErrNoTransparentColors is returned when a user tries to set their active color or a cell color to transparent.
// Transparent here is defined as having an alpha component of less than 15 (0xF).
var ErrNoTransparentColors error = errors.New("transparent colors not allowed")
const (
UserActiveColorType = "USER_ACTIVE_COLOR"
)
// UserActiveColor is the action sent when the user's current color, the one being painted with, changes.
type UserActiveColor struct {
@ -16,6 +15,10 @@ type UserActiveColor struct {
Color state.HexColor `json:"color"`
}
func (c UserActiveColor) Type() SyncableType {
return UserActiveColorType
}
func (c UserActiveColor) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
encoder.AddString("color", c.Color.String())
return nil

Loading…
Cancel
Save