2023-05-31 21:55:40 +00:00
|
|
|
package usher
|
|
|
|
|
|
|
|
import (
|
2023-06-25 14:11:29 +00:00
|
|
|
"mchess_server/chess"
|
|
|
|
lobbies "mchess_server/lobby_registry"
|
2023-06-29 20:20:41 +00:00
|
|
|
"mchess_server/utils"
|
2023-05-31 21:55:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Usher struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
var instance *Usher
|
|
|
|
|
2023-06-29 20:20:41 +00:00
|
|
|
func newUsher() *Usher {
|
2023-05-31 21:55:40 +00:00
|
|
|
return &Usher{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUsher() *Usher {
|
|
|
|
if instance == nil {
|
2023-06-29 20:20:41 +00:00
|
|
|
instance = newUsher()
|
2023-05-31 21:55:40 +00:00
|
|
|
}
|
|
|
|
return instance
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Usher) WelcomeNewPlayer(player *chess.Player) *lobbies.Lobby {
|
|
|
|
lobby := lobbies.GetLobbyRegistry().GetLobbyForPlayer()
|
|
|
|
return lobby
|
|
|
|
}
|
|
|
|
|
2023-06-29 23:50:04 +00:00
|
|
|
func (u *Usher) CreateNewPrivateLobby(player *chess.Player) *lobbies.Lobby {
|
|
|
|
lobby := lobbies.GetLobbyRegistry().CreateNewPrivateLobby()
|
|
|
|
return lobby
|
2023-06-28 17:51:36 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 20:20:41 +00:00
|
|
|
func (u *Usher) FindExistingPrivateLobby(p utils.Passphrase) *lobbies.Lobby {
|
2023-06-29 23:50:04 +00:00
|
|
|
lobby := lobbies.GetLobbyRegistry().GetLobbyByPassphrase(p)
|
|
|
|
if lobby.IsFull() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return lobby
|
2023-06-29 20:20:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 19:14:02 +00:00
|
|
|
func (u *Usher) AddPlayerToLobbyAndStartGameIfFull(player *chess.Player, lobby *lobbies.Lobby) {
|
|
|
|
lobby.AddPlayerAndStartGameIfFull(player)
|
2023-05-31 21:55:40 +00:00
|
|
|
}
|