42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"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"`
|
|
Position *string `json:"position,omitempty"`
|
|
}
|
|
|
|
type MessageType string
|
|
|
|
const (
|
|
MoveMessage MessageType = "move"
|
|
InvalidMoveMessage MessageType = "invalidMove"
|
|
ColorDetermined MessageType = "colorDetermined"
|
|
TakenEnPassant MessageType = "takenEnPassant"
|
|
)
|
|
|
|
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})
|
|
}
|