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

@ -8,10 +8,10 @@ import (
) )
type Lobby struct { type Lobby struct {
Uuid uuid.UUID Uuid uuid.UUID
Game *chess.Game Game *chess.Game
PlayerJoined chan bool PlayerJoined chan bool
Passphrase utils.Passphrase Passphrase utils.Passphrase
} }
func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby { func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
@ -22,11 +22,11 @@ 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()
return lobby return lobby
} }
func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) { func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) {

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,12 +47,12 @@ 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
} }
} }
return nil return nil
} }
func (r *LobbyRegistry) addNewLobby(lobby *Lobby) uuid.UUID { 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/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,47 +73,49 @@ 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)
c.IndentedJSON(http.StatusNotFound, req) 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()
player := chess.NewPlayer(uuid.New())
u := usher.GetUsher()
mut.Lock() mut.Lock()
defer mut.Unlock() defer mut.Unlock()
lobby := u.FindExistingPrivateLobby(utils.Passphrase(*req.Passphrase)) lobby := u.FindExistingPrivateLobby(utils.Passphrase(*req.Passphrase))
if lobby != nil { if lobby != nil {
u.AddPlayerToLobbyAndStartGameIfFull(player, lobby) u.AddPlayerToLobbyAndStartGameIfFull(player, lobby)
} else { } else {
c.IndentedJSON(http.StatusNotFound, req) c.IndentedJSON(http.StatusNotFound, req)
return return
} }
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.IndentedJSON(http.StatusOK, info) c.Header("Access-Control-Allow-Origin", "*")
c.IndentedJSON(http.StatusOK, info)
} }
func registerWebSocketConnection(c *gin.Context) { func registerWebSocketConnection(c *gin.Context) {
@ -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) {