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) disconnectCallback func(p *Player)
} }
func NewPlayer(uuid uuid.UUID) *Player { func NewPlayer(uuid uuid.UUID, opts ...func(*Player)) *Player {
player := &Player{ player := &Player{
Uuid: uuid, Uuid: uuid,
Conn: connection.NewConnection( Conn: connection.NewConnection(
connection.WithContext(context.Background())), connection.WithContext(context.Background())),
} }
for _, opt := range opts {
opt(player)
}
return player return player
} }
@ -140,3 +144,7 @@ func (p *Player) readMessage() ([]byte, error) {
func (p Player) GetPlayerColor() string { func (p Player) GetPlayerColor() string {
return string(p.color) 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 { func (r *LobbyRegistry) GetLobbyByPassphrase(p utils.Passphrase) *Lobby {
if p.IsEmpty() {
return nil
}
for _, lobby := range r.lobbies { for _, lobby := range r.lobbies {
if lobby.Passphrase == p { if lobby.Passphrase == p {
return lobby return lobby

12
main.go
View File

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

View File

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

View File

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