mchess-server/lobby_registry/registry.go

42 lines
722 B
Go
Raw Normal View History

package lobby_registry
import (
"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) GetLobbyForPlayer() *Lobby {
for _, lobby := range r.lobbies {
if !lobby.IsFull() {
return lobby
}
}
newLobby := NewEmptyLobbyByUUID(uuid.New())
r.addNewLobby(newLobby)
return newLobby
}
func (r *LobbyRegistry) addNewLobby(lobby *Lobby) uuid.UUID {
uuid := uuid.New()
r.lobbies[uuid] = lobby
return uuid
}