mchess-server/lobby_registry/lobby.go

59 lines
1.1 KiB
Go
Raw Normal View History

package lobby_registry
import (
2023-06-25 14:11:29 +00:00
"mchess_server/chess"
2023-06-28 17:51:36 +00:00
"mchess_server/utils"
"github.com/google/uuid"
)
type Lobby struct {
2023-06-06 20:58:33 +00:00
Uuid uuid.UUID
Game *chess.Game
PlayerJoined chan bool
2023-06-28 17:51:36 +00:00
Passphrase string
}
func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
return &Lobby{
Uuid: uuid,
2023-06-06 20:58:33 +00:00
Game: chess.NewGame(),
PlayerJoined: make(chan bool),
}
}
2023-06-28 17:51:36 +00:00
func NewEmptyLobbyWithPassphrase()*Lobby {
lobby := NewEmptyLobbyWithUUID(uuid.New())
lobby.Passphrase = string(utils.NewPassphrase())
return lobby
}
2023-06-25 14:11:29 +00:00
func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) {
l.Game.AddPlayersToGame(player)
if l.IsFull() {
go l.Game.Handle()
}
}
func (w *Lobby) IsFull() bool {
2023-06-06 20:58:33 +00:00
return len(w.Game.GetPlayers()) == 2
}
func (l *Lobby) GetPlayerByUUID(uuid uuid.UUID) (*chess.Player, bool) {
2023-06-06 20:58:33 +00:00
for _, player := range l.Game.GetPlayers() {
if player.Uuid == uuid {
return player, true
}
}
return nil, false
}
func (l *Lobby) GetPlayer1() *chess.Player {
2023-06-06 20:58:33 +00:00
return l.Game.GetPlayer1()
}
func (l *Lobby) GetPlayer2() *chess.Player {
2023-06-06 20:58:33 +00:00
return l.Game.GetPlayer2()
}