From b052d8e21c055eef16cb0c6e36f41cb461f98def Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 30 Jun 2023 01:50:04 +0200 Subject: [PATCH] Finalize playing in private lobbies. --- api/player_info.go | 6 ++-- lobby_registry/lobby.go | 14 ++++----- lobby_registry/registry.go | 18 +++++++---- main.go | 64 ++++++++++++++++++++------------------ usher/usher.go | 12 ++++--- 5 files changed, 63 insertions(+), 51 deletions(-) diff --git a/api/player_info.go b/api/player_info.go index be2b054..3540151 100644 --- a/api/player_info.go +++ b/api/player_info.go @@ -3,7 +3,7 @@ package api import "github.com/google/uuid" type PlayerInfo struct { - PlayerID uuid.UUID `json:"playerID"` - LobbyID uuid.UUID `json:"lobbyID"` - Passphrase *string `json:"passphrase,omitempty"` + PlayerID *uuid.UUID `json:"playerID,omitempty"` + LobbyID *uuid.UUID `json:"lobbyID,omitempty"` + Passphrase *string `json:"passphrase,omitempty"` } diff --git a/lobby_registry/lobby.go b/lobby_registry/lobby.go index beab692..d47cb3b 100644 --- a/lobby_registry/lobby.go +++ b/lobby_registry/lobby.go @@ -8,10 +8,10 @@ import ( ) type Lobby struct { - Uuid uuid.UUID - Game *chess.Game + Uuid uuid.UUID + Game *chess.Game PlayerJoined chan bool - Passphrase utils.Passphrase + Passphrase utils.Passphrase } func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby { @@ -22,11 +22,11 @@ func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby { } } -func NewEmptyLobbyWithPassphrase()*Lobby { - lobby := NewEmptyLobbyWithUUID(uuid.New()) - lobby.Passphrase = utils.NewPassphrase() +func newEmptyLobbyWithPassphrase() *Lobby { + lobby := NewEmptyLobbyWithUUID(uuid.New()) + lobby.Passphrase = utils.NewPassphrase() - return lobby + return lobby } func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) { diff --git a/lobby_registry/registry.go b/lobby_registry/registry.go index 4e001d6..1412462 100644 --- a/lobby_registry/registry.go +++ b/lobby_registry/registry.go @@ -24,6 +24,12 @@ 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() { @@ -41,12 +47,12 @@ func (r *LobbyRegistry) GetLobbyByUUID(uuid uuid.UUID) *Lobby { } func (r *LobbyRegistry) GetLobbyByPassphrase(p utils.Passphrase) *Lobby { - for _,lobby := range r.lobbies { - if lobby.Passphrase == p { - return lobby - } - } - return nil + for _, lobby := range r.lobbies { + if lobby.Passphrase == p { + return lobby + } + } + return nil } func (r *LobbyRegistry) addNewLobby(lobby *Lobby) uuid.UUID { diff --git a/main.go b/main.go index 86d7730..d4f77cf 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,7 @@ func main() { router.GET("/api/random", registerForRandomGame) router.GET("/api/hostPrivate", hostPrivateGame) - router.GET("/api/joinPrivate", joinPrivateGame) + router.POST("/api/joinPrivate", joinPrivateGame) router.GET("/api/ws", registerWebSocketConnection) if hostname == "mbook" { @@ -59,8 +59,8 @@ func registerForRandomGame(c *gin.Context) { mut.Unlock() info := api.PlayerInfo{ - PlayerID: player.Uuid, - LobbyID: lobby.Uuid, + PlayerID: &player.Uuid, + LobbyID: &lobby.Uuid, } log.Println("responding with info ", info) @@ -73,47 +73,49 @@ func hostPrivateGame(c *gin.Context) { u := usher.GetUsher() mut.Lock() - lobby := u.FindNewPrivateLobby(player) - u.AddPlayerToLobbyAndStartGameIfFull(player, lobby) + lobby := u.CreateNewPrivateLobby(player) + u.AddPlayerToLobbyAndStartGameIfFull(player, lobby) mut.Unlock() passphrase := lobby.Passphrase.String() info := api.PlayerInfo{ - PlayerID: player.Uuid, - LobbyID: lobby.Uuid, + PlayerID: &player.Uuid, + LobbyID: &lobby.Uuid, Passphrase: &passphrase, } - c.Header("Access-Control-Allow-Origin", "*") c.IndentedJSON(http.StatusOK, info) } func joinPrivateGame(c *gin.Context) { - req := api.PlayerInfo{} - err := c.ShouldBindJSON(req) - if err!= nil || req.Passphrase == nil || *req.Passphrase == "" { - c.IndentedJSON(http.StatusNotFound, req) - } - - player := chess.NewPlayer(uuid.New()) - u := usher.GetUsher() + req := api.PlayerInfo{} + log.Println("OI, WHERES ME LOGS") + log.Println(c.Request.Body) + err := c.ShouldBindJSON(&req) + if err != nil || req.Passphrase == nil || *req.Passphrase == "" { + c.IndentedJSON(http.StatusNotFound, req) + } + + player := chess.NewPlayer(uuid.New()) + u := usher.GetUsher() mut.Lock() - defer mut.Unlock() + defer mut.Unlock() lobby := u.FindExistingPrivateLobby(utils.Passphrase(*req.Passphrase)) - if lobby != nil { - u.AddPlayerToLobbyAndStartGameIfFull(player, lobby) - } else { - c.IndentedJSON(http.StatusNotFound, req) - return - } + if lobby != nil { + u.AddPlayerToLobbyAndStartGameIfFull(player, lobby) + } else { + c.IndentedJSON(http.StatusNotFound, req) + return + } - info := api.PlayerInfo{ - PlayerID: player.Uuid, - LobbyID: lobby.Uuid, - Passphrase: req.Passphrase, - } - c.IndentedJSON(http.StatusOK, info) + info := api.PlayerInfo{ + PlayerID: &player.Uuid, + LobbyID: &lobby.Uuid, + Passphrase: req.Passphrase, + } + c.Header("Access-Control-Allow-Origin", "*") + c.IndentedJSON(http.StatusOK, info) } func registerWebSocketConnection(c *gin.Context) { @@ -145,8 +147,8 @@ func waitForAndHandlePlayerID(ctx context.Context, conn *websocket.Conn) { return } - lobby := lobbies.GetLobbyRegistry().GetLobbyByUUID(info.LobbyID) - player, found := lobby.GetPlayerByUUID(info.PlayerID) + lobby := lobbies.GetLobbyRegistry().GetLobbyByUUID(*info.LobbyID) + player, found := lobby.GetPlayerByUUID(*info.PlayerID) if !found { conn.Close(websocket.StatusCode(400), "player not found") return diff --git a/usher/usher.go b/usher/usher.go index 4705bc6..239c111 100644 --- a/usher/usher.go +++ b/usher/usher.go @@ -27,13 +27,17 @@ func (u *Usher) WelcomeNewPlayer(player *chess.Player) *lobbies.Lobby { return lobby } -func (u*Usher) FindNewPrivateLobby(player *chess.Player) *lobbies.Lobby { - lobby := lobbies.NewEmptyLobbyWithPassphrase() - return lobby +func (u *Usher) CreateNewPrivateLobby(player *chess.Player) *lobbies.Lobby { + lobby := lobbies.GetLobbyRegistry().CreateNewPrivateLobby() + return lobby } func (u *Usher) FindExistingPrivateLobby(p utils.Passphrase) *lobbies.Lobby { - return lobbies.GetLobbyRegistry().GetLobbyByPassphrase(p) + lobby := lobbies.GetLobbyRegistry().GetLobbyByPassphrase(p) + if lobby.IsFull() { + return nil + } + return lobby } func (u *Usher) AddPlayerToLobbyAndStartGameIfFull(player *chess.Player, lobby *lobbies.Lobby) {