35 lines
675 B
Go
35 lines
675 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"`
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func GetColorDeterminedMessage(color types.ChessColor) ([]byte, error) {
|
|
return json.Marshal(WebsocketMessage{Type: ColorDetermined, Color: &color})
|
|
|
|
}
|