make games rejoinable
This commit is contained in:
parent
b4c82d6c8f
commit
682ce8437b
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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")
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user