forgot files

This commit is contained in:
Marco 2024-05-12 15:37:53 +02:00
parent fc088a04fe
commit 4207a0ed72
3 changed files with 169 additions and 0 deletions

58
lobbies/lobby.go Normal file
View File

@ -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()
}

61
lobbies/registry.go Normal file
View File

@ -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
}

50
lobbies/usher.go Normal file
View File

@ -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)
}