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.
50 lines
1.6 KiB
50 lines
1.6 KiB
3 years ago
|
package action
|
||
3 years ago
|
|
||
|
import (
|
||
|
"errors"
|
||
3 years ago
|
"git.reya.zone/reya/hexmap/server/state"
|
||
3 years ago
|
"go.uber.org/zap/zapcore"
|
||
3 years ago
|
)
|
||
|
|
||
3 years ago
|
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")
|
||
|
)
|
||
|
|
||
3 years ago
|
type Type string
|
||
3 years ago
|
|
||
3 years ago
|
// Action is the interface for actions that can be shared between clients, or between the server and a client.
|
||
|
type Action interface {
|
||
3 years ago
|
zapcore.ObjectMarshaler
|
||
3 years ago
|
// Type gives the Javascript type that is sent over the wire.
|
||
3 years ago
|
Type() Type
|
||
3 years ago
|
// Apply causes the action's effects to be applied to s, mutating it in place.
|
||
3 years ago
|
// All Actions must conform to the standard that if an action can't be correctly applied, or if it would
|
||
3 years ago
|
// have no effect, it returns an error without changing s.
|
||
3 years ago
|
// If an action can be correctly applied but would have no effect, it should return ErrNoOp.
|
||
3 years ago
|
// If an action is correctly applied and has an effect, it should return nil.
|
||
|
Apply(s *state.Synced) error
|
||
3 years ago
|
// fromJSONMap causes the action's state to be overwritten by data from the given map.
|
||
|
fromJSONMap(data map[string] interface{}) error
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
3 years ago
|
type parseAction struct {
|
||
|
Type string `json:"type"`
|
||
|
}
|
||
|
|
||
|
type Slice []Action
|
||
3 years ago
|
|
||
3 years ago
|
func (s Slice) MarshalLogArray(encoder zapcore.ArrayEncoder) error {
|
||
3 years ago
|
var finalErr error = nil
|
||
|
for _, a := range s {
|
||
|
err := encoder.AppendObject(a)
|
||
|
if err != nil && finalErr == nil {
|
||
|
finalErr = err
|
||
|
}
|
||
|
}
|
||
|
return finalErr
|
||
|
}
|