2023-05-31 21:55:40 +00:00
|
|
|
package lobby_registry
|
|
|
|
|
|
|
|
import (
|
2023-06-29 20:20:41 +00:00
|
|
|
"mchess_server/utils"
|
|
|
|
|
2023-05-31 21:55:40 +00:00
|
|
|
"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)}
|
|
|
|
}
|
|
|
|
|
2023-06-29 23:50:04 +00:00
|
|
|
func (r *LobbyRegistry) CreateNewPrivateLobby() *Lobby {
|
|
|
|
lobby := newEmptyLobbyWithPassphrase()
|
|
|
|
r.addNewLobby(lobby)
|
|
|
|
return lobby
|
|
|
|
}
|
|
|
|
|
2023-05-31 21:55:40 +00:00
|
|
|
func (r *LobbyRegistry) GetLobbyForPlayer() *Lobby {
|
|
|
|
for _, lobby := range r.lobbies {
|
|
|
|
if !lobby.IsFull() {
|
|
|
|
return lobby
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-02 19:14:02 +00:00
|
|
|
newLobby := NewEmptyLobbyWithUUID(uuid.New())
|
2023-05-31 21:55:40 +00:00
|
|
|
r.addNewLobby(newLobby)
|
|
|
|
return newLobby
|
|
|
|
}
|
|
|
|
|
2023-06-02 19:14:02 +00:00
|
|
|
func (r *LobbyRegistry) GetLobbyByUUID(uuid uuid.UUID) *Lobby {
|
|
|
|
return r.lobbies[uuid]
|
|
|
|
}
|
|
|
|
|
2023-06-29 20:20:41 +00:00
|
|
|
func (r *LobbyRegistry) GetLobbyByPassphrase(p utils.Passphrase) *Lobby {
|
2023-06-29 23:50:04 +00:00
|
|
|
for _, lobby := range r.lobbies {
|
|
|
|
if lobby.Passphrase == p {
|
|
|
|
return lobby
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2023-06-29 20:20:41 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 21:55:40 +00:00
|
|
|
func (r *LobbyRegistry) addNewLobby(lobby *Lobby) uuid.UUID {
|
2023-06-02 19:14:02 +00:00
|
|
|
r.lobbies[lobby.Uuid] = lobby
|
|
|
|
return lobby.Uuid
|
2023-05-31 21:55:40 +00:00
|
|
|
}
|