From 682ce8437b0e811cdbe774e96afb858562dc82bd Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 15 May 2024 14:28:14 +0200 Subject: [PATCH 1/4] make games rejoinable --- api/handler/handler.go | 7 ++++++- chess/game.go | 8 ++++++++ connection/type.go | 6 ++++++ lobbies/lobby.go | 8 ++++++-- lobbies/registry.go | 2 +- lobbies/usher.go | 2 +- 6 files changed, 28 insertions(+), 5 deletions(-) diff --git a/api/handler/handler.go b/api/handler/handler.go index b7382a1..6b03eea 100644 --- a/api/handler/handler.go +++ b/api/handler/handler.go @@ -69,6 +69,7 @@ func JoinPrivateGame(c *gin.Context) { err := c.ShouldBindJSON(&req) if err != nil || req.Passphrase == nil || *req.Passphrase == "" { c.IndentedJSON(http.StatusNotFound, req) + return } u := lobbies.GetUsher() @@ -78,7 +79,10 @@ func JoinPrivateGame(c *gin.Context) { req.PlayerID != nil && req.LobbyID != nil { //is reconnect lobby := u.FindExistingPrivateLobby(utils.Passphrase(*req.Passphrase)) - _, found := lobby.GetPlayerByUUID(*req.PlayerID) + var found bool + if lobby != nil { + _, found = lobby.GetPlayerByUUID(*req.PlayerID) + } if found { c.IndentedJSON( http.StatusOK, @@ -90,6 +94,7 @@ func JoinPrivateGame(c *gin.Context) { return } else { c.IndentedJSON(http.StatusNotFound, req) + return } } diff --git a/chess/game.go b/chess/game.go index 7ac3ceb..4cefb64 100644 --- a/chess/game.go +++ b/chess/game.go @@ -157,6 +157,14 @@ func (game *Game) AddPlayersToGame(player *Player) { game.players = append(game.players, player) } +func (game *Game) AreBothPlayersConnected() bool { + if len(game.GetPlayers()) < 2 { + return false + } + + return game.players[0].hasWebsocketConnection() && game.players[1].hasWebsocketConnection() +} + func (game *Game) killGame() { log.Println("Game should be killed") } diff --git a/connection/type.go b/connection/type.go index 3b0babd..dcd3ca7 100644 --- a/connection/type.go +++ b/connection/type.go @@ -75,6 +75,8 @@ func (conn *Connection) SetWebsocketConnection(ws *gorillaws.Conn) { _, msg, err := conn.ws.ReadMessage() if err != nil { log.Println("while reading from websocket: %w", err) + + conn.unsetWebsocketConnection() if conn.disconnectCallback != nil { conn.disconnectCallback() } @@ -85,6 +87,10 @@ func (conn *Connection) SetWebsocketConnection(ws *gorillaws.Conn) { }() } +func (conn *Connection) unsetWebsocketConnection() { + conn.ws = nil +} + func (conn *Connection) Write(msg []byte) error { conn.wsWriteLock.Lock() defer conn.wsWriteLock.Unlock() diff --git a/lobbies/lobby.go b/lobbies/lobby.go index d10285c..80454f6 100644 --- a/lobbies/lobby.go +++ b/lobbies/lobby.go @@ -31,12 +31,16 @@ func newEmptyLobbyWithPassphrase() *Lobby { func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) { l.Game.AddPlayersToGame(player) - if l.IsFull() { + if l.ContainsTwoPlayers() { l.Game.StartHandling() } } -func (w *Lobby) IsFull() bool { +func (w *Lobby) AreBothPlayersConnected() bool { + return w.Game.AreBothPlayersConnected() +} + +func (w *Lobby) ContainsTwoPlayers() bool { return len(w.Game.GetPlayers()) == 2 } diff --git a/lobbies/registry.go b/lobbies/registry.go index 56d11dc..a346a2e 100644 --- a/lobbies/registry.go +++ b/lobbies/registry.go @@ -32,7 +32,7 @@ func (r *LobbyRegistry) CreateNewPrivateLobby() *Lobby { func (r *LobbyRegistry) GetLobbyForPlayer() *Lobby { for _, lobby := range r.lobbies { - if !lobby.IsFull() { + if !lobby.ContainsTwoPlayers() { return lobby } } diff --git a/lobbies/usher.go b/lobbies/usher.go index 38a0574..86a95d5 100644 --- a/lobbies/usher.go +++ b/lobbies/usher.go @@ -35,7 +35,7 @@ func (u *Usher) CreateNewPrivateLobby(player *chess.Player) *Lobby { func (u *Usher) FindExistingPrivateLobby(p utils.Passphrase) *Lobby { lobby := GetLobbyRegistry().GetLobbyByPassphrase(p) - if lobby == nil || lobby.IsFull() { + if lobby == nil || lobby.AreBothPlayersConnected() { return nil } return lobby From 90dca37d446eb7ed50b8aa1511daf70788c7dc25 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 15 May 2024 19:45:35 +0200 Subject: [PATCH 2/4] fix board state when reconnecting and set access headers --- api/handler/handler.go | 2 ++ chess/game.go | 2 +- chess/player.go | 5 ++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/api/handler/handler.go b/api/handler/handler.go index 6b03eea..886e099 100644 --- a/api/handler/handler.go +++ b/api/handler/handler.go @@ -58,6 +58,7 @@ func GetLobbyForPassphraseHandler(c *gin.Context) { ID: &lobby.Uuid, } + c.Header("Access-Control-Allow-Origin", "*") c.IndentedJSON(http.StatusOK, lobbyInfo) } @@ -84,6 +85,7 @@ func JoinPrivateGame(c *gin.Context) { _, found = lobby.GetPlayerByUUID(*req.PlayerID) } if found { + c.Header("Access-Control-Allow-Origin", "*") c.IndentedJSON( http.StatusOK, api.PlayerInfo{ diff --git a/chess/game.go b/chess/game.go index 4cefb64..6212c8b 100644 --- a/chess/game.go +++ b/chess/game.go @@ -220,5 +220,5 @@ func (game *Game) playerDisconnected(p *Player) { } func (game *Game) SetWebsocketConnectionFor(ctx context.Context, p *Player, ws *gorillaws.Conn) { - p.SetWebsocketConnectionAndSendBoardState(ctx, ws, game.board.PGN(), game.board.colorToMove) + p.SetWebsocketConnectionAndSendBoardState(ctx, ws, &game.board) } diff --git a/chess/player.go b/chess/player.go index ea8d6b5..cc663ae 100644 --- a/chess/player.go +++ b/chess/player.go @@ -41,11 +41,10 @@ func (p *Player) SetWebsocketConnection(ctx context.Context, ws *gorillaws.Conn) func (p *Player) SetWebsocketConnectionAndSendBoardState( ctx context.Context, ws *gorillaws.Conn, - boardPosition string, - turnColor types.ChessColor, + board *Board, ) { p.SetWebsocketConnection(ctx, ws) - p.SendBoardState(types.Move{}, boardPosition, turnColor) + p.SendBoardState(board.getLastMove(), board.PGN(), board.colorToMove) } func (p *Player) SetColor(color types.ChessColor) { From cd2ab106a206719c705a1bcbf6513e4df3fd6f43 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 15 May 2024 21:17:01 +0200 Subject: [PATCH 3/4] go get -u --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 3f129d6..09c688f 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/stretchr/testify v1.9.0 ) -require github.com/benbjohnson/clock v1.3.0 // indirect +require github.com/benbjohnson/clock v1.3.5 // indirect require ( github.com/bytedance/sonic v1.11.6 // indirect diff --git a/go.sum b/go.sum index d95f318..c2658f3 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o= +github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= From 75fd0cc400702c7a8af0bb4271785713cb4a4412 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 15 May 2024 21:17:32 +0200 Subject: [PATCH 4/4] go mod tidy --- go.sum | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.sum b/go.sum index c2658f3..4e57418 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= -github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o= github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= @@ -73,6 +71,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/ratelimit v0.3.1 h1:K4qVE+byfv/B3tC+4nYWP7v/6SimcO7HzHekoMNBma0= go.uber.org/ratelimit v0.3.1/go.mod h1:6euWsTB6U/Nb3X++xEUXA8ciPJvr19Q/0h1+oDcJhRk= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=