mchess-server/api/move.go

35 lines
675 B
Go
Raw Normal View History

package api
2023-06-12 20:32:31 +00:00
import (
"encoding/json"
"local/m/mchess_server/types"
)
2023-06-08 18:20:37 +00:00
type WebsocketMessage struct {
2023-06-12 20:32:31 +00:00
Type MessageType `json:"messageType"`
Move *types.Move `json:"move,omitempty"`
Color *types.ChessColor `json:"color,omitempty"`
}
2023-06-08 18:20:37 +00:00
type MessageType string
const (
MoveMessage MessageType = "move"
ColorDetermined MessageType = "colorDetermined"
)
func (m WebsocketMessage) IsValidMove() bool {
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})
}