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.
This commit is contained in:
Marco 2023-12-09 20:31:45 +01:00
parent 45fac78e06
commit c29cdffbc2
2 changed files with 10 additions and 6 deletions

View File

@ -17,7 +17,7 @@ type WebsocketMessage struct {
type MessageType string type MessageType string
const ( const (
PositionMessage MessageType = "position" BoardStateMessage MessageType = "boardState"
MoveMessage MessageType = "move" MoveMessage MessageType = "move"
InvalidMoveMessage MessageType = "invalidMove" InvalidMoveMessage MessageType = "invalidMove"
ColorDetermined MessageType = "colorDetermined" ColorDetermined MessageType = "colorDetermined"
@ -38,7 +38,7 @@ func (m WebsocketMessage) IsValidMoveMessage() bool {
} }
func GetColorDeterminedMessage(color types.ChessColor) ([]byte, error) { 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) { func GetInvalidMoveMessage(move types.Move, reason string) ([]byte, error) {

View File

@ -30,7 +30,7 @@ func NewPlayer(uuid uuid.UUID) *Player {
return player return player
} }
func (p Player) HasWebsocketConnection() bool { func (p Player) hasWebsocketConnection() bool {
return p.Conn.HasWebsocketConnection() return p.Conn.HasWebsocketConnection()
} }
@ -45,7 +45,7 @@ func (p *Player) SetWebsocketConnectionAndSendBoardState(
turnColor types.ChessColor, turnColor types.ChessColor,
) { ) {
p.SetWebsocketConnection(ctx, ws) p.SetWebsocketConnection(ctx, ws)
p.SendPosition(boardPosition, turnColor) p.SendBoardState(boardPosition, turnColor)
} }
func (p *Player) SetDisconnectCallback(cb func(*Player)) { func (p *Player) SetDisconnectCallback(cb func(*Player)) {
@ -58,14 +58,18 @@ func (p *Player) PlayerDisconnectedCallback() {
p.disconnectCallback(p) 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 var pColor = p.color
if p.color == "" { // we default to white if we do not know the color yet if p.color == "" { // we default to white if we do not know the color yet
pColor = types.White pColor = types.White
} }
messageToSend, err := json.Marshal(api.WebsocketMessage{ messageToSend, err := json.Marshal(api.WebsocketMessage{
Type: api.PositionMessage, Type: api.BoardStateMessage,
TurnColor: &turnColor, TurnColor: &turnColor,
PlayerColor: &pColor, PlayerColor: &pColor,
Position: &boardPosition, Position: &boardPosition,