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"
type PlayerInfo struct {
PlayerID uuid.UUID `json:"playerID"`
LobbyID uuid.UUID `json:"lobbyID"`
PlayerID *uuid.UUID `json:"playerID,omitempty"`
LobbyID *uuid.UUID `json:"lobbyID,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.Passphrase = utils.NewPassphrase()

View File

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

26
main.go
View File

@ -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,24 +73,25 @@ func hostPrivateGame(c *gin.Context) {
u := usher.GetUsher()
mut.Lock()
lobby := u.FindNewPrivateLobby(player)
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)
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)
}
@ -109,10 +110,11 @@ func joinPrivateGame(c *gin.Context) {
}
info := api.PlayerInfo{
PlayerID: player.Uuid,
LobbyID: lobby.Uuid,
PlayerID: &player.Uuid,
LobbyID: &lobby.Uuid,
Passphrase: req.Passphrase,
}
c.Header("Access-Control-Allow-Origin", "*")
c.IndentedJSON(http.StatusOK, info)
}
@ -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

View File

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