wip check if connecting player exists in lobby

This commit is contained in:
Marco 2023-12-30 12:55:59 +01:00
parent 946f4b632a
commit e478bf46fc
5 changed files with 26 additions and 12 deletions

View File

@ -20,13 +20,17 @@ type Player struct {
disconnectCallback func(p *Player)
}
func NewPlayer(uuid uuid.UUID) *Player {
func NewPlayer(uuid uuid.UUID, opts ...func(*Player)) *Player {
player := &Player{
Uuid: uuid,
Conn: connection.NewConnection(
connection.WithContext(context.Background())),
}
for _, opt := range opts {
opt(player)
}
return player
}
@ -140,3 +144,7 @@ func (p *Player) readMessage() ([]byte, error) {
func (p Player) GetPlayerColor() string {
return string(p.color)
}
func (p Player) IsEqualTo(other *Player) bool {
return p.Uuid.ID() == other.Uuid.ID()
}

View File

@ -47,6 +47,10 @@ func (r *LobbyRegistry) GetLobbyByUUID(uuid uuid.UUID) *Lobby {
}
func (r *LobbyRegistry) GetLobbyByPassphrase(p utils.Passphrase) *Lobby {
if p.IsEmpty() {
return nil
}
for _, lobby := range r.lobbies {
if lobby.Passphrase == p {
return lobby

12
main.go
View File

@ -93,17 +93,19 @@ func joinPrivateGame(c *gin.Context) {
req := api.PlayerInfo{}
log.Println(c.Request.Body)
err := c.ShouldBindJSON(&req)
if err != nil || req.Passphrase == nil || *req.Passphrase == "" {
c.IndentedJSON(http.StatusNotFound, req)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, req)
}
connectingPlayer := chess.NewPlayer(*req.PlayerID)
player := chess.NewPlayer(uuid.New())
u := usher.GetUsher()
if req.Passphrase != nil &&
*req.Passphrase != "" &&
req.PlayerID != nil &&
req.LobbyID != nil { //is reconnect
lobby := u.FindExistingPrivateLobby(utils.Passphrase(*req.Passphrase))
lobby := u.FindExistingPrivateLobbyByID(utils.Passphrase(*req.Passphrase))
_, found := lobby.GetPlayerByUUID(*req.PlayerID)
if found {
c.IndentedJSON(
@ -119,11 +121,11 @@ func joinPrivateGame(c *gin.Context) {
}
}
player := chess.NewPlayer(uuid.New())
player = chess.NewPlayer(uuid.New())
mut.Lock()
defer mut.Unlock()
lobby := u.FindExistingPrivateLobby(utils.Passphrase(*req.Passphrase))
lobby := u.FindExistingPrivateLobbyByID(utils.Passphrase(*req.Passphrase))
if lobby != nil {
u.AddPlayerToLobbyAndStartGameIfFull(player, lobby)
} else {

View File

@ -32,12 +32,8 @@ func (u *Usher) CreateNewPrivateLobby(player *chess.Player) *lobbies.Lobby {
return lobby
}
func (u *Usher) FindExistingPrivateLobby(p utils.Passphrase) *lobbies.Lobby {
lobby := lobbies.GetLobbyRegistry().GetLobbyByPassphrase(p)
if lobby == nil || lobby.IsFull() {
return nil
}
return lobby
func (u *Usher) FindExistingPrivateLobbyByID(p utils.Passphrase) *lobbies.Lobby {
return lobbies.GetLobbyRegistry().GetLobbyByPassphrase(p)
}
func (u *Usher) AddPlayerToLobbyAndStartGameIfFull(player *chess.Player, lobby *lobbies.Lobby) {

View File

@ -36,6 +36,10 @@ func (p Passphrase) String() string {
return string(p)
}
func (p Passphrase) IsEmpty() bool {
return p.String() == ""
}
func isAccecpable(s string) bool {
l := len(s)
if l > 8 || l < 3 {