|
|
|
@ -6,8 +6,6 @@ import ( |
|
|
|
|
"hexmap-server/state" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// TODO: Make all the ServerMessages implement ServerMessage.
|
|
|
|
|
|
|
|
|
|
// ServerMessageType is an enum type for the server's messages.
|
|
|
|
|
type ServerMessageType string |
|
|
|
|
|
|
|
|
@ -32,13 +30,41 @@ type ServerHello struct { |
|
|
|
|
// Version is the protocol version the server is running.
|
|
|
|
|
Version int `json:"version"` |
|
|
|
|
// State is the complete state of the server as of when the client joined.
|
|
|
|
|
State state.Synced `json:"state"` |
|
|
|
|
State *state.Synced `json:"state"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s ServerHello) MarshalLogObject(encoder zapcore.ObjectEncoder) error { |
|
|
|
|
encoder.AddString("type", string(ServerHelloType)) |
|
|
|
|
encoder.AddInt("version", s.Version) |
|
|
|
|
return encoder.AddObject("state", s.State) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s ServerHello) ServerType() ServerMessageType { |
|
|
|
|
return ServerHelloType |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ServerRefresh is the action sent to reestablish the current state of the server in response to ClientRefresh.
|
|
|
|
|
type ServerRefresh struct { |
|
|
|
|
// State is the complete state of the server as of when the corresponding ClientRefresh was processed.
|
|
|
|
|
State state.Synced `json:"state"` |
|
|
|
|
State *state.Synced `json:"state"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s ServerRefresh) MarshalLogObject(encoder zapcore.ObjectEncoder) error { |
|
|
|
|
encoder.AddString("type", string(ServerRefreshType)) |
|
|
|
|
return encoder.AddObject("state", s.State) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s ServerRefresh) ServerType() ServerMessageType { |
|
|
|
|
return ServerRefreshType |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type IDSlice []int |
|
|
|
|
|
|
|
|
|
func (i IDSlice) MarshalLogArray(encoder zapcore.ArrayEncoder) error { |
|
|
|
|
for _, v := range i { |
|
|
|
|
encoder.AppendInt(v) |
|
|
|
|
} |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ServerOK is the action sent when one or more client actions have been accepted and applied.
|
|
|
|
@ -46,7 +72,16 @@ type ServerOK struct { |
|
|
|
|
// IDs contains the IDs of the actions which were accepted and applied, in the order they were accepted and applied.
|
|
|
|
|
// This is the same as the order they were received, though other actions may have been between these that were
|
|
|
|
|
// rejected.
|
|
|
|
|
IDs []int `json:"ids"` |
|
|
|
|
IDs IDSlice `json:"ids"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s ServerOK) MarshalLogObject(encoder zapcore.ObjectEncoder) error { |
|
|
|
|
encoder.AddString("type", string(ServerOKType)) |
|
|
|
|
return encoder.AddArray("ids", s.IDs) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s ServerOK) ServerType() ServerMessageType { |
|
|
|
|
return ServerOKType |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ServerFailed is the action sent when one or more client actions have been rejected.
|
|
|
|
@ -54,14 +89,34 @@ type ServerFailed struct { |
|
|
|
|
// IDs contains the IDs of the actions which were rejected, in the order they were rejected.
|
|
|
|
|
// This is the same as the order they were received, though other actions may have been between these that were
|
|
|
|
|
// accepted and applied.
|
|
|
|
|
IDs []int `json:"ids"` |
|
|
|
|
IDs IDSlice `json:"ids"` |
|
|
|
|
// Error contains the error text sent from the server about why these actions failed.
|
|
|
|
|
Error string `json:"error"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s ServerFailed) MarshalLogObject(encoder zapcore.ObjectEncoder) error { |
|
|
|
|
encoder.AddString("type", string(ServerFailedType)) |
|
|
|
|
err := encoder.AddArray("ids", s.IDs) |
|
|
|
|
encoder.AddString("error", s.Error) |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s ServerFailed) ServerType() ServerMessageType { |
|
|
|
|
return ServerFailedType |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ServerAct is the action sent when one or more client actions from other clients have been accepted and applied.
|
|
|
|
|
// The client's own actions will never be included in this action.
|
|
|
|
|
type ServerAct struct { |
|
|
|
|
// Actions contains the actions that are now being applied.
|
|
|
|
|
Actions []action.Syncable `json:"actions"` |
|
|
|
|
Actions action.SyncableSlice `json:"actions"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s ServerAct) MarshalLogObject(encoder zapcore.ObjectEncoder) error { |
|
|
|
|
encoder.AddString("type", string(ServerActType)) |
|
|
|
|
return encoder.AddArray("actions", s.Actions) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s ServerAct) ServerType() ServerMessageType { |
|
|
|
|
return ServerActType |
|
|
|
|
} |
|
|
|
|