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"`
Passphrase *string `json:"passphrase,omitempty"`
PlayerID *uuid.UUID `json:"playerID,omitempty"`
LobbyID *uuid.UUID `json:"lobbyID,omitempty"`
Passphrase *string `json:"passphrase,omitempty"`
}

View File

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

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

64
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,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

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