Compare commits
2 Commits
master
...
check-if-p
Author | SHA1 | Date | |
---|---|---|---|
8aa5cd88ef | |||
e478bf46fc |
@ -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()
|
||||
}
|
||||
|
@ -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
|
||||
|
14
main.go
14
main.go
@ -93,18 +93,20 @@ 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))
|
||||
_, found := lobby.GetPlayerByUUID(*req.PlayerID)
|
||||
lobby := u.FindExistingPrivateLobbyByID(utils.Passphrase(*req.Passphrase))
|
||||
_, found := lobby.GetPlayerByUUID(connectingPlayer.Uuid)
|
||||
if found {
|
||||
c.IndentedJSON(
|
||||
http.StatusOK,
|
||||
@ -119,11 +121,9 @@ func joinPrivateGame(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
@ -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) {
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user