diff --git a/chess/player.go b/chess/player.go index 28d2b57..6de7e29 100644 --- a/chess/player.go +++ b/chess/player.go @@ -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() +} diff --git a/lobby_registry/registry.go b/lobby_registry/registry.go index 1412462..280c89d 100644 --- a/lobby_registry/registry.go +++ b/lobby_registry/registry.go @@ -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 diff --git a/main.go b/main.go index a2432ef..cd3f2ac 100644 --- a/main.go +++ b/main.go @@ -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 { diff --git a/usher/usher.go b/usher/usher.go index cfbbd51..3d8ac43 100644 --- a/usher/usher.go +++ b/usher/usher.go @@ -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) { diff --git a/utils/passphrase.go b/utils/passphrase.go index 4f54306..8424f3d 100644 --- a/utils/passphrase.go +++ b/utils/passphrase.go @@ -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 {