mchess-server/lobbies/lobby.go
2024-05-21 23:44:37 +02:00

61 lines
1.1 KiB
Go

package lobbies
import (
"mchess_server/chess"
"mchess_server/utils"
"github.com/google/uuid"
)
type Lobby struct {
Uuid uuid.UUID
Game *chess.Game
Passphrase utils.Passphrase
}
func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
return &Lobby{
Uuid: uuid,
Game: chess.NewGame(),
}
}
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.ContainsTwoPlayers() {
l.Game.StartHandling()
}
}
func (w *Lobby) AreBothPlayersConnected() bool {
return w.Game.AreBothPlayersConnected()
}
func (w *Lobby) ContainsTwoPlayers() 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()
}