40 lines
965 B
Go
40 lines
965 B
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"local/m/mchess_server/types"
|
|
)
|
|
|
|
type WebsocketMessage struct {
|
|
Type MessageType `json:"messageType"`
|
|
Move *types.Move `json:"move,omitempty"`
|
|
Color *types.ChessColor `json:"color,omitempty"`
|
|
Reason *string `json:"reason,omitempty"`
|
|
}
|
|
|
|
type MessageType string
|
|
|
|
const (
|
|
MoveMessage MessageType = "move"
|
|
InvalidMoveMessage MessageType = "invalidMove"
|
|
ColorDetermined MessageType = "colorDetermined"
|
|
)
|
|
|
|
func (m WebsocketMessage) IsValidMoveMessage() bool {
|
|
if m.Type != MoveMessage {
|
|
return false
|
|
}
|
|
if m.Move == nil {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func GetColorDeterminedMessage(color types.ChessColor) ([]byte, error) {
|
|
return json.Marshal(WebsocketMessage{Type: ColorDetermined, Color: &color})
|
|
}
|
|
|
|
func GetInvalidMoveMessage(move types.Move, reason string) ([]byte, error) {
|
|
return json.Marshal(WebsocketMessage{Type: InvalidMoveMessage, Move: &move, Reason: &reason})
|
|
}
|