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.
147 lines
4.4 KiB
147 lines
4.4 KiB
package room
|
|
|
|
import (
|
|
"git.reya.zone/reya/hexmap/server/action"
|
|
"git.reya.zone/reya/hexmap/server/state"
|
|
"github.com/rs/xid"
|
|
"go.uber.org/zap/zapcore"
|
|
)
|
|
|
|
// Message marks messages going to clients from the room.
|
|
type Message interface {
|
|
zapcore.ObjectMarshaler
|
|
// RoomID marks the ID of the room this Message originated from, in case the Client has a shared IncomingChannel.
|
|
RoomID() xid.ID
|
|
}
|
|
|
|
// JoinResponse is the message sent by the room on a new client's IncomingChannel after it joins.
|
|
type JoinResponse struct {
|
|
id xid.ID
|
|
// currentState is a pointer to a copy of the room's current state.
|
|
// If the client refused the room state in its join message, this will be a nil pointer instead.
|
|
currentState *state.Synced
|
|
}
|
|
|
|
func (j JoinResponse) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
|
|
encoder.AddString("type", "JoinResponse")
|
|
encoder.AddString("id", j.id.String())
|
|
return encoder.AddObject("currentState", j.currentState)
|
|
}
|
|
|
|
func (j JoinResponse) RoomID() xid.ID {
|
|
return j.id
|
|
}
|
|
|
|
// CurrentState returns the state of the room as of when the JoinRequest was processed.
|
|
func (j JoinResponse) CurrentState() *state.Synced {
|
|
return j.currentState
|
|
}
|
|
|
|
// RefreshResponse is the message sent by the room after a client requests it, or immediately on join.
|
|
type RefreshResponse struct {
|
|
id xid.ID
|
|
// currentState is a pointer to a copy of the room's current state.
|
|
currentState *state.Synced
|
|
}
|
|
|
|
func (r RefreshResponse) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
|
|
encoder.AddString("type", "JoinResponse")
|
|
encoder.AddString("id", r.id.String())
|
|
return encoder.AddObject("currentState", r.currentState)
|
|
}
|
|
|
|
func (r RefreshResponse) RoomID() xid.ID {
|
|
return r.id
|
|
}
|
|
|
|
// CurrentState returns the state of the room as of when the RefreshRequest was processed.
|
|
func (r RefreshResponse) CurrentState() *state.Synced {
|
|
return r.currentState
|
|
}
|
|
|
|
// ApplyResponse returns the result of an action to _only_ the one that sent the ApplyRequest.
|
|
type ApplyResponse struct {
|
|
id xid.ID
|
|
// actionID is the ID of the action that completed or failed.
|
|
actionID int
|
|
// result is nil if the action was completed, or an error if it failed.
|
|
result error
|
|
}
|
|
|
|
func (a ApplyResponse) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
|
|
encoder.AddString("type", "ApplyResponse")
|
|
encoder.AddString("id", a.id.String())
|
|
encoder.AddInt("actionId", a.actionID)
|
|
encoder.AddBool("success", a.result == nil)
|
|
if a.result != nil {
|
|
encoder.AddString("failure", a.result.Error())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a ApplyResponse) RoomID() xid.ID {
|
|
return a.id
|
|
}
|
|
|
|
// Success returns true if the action succeeded, false if it failed.
|
|
func (a ApplyResponse) Success() bool {
|
|
return a.result == nil
|
|
}
|
|
|
|
// Failure returns the error if the action failed, or nil if it succeeded.
|
|
func (a ApplyResponse) Failure() error {
|
|
return a.result
|
|
}
|
|
|
|
// ActionBroadcast is sent to all clients _other_ than the one that sent the ApplyRequest when an action succeeds.
|
|
type ActionBroadcast struct {
|
|
id xid.ID
|
|
// originalClientID is the client that sent the action in the first place.
|
|
originalClientID xid.ID
|
|
// originalActionID is the ID that the client that sent the action sent.
|
|
originalActionID int
|
|
// action is the action that succeeded.
|
|
action action.Action
|
|
}
|
|
|
|
func (a ActionBroadcast) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
|
|
encoder.AddString("type", "ActionBroadcast")
|
|
encoder.AddString("id", a.id.String())
|
|
encoder.AddString("originalClientId", a.originalClientID.String())
|
|
encoder.AddInt("originalActionId", a.originalActionID)
|
|
return encoder.AddObject("action", a.action)
|
|
}
|
|
|
|
func (a ActionBroadcast) RoomID() xid.ID {
|
|
return a.id
|
|
}
|
|
|
|
// LeaveResponse is the message sent by the room when it has accepted that a client has left, and will send it no further messages.
|
|
type LeaveResponse struct {
|
|
id xid.ID
|
|
}
|
|
|
|
func (l LeaveResponse) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
|
|
encoder.AddString("type", "LeaveResponse")
|
|
encoder.AddString("id", l.id.String())
|
|
return nil
|
|
}
|
|
|
|
func (l LeaveResponse) RoomID() xid.ID {
|
|
return l.id
|
|
}
|
|
|
|
// ShutdownRequest is the message sent by the room when something causes it to shut down. It will send the client no further messages except a possible LeaveResponse.
|
|
type ShutdownRequest struct {
|
|
id xid.ID
|
|
}
|
|
|
|
func (s ShutdownRequest) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
|
|
encoder.AddString("type", "ShutdownRequest")
|
|
encoder.AddString("id", s.id.String())
|
|
return nil
|
|
}
|
|
|
|
func (s ShutdownRequest) RoomID() xid.ID {
|
|
return s.id
|
|
}
|
|
|