2022-12-14 21:19:47 +00:00
|
|
|
package server
|
|
|
|
|
2023-04-18 20:47:51 +00:00
|
|
|
import (
|
2023-05-30 20:01:20 +00:00
|
|
|
"sync"
|
|
|
|
|
2023-04-18 20:47:51 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
2022-12-14 21:19:47 +00:00
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
type Lobby struct {
|
|
|
|
players map[uuid.UUID]*Player
|
|
|
|
mutex sync.Mutex
|
|
|
|
}
|
2022-12-14 21:19:47 +00:00
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
var lobbyInstance *Lobby = nil
|
2022-12-14 21:19:47 +00:00
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
func GetLobby() *Lobby {
|
2022-12-14 21:19:47 +00:00
|
|
|
if lobbyInstance == nil {
|
|
|
|
lobbyInstance = newLobby()
|
|
|
|
}
|
|
|
|
|
|
|
|
return lobbyInstance
|
|
|
|
}
|
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
func newLobby() *Lobby {
|
|
|
|
return &Lobby{
|
|
|
|
players: make(map[uuid.UUID]*Player, 0),
|
|
|
|
}
|
2022-12-14 21:19:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
func (l *Lobby) RegisterPlayer(player *Player) {
|
|
|
|
l.players[player.Uuid] = player
|
2023-04-18 20:47:51 +00:00
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
var playersToBeAddedToGame []Player
|
2022-12-14 21:19:47 +00:00
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
for _, player := range l.players {
|
2023-05-28 15:43:05 +00:00
|
|
|
if !player.InGame {
|
2023-05-30 20:01:20 +00:00
|
|
|
playersToBeAddedToGame = append(playersToBeAddedToGame, *player)
|
2023-04-18 20:47:51 +00:00
|
|
|
}
|
2023-05-28 15:43:05 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
if len(playersToBeAddedToGame) < 2 {
|
2023-05-28 15:43:05 +00:00
|
|
|
return
|
|
|
|
}
|
2022-12-14 21:19:47 +00:00
|
|
|
|
2023-05-28 15:43:05 +00:00
|
|
|
game := NewGame()
|
2023-05-30 20:01:20 +00:00
|
|
|
game.addPlayersToGame([2]Player(playersToBeAddedToGame[:2]))
|
|
|
|
}
|
2023-04-22 17:23:46 +00:00
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
func (l *Lobby) GetPlayer(playerID uuid.UUID) (*Player, bool) {
|
|
|
|
player, found := l.players[playerID]
|
|
|
|
return player, found
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Lobby) Lock() {
|
|
|
|
l.mutex.Lock()
|
|
|
|
}
|
2023-04-22 19:41:24 +00:00
|
|
|
|
2023-05-30 20:01:20 +00:00
|
|
|
func (l *Lobby) Unlock() {
|
|
|
|
l.mutex.Unlock()
|
2022-12-14 21:19:47 +00:00
|
|
|
}
|