package server import ( "sync" "github.com/google/uuid" ) type Lobby struct { players map[uuid.UUID]*Player mutex sync.Mutex } var lobbyInstance *Lobby = nil func GetLobby() *Lobby { if lobbyInstance == nil { lobbyInstance = newLobby() } return lobbyInstance } func newLobby() *Lobby { return &Lobby{ players: make(map[uuid.UUID]*Player, 0), } } func (l *Lobby) RegisterPlayer(player *Player) { l.players[player.Uuid] = player var playersToBeAddedToGame []Player for _, player := range l.players { if !player.InGame { playersToBeAddedToGame = append(playersToBeAddedToGame, *player) } } if len(playersToBeAddedToGame) < 2 { return } game := NewGame() game.addPlayersToGame([2]Player(playersToBeAddedToGame[:2])) } func (l *Lobby) GetPlayer(playerID uuid.UUID) (*Player, bool) { player, found := l.players[playerID] return player, found } func (l *Lobby) Lock() { l.mutex.Lock() } func (l *Lobby) Unlock() { l.mutex.Unlock() }