2023-05-31 21:55:40 +00:00
|
|
|
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"
|
2023-05-31 21:55:40 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Lobby struct {
|
2023-06-29 23:50:04 +00:00
|
|
|
Uuid uuid.UUID
|
|
|
|
Game *chess.Game
|
2023-06-02 19:14:02 +00:00
|
|
|
PlayerJoined chan bool
|
2023-06-29 23:50:04 +00:00
|
|
|
Passphrase utils.Passphrase
|
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,
|
2023-06-06 20:58:33 +00:00
|
|
|
Game: chess.NewGame(),
|
2023-06-02 19:14:02 +00:00
|
|
|
PlayerJoined: make(chan bool),
|
|
|
|
}
|
2023-05-31 21:55:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 23:50:04 +00:00
|
|
|
func newEmptyLobbyWithPassphrase() *Lobby {
|
|
|
|
lobby := NewEmptyLobbyWithUUID(uuid.New())
|
|
|
|
lobby.Passphrase = utils.NewPassphrase()
|
2023-06-28 17:51:36 +00:00
|
|
|
|
2023-06-29 23:50:04 +00:00
|
|
|
return lobby
|
2023-06-28 17:51:36 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2023-06-02 19:14:02 +00:00
|
|
|
}
|
2023-05-31 21:55:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Lobby) IsFull() bool {
|
2023-06-06 20:58:33 +00:00
|
|
|
return len(w.Game.GetPlayers()) == 2
|
2023-05-31 21:55:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 19:14:02 +00:00
|
|
|
func (l *Lobby) GetPlayerByUUID(uuid uuid.UUID) (*chess.Player, bool) {
|
2023-06-06 20:58:33 +00:00
|
|
|
for _, player := range l.Game.GetPlayers() {
|
2023-06-02 19:14:02 +00:00
|
|
|
if player.Uuid == uuid {
|
|
|
|
return player, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2023-05-31 21:55:40 +00:00
|
|
|
func (l *Lobby) GetPlayer1() *chess.Player {
|
2023-06-06 20:58:33 +00:00
|
|
|
return l.Game.GetPlayer1()
|
2023-05-31 21:55:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Lobby) GetPlayer2() *chess.Player {
|
2023-06-06 20:58:33 +00:00
|
|
|
return l.Game.GetPlayer2()
|
2023-05-31 21:55:40 +00:00
|
|
|
}
|