mchess-server/lobbies/registry.go
Marco 636ce06836 Improve reconnection handling
1. Lobbies are only identified by their passphrases
2. Improve logging
3. Do not close an existing websocket connection for a player but ignore
   the request
2024-05-20 15:36:13 +02:00

58 lines
1.1 KiB
Go

package lobbies
import (
"mchess_server/utils"
"github.com/google/uuid"
)
type LobbyRegistry struct {
lobbies map[utils.Passphrase]*Lobby
}
var instance *LobbyRegistry
func GetLobbyRegistry() *LobbyRegistry {
if instance == nil {
instance = newLobbyRegistry()
}
return instance
}
func newLobbyRegistry() *LobbyRegistry {
return &LobbyRegistry{lobbies: make(map[utils.Passphrase]*Lobby)}
}
func (r *LobbyRegistry) CreateNewPrivateLobby() *Lobby {
lobby := newEmptyLobbyWithPassphrase()
r.addNewLobby(lobby)
return lobby
}
func (r *LobbyRegistry) GetLobbyForPlayer() *Lobby {
for _, lobby := range r.lobbies {
if !lobby.ContainsTwoPlayers() {
return lobby
}
}
newLobby := NewEmptyLobbyWithUUID(uuid.New())
r.addNewLobby(newLobby)
return newLobby
}
func (r *LobbyRegistry) GetLobbyByPassphrase(p utils.Passphrase) *Lobby {
for _, lobby := range r.lobbies {
if lobby.Passphrase == p {
return lobby
}
}
return nil
}
func (r *LobbyRegistry) addNewLobby(lobby *Lobby) uuid.UUID {
r.lobbies[lobby.Passphrase] = lobby
return lobby.Uuid
}