From c29cdffbc2ca92b235129c911473ed8c35dd6fee Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 9 Dec 2023 20:31:45 +0100 Subject: [PATCH] Reconnect works (kind of) With the right changes in the client, the reconnect works (but only for the first time). At the moment, we will create a new player whenever connection wants to join a private game. This will also clear all the disconnect callbacks that we set in the player. --- api/move.go | 4 ++-- chess/player.go | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/api/move.go b/api/move.go index b721482..e42a743 100644 --- a/api/move.go +++ b/api/move.go @@ -17,7 +17,7 @@ type WebsocketMessage struct { type MessageType string const ( - PositionMessage MessageType = "position" + BoardStateMessage MessageType = "boardState" MoveMessage MessageType = "move" InvalidMoveMessage MessageType = "invalidMove" ColorDetermined MessageType = "colorDetermined" @@ -38,7 +38,7 @@ func (m WebsocketMessage) IsValidMoveMessage() bool { } func GetColorDeterminedMessage(color types.ChessColor) ([]byte, error) { - return json.Marshal(WebsocketMessage{Type: ColorDetermined, TurnColor: &color}) + return json.Marshal(WebsocketMessage{Type: ColorDetermined, PlayerColor: &color}) } func GetInvalidMoveMessage(move types.Move, reason string) ([]byte, error) { diff --git a/chess/player.go b/chess/player.go index 7d40491..13d5348 100644 --- a/chess/player.go +++ b/chess/player.go @@ -30,7 +30,7 @@ func NewPlayer(uuid uuid.UUID) *Player { return player } -func (p Player) HasWebsocketConnection() bool { +func (p Player) hasWebsocketConnection() bool { return p.Conn.HasWebsocketConnection() } @@ -45,7 +45,7 @@ func (p *Player) SetWebsocketConnectionAndSendBoardState( turnColor types.ChessColor, ) { p.SetWebsocketConnection(ctx, ws) - p.SendPosition(boardPosition, turnColor) + p.SendBoardState(boardPosition, turnColor) } func (p *Player) SetDisconnectCallback(cb func(*Player)) { @@ -58,14 +58,18 @@ func (p *Player) PlayerDisconnectedCallback() { p.disconnectCallback(p) } -func (p *Player) SendPosition(boardPosition string, turnColor types.ChessColor) error { +func (p *Player) IsInGame() bool { + return p.hasWebsocketConnection() +} + +func (p *Player) SendBoardState(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{ - Type: api.PositionMessage, + Type: api.BoardStateMessage, TurnColor: &turnColor, PlayerColor: &pColor, Position: &boardPosition,