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