diff --git a/api/player_info.go b/api/player_info.go index 1a711ef..be2b054 100644 --- a/api/player_info.go +++ b/api/player_info.go @@ -5,4 +5,5 @@ import "github.com/google/uuid" type PlayerInfo struct { PlayerID uuid.UUID `json:"playerID"` LobbyID uuid.UUID `json:"lobbyID"` + Passphrase *string `json:"passphrase,omitempty"` } diff --git a/chess/player.go b/chess/player.go index 61fa2cf..028fad4 100644 --- a/chess/player.go +++ b/chess/player.go @@ -4,9 +4,9 @@ import ( "context" "encoding/json" "errors" + "log" "mchess_server/api" "mchess_server/types" - "log" "time" "github.com/google/uuid" diff --git a/lobby_registry/lobby.go b/lobby_registry/lobby.go index d090c45..beab692 100644 --- a/lobby_registry/lobby.go +++ b/lobby_registry/lobby.go @@ -11,7 +11,7 @@ type Lobby struct { Uuid uuid.UUID Game *chess.Game PlayerJoined chan bool - Passphrase string + Passphrase utils.Passphrase } func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby { @@ -24,7 +24,7 @@ func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby { func NewEmptyLobbyWithPassphrase()*Lobby { lobby := NewEmptyLobbyWithUUID(uuid.New()) - lobby.Passphrase = string(utils.NewPassphrase()) + lobby.Passphrase = utils.NewPassphrase() return lobby } diff --git a/lobby_registry/registry.go b/lobby_registry/registry.go index 954c0ee..4e001d6 100644 --- a/lobby_registry/registry.go +++ b/lobby_registry/registry.go @@ -1,6 +1,8 @@ package lobby_registry import ( + "mchess_server/utils" + "github.com/google/uuid" ) @@ -38,6 +40,15 @@ func (r *LobbyRegistry) GetLobbyByUUID(uuid uuid.UUID) *Lobby { return r.lobbies[uuid] } +func (r *LobbyRegistry) GetLobbyByPassphrase(p utils.Passphrase) *Lobby { + for _,lobby := range r.lobbies { + if lobby.Passphrase == p { + return lobby + } + } + return nil +} + func (r *LobbyRegistry) addNewLobby(lobby *Lobby) uuid.UUID { r.lobbies[lobby.Uuid] = lobby return lobby.Uuid diff --git a/main.go b/main.go index d809267..86d7730 100644 --- a/main.go +++ b/main.go @@ -4,11 +4,12 @@ import ( "context" "encoding/json" "fmt" + "log" "mchess_server/api" "mchess_server/chess" lobbies "mchess_server/lobby_registry" "mchess_server/usher" - "log" + "mchess_server/utils" "net/http" "os" "sync" @@ -68,18 +69,51 @@ func registerForRandomGame(c *gin.Context) { } func hostPrivateGame(c *gin.Context) { - player := chess.NewPlayer(uuid.New()) - usher.GetUsher() + player := chess.NewPlayer(uuid.New()) + u := usher.GetUsher() - mut.Lock() - lobby := usher.NewUsher().FindNewPrivateLobby(player) - mut.Unlock() + mut.Lock() + lobby := u.FindNewPrivateLobby(player) + u.AddPlayerToLobbyAndStartGameIfFull(player, lobby) + mut.Unlock() - c.IndentedJSON(http.StatusOK, lobby.Passphrase) + passphrase := lobby.Passphrase.String() + info := api.PlayerInfo{ + PlayerID: player.Uuid, + LobbyID: lobby.Uuid, + Passphrase: &passphrase, + } + + c.Header("Access-Control-Allow-Origin", "*") + c.IndentedJSON(http.StatusOK, info) } func joinPrivateGame(c *gin.Context) { + req := api.PlayerInfo{} + err := c.ShouldBindJSON(req) + if err!= nil || req.Passphrase == nil || *req.Passphrase == "" { + c.IndentedJSON(http.StatusNotFound, req) + } + player := chess.NewPlayer(uuid.New()) + u := usher.GetUsher() + + mut.Lock() + defer mut.Unlock() + lobby := u.FindExistingPrivateLobby(utils.Passphrase(*req.Passphrase)) + if lobby != nil { + u.AddPlayerToLobbyAndStartGameIfFull(player, lobby) + } else { + c.IndentedJSON(http.StatusNotFound, req) + return + } + + info := api.PlayerInfo{ + PlayerID: player.Uuid, + LobbyID: lobby.Uuid, + Passphrase: req.Passphrase, + } + c.IndentedJSON(http.StatusOK, info) } func registerWebSocketConnection(c *gin.Context) { diff --git a/usher/usher.go b/usher/usher.go index 2110b93..4705bc6 100644 --- a/usher/usher.go +++ b/usher/usher.go @@ -3,6 +3,7 @@ package usher import ( "mchess_server/chess" lobbies "mchess_server/lobby_registry" + "mchess_server/utils" ) type Usher struct { @@ -10,13 +11,13 @@ type Usher struct { var instance *Usher -func NewUsher() *Usher { +func newUsher() *Usher { return &Usher{} } func GetUsher() *Usher { if instance == nil { - instance = NewUsher() + instance = newUsher() } return instance } @@ -31,6 +32,10 @@ func (u*Usher) FindNewPrivateLobby(player *chess.Player) *lobbies.Lobby { return lobby } +func (u *Usher) FindExistingPrivateLobby(p utils.Passphrase) *lobbies.Lobby { + return lobbies.GetLobbyRegistry().GetLobbyByPassphrase(p) +} + func (u *Usher) AddPlayerToLobbyAndStartGameIfFull(player *chess.Player, lobby *lobbies.Lobby) { lobby.AddPlayerAndStartGameIfFull(player) } diff --git a/utils/passphrase.go b/utils/passphrase.go index 9a175ee..272a084 100644 --- a/utils/passphrase.go +++ b/utils/passphrase.go @@ -12,8 +12,8 @@ func NewPassphrase() Passphrase { var phrase string var retries int var word string - var words int = 3 - + var words int = 2 +//TODO make sure passphrases are unique for words > 0 { retries = 20 for { @@ -53,7 +53,7 @@ func isAccecpable(s string) bool { func isProfanity(s string) bool { contains := []string{ "nigg", - "fagg", + "fag", } startsWith := []string{ "spic",