mchess-server/usher/usher.go
Marco aac428baab First changes to move away from central registry of all players
1. Introduce 'usher' that opens lobbies and fills it and waits for them
   to be filled
2. Add global registry of all games
2023-05-31 23:55:40 +02:00

40 lines
718 B
Go

package usher
import (
"local/m/mchess_server/chess"
lobbies "local/m/mchess_server/lobby_registry"
)
type Usher struct {
}
var instance *Usher
func NewUsher() *Usher {
return &Usher{}
}
func GetUsher() *Usher {
if instance == nil {
instance = NewUsher()
}
return instance
}
func (u *Usher) WelcomeNewPlayer(player *chess.Player) *lobbies.Lobby {
lobby := lobbies.GetLobbyRegistry().GetLobbyForPlayer()
return lobby
}
func (u *Usher) AddPlayerToLobby(player *chess.Player, lobby *lobbies.Lobby) {
lobby.AddPlayer(player)
}
func (u *Usher) WaitForTwoPlayersInLobby(lobby *lobbies.Lobby) {
player1 := lobby.GetPlayer1()
<-player1.JoinedLobby
player2 := lobby.GetPlayer2()
<-player2.JoinedLobby
}