mchess-server/server/lobby.go

61 lines
977 B
Go
Raw Normal View History

package server
2023-04-18 20:47:51 +00:00
import (
"sync"
2023-04-18 20:47:51 +00:00
"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
2023-04-18 20:47:51 +00:00
var playersToBeAddedToGame []Player
for _, player := range l.players {
if !player.InGame {
playersToBeAddedToGame = append(playersToBeAddedToGame, *player)
2023-04-18 20:47:51 +00:00
}
}
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()
}