Add position to websocket message.

This commit is contained in:
Marco 2023-08-14 00:05:47 +02:00
parent ff2ec599fe
commit 9fbffc61f9
5 changed files with 11 additions and 9 deletions

View File

@ -6,10 +6,11 @@ import (
)
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 `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

View File

@ -157,12 +157,12 @@ func (game Game) notifyPlayersAboutGameStart() error {
}
func (game Game) broadcastMove(move types.Move) error {
err := game.GetPlayer1().SendMove(move)
err := game.GetPlayer1().SendMoveAndPosition(move, game.board.PGN())
if err != nil {
return err
}
err = game.GetPlayer2().SendMove(move)
err = game.GetPlayer2().SendMoveAndPosition(move, game.board.PGN())
if err != nil {
return err
}

View File

@ -5,7 +5,7 @@ import (
"strings"
)
func (b *Board) pgn() string {
func (b *Board) PGN() string {
var pgn string
var shortName string

View File

@ -10,7 +10,7 @@ func Test_PGN_StartingPosition(t *testing.T) {
board := newBoard()
board.Init()
pgn := board.pgn()
pgn := board.PGN()
assert.Equal(t,
"RNBQKBNR/PPPPPPPP/--------/--------/--------/--------/pppppppp/rnbqkbnr",

View File

@ -36,10 +36,11 @@ func (p *Player) SetConnection(ctx context.Context, conn *websocket.Conn) {
p.wsConnEstablished <- true
}
func (p *Player) SendMove(move types.Move) error {
func (p *Player) SendMoveAndPosition(move types.Move, boardPosition string) error {
messageToSend, err := json.Marshal(api.WebsocketMessage{
Type: api.MoveMessage,
Move: &move,
Position: &boardPosition,
})
if err != nil {
log.Println("Error while marshalling: ", err)