mchess-server/api/move.go

40 lines
957 B
Go
Raw Normal View History

package api
2023-06-12 20:32:31 +00:00
import (
"encoding/json"
2023-06-25 14:11:29 +00:00
"mchess_server/types"
2023-06-12 20:32:31 +00:00
)
2023-06-08 18:20:37 +00:00
type WebsocketMessage struct {
Type MessageType `json:"messageType"`
Move *types.Move `json:"move,omitempty"`
Color *types.ChessColor `json:"color,omitempty"`
Reason *string `json:"reason,omitempty"`
}
2023-06-08 18:20:37 +00:00
type MessageType string
const (
MoveMessage MessageType = "move"
InvalidMoveMessage MessageType = "invalidMove"
ColorDetermined MessageType = "colorDetermined"
2023-06-08 18:20:37 +00:00
)
func (m WebsocketMessage) IsValidMoveMessage() bool {
2023-06-08 18:20:37 +00:00
if m.Type != MoveMessage {
return false
}
if m.Move == nil {
return false
}
return true
}
2023-06-12 20:32:31 +00:00
func GetColorDeterminedMessage(color types.ChessColor) ([]byte, error) {
return json.Marshal(WebsocketMessage{Type: ColorDetermined, Color: &color})
}
2023-06-12 20:32:31 +00:00
func GetInvalidMoveMessage(move types.Move, reason string) ([]byte, error) {
return json.Marshal(WebsocketMessage{Type: InvalidMoveMessage, Move: &move, Reason: &reason})
2023-06-12 20:32:31 +00:00
}