2023-05-31 21:55:40 +00:00
|
|
|
package lobby_registry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"local/m/mchess_server/chess"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Lobby struct {
|
2023-06-02 19:14:02 +00:00
|
|
|
Uuid uuid.UUID
|
2023-05-31 21:55:40 +00:00
|
|
|
players []*chess.Player
|
2023-06-02 19:14:02 +00:00
|
|
|
|
|
|
|
Game chess.Game
|
|
|
|
|
|
|
|
PlayerJoined chan bool
|
2023-05-31 21:55:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 19:14:02 +00:00
|
|
|
func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
|
|
|
|
return &Lobby{
|
|
|
|
Uuid: uuid,
|
|
|
|
Game: *chess.NewGame(),
|
|
|
|
PlayerJoined: make(chan bool),
|
|
|
|
}
|
2023-05-31 21:55:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 19:14:02 +00:00
|
|
|
func (w *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) {
|
2023-05-31 21:55:40 +00:00
|
|
|
w.players = append(w.players, player)
|
2023-06-02 19:14:02 +00:00
|
|
|
w.Game.AddPlayersToGame(player)
|
|
|
|
if w.IsFull() {
|
|
|
|
go w.Game.Handle()
|
|
|
|
}
|
2023-05-31 21:55:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Lobby) IsFull() bool {
|
|
|
|
return len(w.players) == 2
|
|
|
|
}
|
|
|
|
|
2023-06-02 19:14:02 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-31 21:55:40 +00:00
|
|
|
func (l *Lobby) GetPlayer1() *chess.Player {
|
|
|
|
return l.players[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Lobby) GetPlayer2() *chess.Player {
|
|
|
|
return l.players[1]
|
|
|
|
}
|