62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
package lobby_registry
|
|
|
|
import (
|
|
"mchess_server/utils"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type LobbyRegistry struct {
|
|
lobbies map[uuid.UUID]*Lobby
|
|
}
|
|
|
|
var instance *LobbyRegistry
|
|
|
|
func GetLobbyRegistry() *LobbyRegistry {
|
|
if instance == nil {
|
|
instance = newLobbyRegistry()
|
|
}
|
|
|
|
return instance
|
|
}
|
|
|
|
func newLobbyRegistry() *LobbyRegistry {
|
|
return &LobbyRegistry{lobbies: make(map[uuid.UUID]*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.IsFull() {
|
|
return lobby
|
|
}
|
|
}
|
|
|
|
newLobby := NewEmptyLobbyWithUUID(uuid.New())
|
|
r.addNewLobby(newLobby)
|
|
return newLobby
|
|
}
|
|
|
|
func (r *LobbyRegistry) GetLobbyByUUID(uuid uuid.UUID) *Lobby {
|
|
return r.lobbies[uuid]
|
|
}
|
|
|
|
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.Uuid] = lobby
|
|
return lobby.Uuid
|
|
}
|