Finalize playing in private lobbies.

This commit is contained in:
Marco 2023-06-30 01:50:04 +02:00
parent 4b04b3532c
commit b052d8e21c
5 changed files with 63 additions and 51 deletions

View File

@ -3,7 +3,7 @@ package api
import "github.com/google/uuid" import "github.com/google/uuid"
type PlayerInfo struct { type PlayerInfo struct {
PlayerID uuid.UUID `json:"playerID"` PlayerID *uuid.UUID `json:"playerID,omitempty"`
LobbyID uuid.UUID `json:"lobbyID"` LobbyID *uuid.UUID `json:"lobbyID,omitempty"`
Passphrase *string `json:"passphrase,omitempty"` Passphrase *string `json:"passphrase,omitempty"`
} }

View File

@ -22,7 +22,7 @@ func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
} }
} }
func NewEmptyLobbyWithPassphrase()*Lobby { func newEmptyLobbyWithPassphrase() *Lobby {
lobby := NewEmptyLobbyWithUUID(uuid.New()) lobby := NewEmptyLobbyWithUUID(uuid.New())
lobby.Passphrase = utils.NewPassphrase() lobby.Passphrase = utils.NewPassphrase()

View File

@ -24,6 +24,12 @@ func newLobbyRegistry() *LobbyRegistry {
return &LobbyRegistry{lobbies: make(map[uuid.UUID]*Lobby)} 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 { func (r *LobbyRegistry) GetLobbyForPlayer() *Lobby {
for _, lobby := range r.lobbies { for _, lobby := range r.lobbies {
if !lobby.IsFull() { if !lobby.IsFull() {
@ -41,7 +47,7 @@ func (r *LobbyRegistry) GetLobbyByUUID(uuid uuid.UUID) *Lobby {
} }
func (r *LobbyRegistry) GetLobbyByPassphrase(p utils.Passphrase) *Lobby { func (r *LobbyRegistry) GetLobbyByPassphrase(p utils.Passphrase) *Lobby {
for _,lobby := range r.lobbies { for _, lobby := range r.lobbies {
if lobby.Passphrase == p { if lobby.Passphrase == p {
return lobby return lobby
} }

28
main.go
View File

@ -33,7 +33,7 @@ func main() {
router.GET("/api/random", registerForRandomGame) router.GET("/api/random", registerForRandomGame)
router.GET("/api/hostPrivate", hostPrivateGame) router.GET("/api/hostPrivate", hostPrivateGame)
router.GET("/api/joinPrivate", joinPrivateGame) router.POST("/api/joinPrivate", joinPrivateGame)
router.GET("/api/ws", registerWebSocketConnection) router.GET("/api/ws", registerWebSocketConnection)
if hostname == "mbook" { if hostname == "mbook" {
@ -59,8 +59,8 @@ func registerForRandomGame(c *gin.Context) {
mut.Unlock() mut.Unlock()
info := api.PlayerInfo{ info := api.PlayerInfo{
PlayerID: player.Uuid, PlayerID: &player.Uuid,
LobbyID: lobby.Uuid, LobbyID: &lobby.Uuid,
} }
log.Println("responding with info ", info) log.Println("responding with info ", info)
@ -73,25 +73,26 @@ func hostPrivateGame(c *gin.Context) {
u := usher.GetUsher() u := usher.GetUsher()
mut.Lock() mut.Lock()
lobby := u.FindNewPrivateLobby(player) lobby := u.CreateNewPrivateLobby(player)
u.AddPlayerToLobbyAndStartGameIfFull(player, lobby) u.AddPlayerToLobbyAndStartGameIfFull(player, lobby)
mut.Unlock() mut.Unlock()
passphrase := lobby.Passphrase.String() passphrase := lobby.Passphrase.String()
info := api.PlayerInfo{ info := api.PlayerInfo{
PlayerID: player.Uuid, PlayerID: &player.Uuid,
LobbyID: lobby.Uuid, LobbyID: &lobby.Uuid,
Passphrase: &passphrase, Passphrase: &passphrase,
} }
c.Header("Access-Control-Allow-Origin", "*") c.Header("Access-Control-Allow-Origin", "*")
c.IndentedJSON(http.StatusOK, info) c.IndentedJSON(http.StatusOK, info)
} }
func joinPrivateGame(c *gin.Context) { func joinPrivateGame(c *gin.Context) {
req := api.PlayerInfo{} req := api.PlayerInfo{}
err := c.ShouldBindJSON(req) log.Println("OI, WHERES ME LOGS")
if err!= nil || req.Passphrase == nil || *req.Passphrase == "" { log.Println(c.Request.Body)
err := c.ShouldBindJSON(&req)
if err != nil || req.Passphrase == nil || *req.Passphrase == "" {
c.IndentedJSON(http.StatusNotFound, req) c.IndentedJSON(http.StatusNotFound, req)
} }
@ -109,10 +110,11 @@ func joinPrivateGame(c *gin.Context) {
} }
info := api.PlayerInfo{ info := api.PlayerInfo{
PlayerID: player.Uuid, PlayerID: &player.Uuid,
LobbyID: lobby.Uuid, LobbyID: &lobby.Uuid,
Passphrase: req.Passphrase, Passphrase: req.Passphrase,
} }
c.Header("Access-Control-Allow-Origin", "*")
c.IndentedJSON(http.StatusOK, info) c.IndentedJSON(http.StatusOK, info)
} }
@ -145,8 +147,8 @@ func waitForAndHandlePlayerID(ctx context.Context, conn *websocket.Conn) {
return return
} }
lobby := lobbies.GetLobbyRegistry().GetLobbyByUUID(info.LobbyID) lobby := lobbies.GetLobbyRegistry().GetLobbyByUUID(*info.LobbyID)
player, found := lobby.GetPlayerByUUID(info.PlayerID) player, found := lobby.GetPlayerByUUID(*info.PlayerID)
if !found { if !found {
conn.Close(websocket.StatusCode(400), "player not found") conn.Close(websocket.StatusCode(400), "player not found")
return return

View File

@ -27,13 +27,17 @@ func (u *Usher) WelcomeNewPlayer(player *chess.Player) *lobbies.Lobby {
return lobby return lobby
} }
func (u*Usher) FindNewPrivateLobby(player *chess.Player) *lobbies.Lobby { func (u *Usher) CreateNewPrivateLobby(player *chess.Player) *lobbies.Lobby {
lobby := lobbies.NewEmptyLobbyWithPassphrase() lobby := lobbies.GetLobbyRegistry().CreateNewPrivateLobby()
return lobby return lobby
} }
func (u *Usher) FindExistingPrivateLobby(p utils.Passphrase) *lobbies.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) { func (u *Usher) AddPlayerToLobbyAndStartGameIfFull(player *chess.Player, lobby *lobbies.Lobby) {