59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
package lobby_registry
|
|
|
|
import (
|
|
"mchess_server/chess"
|
|
"mchess_server/utils"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Lobby struct {
|
|
Uuid uuid.UUID
|
|
Game *chess.Game
|
|
PlayerJoined chan bool
|
|
Passphrase utils.Passphrase
|
|
}
|
|
|
|
func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
|
|
return &Lobby{
|
|
Uuid: uuid,
|
|
Game: chess.NewGame(),
|
|
PlayerJoined: make(chan bool),
|
|
}
|
|
}
|
|
|
|
func NewEmptyLobbyWithPassphrase()*Lobby {
|
|
lobby := NewEmptyLobbyWithUUID(uuid.New())
|
|
lobby.Passphrase = utils.NewPassphrase()
|
|
|
|
return lobby
|
|
}
|
|
|
|
func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) {
|
|
l.Game.AddPlayersToGame(player)
|
|
if l.IsFull() {
|
|
go l.Game.Handle()
|
|
}
|
|
}
|
|
|
|
func (w *Lobby) IsFull() bool {
|
|
return len(w.Game.GetPlayers()) == 2
|
|
}
|
|
|
|
func (l *Lobby) GetPlayerByUUID(uuid uuid.UUID) (*chess.Player, bool) {
|
|
for _, player := range l.Game.GetPlayers() {
|
|
if player.Uuid == uuid {
|
|
return player, true
|
|
}
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
func (l *Lobby) GetPlayer1() *chess.Player {
|
|
return l.Game.GetPlayer1()
|
|
}
|
|
|
|
func (l *Lobby) GetPlayer2() *chess.Player {
|
|
return l.Game.GetPlayer2()
|
|
}
|