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