mchess-server/lobby_registry/lobby.go

54 lines
938 B
Go
Raw Normal View History

package lobby_registry
import (
"local/m/mchess_server/chess"
"github.com/google/uuid"
)
type Lobby struct {
Uuid uuid.UUID
players []*chess.Player
Game chess.Game
PlayerJoined chan bool
}
func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
return &Lobby{
Uuid: uuid,
Game: *chess.NewGame(),
PlayerJoined: make(chan bool),
}
}
func (w *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) {
w.players = append(w.players, player)
w.Game.AddPlayersToGame(player)
if w.IsFull() {
go w.Game.Handle()
}
}
func (w *Lobby) IsFull() bool {
return len(w.players) == 2
}
func (l *Lobby) GetPlayerByUUID(uuid uuid.UUID) (*chess.Player, bool) {
for _, player := range l.players {
if player.Uuid == uuid {
return player, true
}
}
return nil, false
}
func (l *Lobby) GetPlayer1() *chess.Player {
return l.players[0]
}
func (l *Lobby) GetPlayer2() *chess.Player {
return l.players[1]
}