From 4207a0ed724c13ccaf39c397d5cdf584ada08392 Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 12 May 2024 15:37:53 +0200 Subject: [PATCH] forgot files --- lobbies/lobby.go | 58 ++++++++++++++++++++++++++++++++++++++++++ lobbies/registry.go | 61 +++++++++++++++++++++++++++++++++++++++++++++ lobbies/usher.go | 50 +++++++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 lobbies/lobby.go create mode 100644 lobbies/registry.go create mode 100644 lobbies/usher.go diff --git a/lobbies/lobby.go b/lobbies/lobby.go new file mode 100644 index 0000000..d10285c --- /dev/null +++ b/lobbies/lobby.go @@ -0,0 +1,58 @@ +package lobbies + +import ( + "mchess_server/chess" + "mchess_server/utils" + + "github.com/google/uuid" +) + +type Lobby struct { + Uuid uuid.UUID + Game *chess.Game + PlayerJoined chan bool + Passphrase utils.Passphrase +} + +func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby { + return &Lobby{ + Uuid: uuid, + Game: chess.NewGame(), + PlayerJoined: make(chan bool), + } +} + +func newEmptyLobbyWithPassphrase() *Lobby { + lobby := NewEmptyLobbyWithUUID(uuid.New()) + lobby.Passphrase = utils.NewPassphrase() + + return lobby +} + +func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) { + l.Game.AddPlayersToGame(player) + if l.IsFull() { + l.Game.StartHandling() + } +} + +func (w *Lobby) IsFull() bool { + return len(w.Game.GetPlayers()) == 2 +} + +func (l *Lobby) GetPlayerByUUID(uuid uuid.UUID) (*chess.Player, bool) { + for _, player := range l.Game.GetPlayers() { + if player.Uuid == uuid { + return player, true + } + } + return nil, false +} + +func (l *Lobby) GetPlayer1() *chess.Player { + return l.Game.GetPlayer1() +} + +func (l *Lobby) GetPlayer2() *chess.Player { + return l.Game.GetPlayer2() +} diff --git a/lobbies/registry.go b/lobbies/registry.go new file mode 100644 index 0000000..56d11dc --- /dev/null +++ b/lobbies/registry.go @@ -0,0 +1,61 @@ +package lobbies + +import ( + "mchess_server/utils" + + "github.com/google/uuid" +) + +type LobbyRegistry struct { + lobbies map[uuid.UUID]*Lobby +} + +var instance *LobbyRegistry + +func GetLobbyRegistry() *LobbyRegistry { + if instance == nil { + instance = newLobbyRegistry() + } + + return instance +} + +func newLobbyRegistry() *LobbyRegistry { + return &LobbyRegistry{lobbies: make(map[uuid.UUID]*Lobby)} +} + +func (r *LobbyRegistry) CreateNewPrivateLobby() *Lobby { + lobby := newEmptyLobbyWithPassphrase() + r.addNewLobby(lobby) + return lobby +} + +func (r *LobbyRegistry) GetLobbyForPlayer() *Lobby { + for _, lobby := range r.lobbies { + if !lobby.IsFull() { + return lobby + } + } + + newLobby := NewEmptyLobbyWithUUID(uuid.New()) + r.addNewLobby(newLobby) + return newLobby +} + +func (r *LobbyRegistry) GetLobbyByUUID(uuid uuid.UUID) *Lobby { + return r.lobbies[uuid] +} + +func (r *LobbyRegistry) GetLobbyByPassphrase(p utils.Passphrase) *Lobby { + for _, lobby := range r.lobbies { + if lobby.Passphrase == p { + return lobby + } + } + return nil +} + +func (r *LobbyRegistry) addNewLobby(lobby *Lobby) uuid.UUID { + r.lobbies[lobby.Uuid] = lobby + return lobby.Uuid +} diff --git a/lobbies/usher.go b/lobbies/usher.go new file mode 100644 index 0000000..38a0574 --- /dev/null +++ b/lobbies/usher.go @@ -0,0 +1,50 @@ +package lobbies + +import ( + "mchess_server/chess" + "mchess_server/utils" + + "github.com/google/uuid" +) + +type Usher struct { +} + +var usherInstance *Usher + +func newUsher() *Usher { + return &Usher{} +} + +func GetUsher() *Usher { + if usherInstance == nil { + usherInstance = newUsher() + } + return usherInstance +} + +func (u *Usher) WelcomeNewPlayer(player *chess.Player) *Lobby { + lobby := GetLobbyRegistry().GetLobbyForPlayer() + return lobby +} + +func (u *Usher) CreateNewPrivateLobby(player *chess.Player) *Lobby { + lobby := GetLobbyRegistry().CreateNewPrivateLobby() + return lobby +} + +func (u *Usher) FindExistingPrivateLobby(p utils.Passphrase) *Lobby { + lobby := GetLobbyRegistry().GetLobbyByPassphrase(p) + if lobby == nil || lobby.IsFull() { + return nil + } + return lobby +} + +func (*Usher) GetLobbyByID(id uuid.UUID) *Lobby { + return GetLobbyRegistry().GetLobbyByUUID(id) +} + +func (u *Usher) AddPlayerToLobbyAndStartGameIfFull(player *chess.Player, lobby *Lobby) { + lobby.AddPlayerAndStartGameIfFull(player) +}