mchess-server/lobbies/lobby.go

61 lines
1.1 KiB
Go
Raw Normal View History

2024-05-12 13:37:53 +00:00
package lobbies
import (
"mchess_server/chess"
"mchess_server/utils"
"github.com/google/uuid"
)
type Lobby struct {
2024-05-21 21:44:37 +00:00
Uuid uuid.UUID
Game *chess.Game
Passphrase utils.Passphrase
2024-05-12 13:37:53 +00:00
}
func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
return &Lobby{
2024-05-21 21:44:37 +00:00
Uuid: uuid,
Game: chess.NewGame(),
2024-05-12 13:37:53 +00:00
}
}
func newEmptyLobbyWithPassphrase() *Lobby {
lobby := NewEmptyLobbyWithUUID(uuid.New())
lobby.Passphrase = utils.NewPassphrase()
return lobby
}
func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) {
l.Game.AddPlayersToGame(player)
2024-05-15 12:28:14 +00:00
if l.ContainsTwoPlayers() {
2024-05-12 13:37:53 +00:00
l.Game.StartHandling()
}
}
2024-05-15 12:28:14 +00:00
func (w *Lobby) AreBothPlayersConnected() bool {
return w.Game.AreBothPlayersConnected()
}
func (w *Lobby) ContainsTwoPlayers() bool {
2024-05-12 13:37:53 +00:00
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()
}