Switch from sending moves to sending state

This commit is contained in:
Marco 2023-12-25 17:52:32 +01:00
parent 6e66d892b5
commit a6d8f47e8a
2 changed files with 7 additions and 6 deletions

View File

@ -161,20 +161,20 @@ func (game Game) notifyPlayersAboutGameStart() error {
}
game.GetPlayer1().writeMessage(colorDeterminedPlayer1)
game.GetPlayer1().SendBoardState(game.board.PGN(), types.White)
game.GetPlayer1().SendBoardState(types.Move{}, game.board.PGN(), types.White)
game.GetPlayer2().writeMessage(colorDeterminedPlayer2)
game.GetPlayer2().SendBoardState(game.board.PGN(), types.Black)
game.GetPlayer2().SendBoardState(types.Move{}, game.board.PGN(), types.White)
return nil
}
func (game Game) broadcastMove(move types.Move) error {
err := game.GetPlayer1().SendMoveAndPosition(move, game.board.PGN())
err := game.GetPlayer1().SendBoardState(move, game.board.PGN(), game.currentTurnPlayer.color)
if err != nil {
return err
}
err = game.GetPlayer2().SendMoveAndPosition(move, game.board.PGN())
err = game.GetPlayer2().SendBoardState(move, game.board.PGN(), game.currentTurnPlayer.color)
if err != nil {
return err
}

View File

@ -45,7 +45,7 @@ func (p *Player) SetWebsocketConnectionAndSendBoardState(
turnColor types.ChessColor,
) {
p.SetWebsocketConnection(ctx, ws)
p.SendBoardState(boardPosition, turnColor)
p.SendBoardState(types.Move{}, boardPosition, turnColor)
}
func (p *Player) SetDisconnectCallback(cb func(*Player)) {
@ -62,13 +62,14 @@ func (p *Player) IsInGame() bool {
return p.hasWebsocketConnection()
}
func (p *Player) SendBoardState(boardPosition string, turnColor types.ChessColor) error {
func (p *Player) SendBoardState(move types.Move, boardPosition string, turnColor types.ChessColor) error {
var pColor = p.color
if p.color == "" { // we default to white if we do not know the color yet
pColor = types.White
}
messageToSend, err := json.Marshal(api.WebsocketMessage{
Move: &move,
Type: api.BoardStateMessage,
TurnColor: &turnColor,
PlayerColor: &pColor,