diff --git a/README b/README new file mode 100644 index 0000000..fc95920 --- /dev/null +++ b/README @@ -0,0 +1,3 @@ +Prerequisites: + 1. Install a word file: + the server uses babbler that expects a word list at /usr/share/dict/words in order to generate the passphrases for lobbies. diff --git a/go.mod b/go.mod index ba7720c..9690755 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/gin-gonic/autotls v0.0.5 github.com/gin-gonic/gin v1.9.0 github.com/google/uuid v1.3.0 + github.com/samber/lo v1.38.1 nhooyr.io/websocket v1.8.7 ) @@ -27,9 +28,9 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/samber/lo v1.38.1 // indirect github.com/stretchr/objx v0.5.0 // indirect github.com/stretchr/testify v1.8.4 // indirect + github.com/tjarratt/babble v0.0.0-20210505082055-cbca2a4833c1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.9 // indirect golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect diff --git a/go.sum b/go.sum index 181c285..33955e2 100644 --- a/go.sum +++ b/go.sum @@ -86,6 +86,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/tjarratt/babble v0.0.0-20210505082055-cbca2a4833c1 h1:j8whCiEmvLCXI3scVn+YnklCU8mwJ9ZJ4/DGAKqQbRE= +github.com/tjarratt/babble v0.0.0-20210505082055-cbca2a4833c1/go.mod h1:O5hBrCGqzfb+8WyY8ico2AyQau7XQwAfEQeEQ5/5V9E= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= diff --git a/lobby_registry/lobby.go b/lobby_registry/lobby.go index 3edb0a8..d090c45 100644 --- a/lobby_registry/lobby.go +++ b/lobby_registry/lobby.go @@ -2,16 +2,16 @@ package lobby_registry import ( "mchess_server/chess" + "mchess_server/utils" "github.com/google/uuid" ) type Lobby struct { Uuid uuid.UUID - Game *chess.Game - PlayerJoined chan bool + Passphrase string } func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby { @@ -22,6 +22,13 @@ func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby { } } +func NewEmptyLobbyWithPassphrase()*Lobby { + lobby := NewEmptyLobbyWithUUID(uuid.New()) + lobby.Passphrase = string(utils.NewPassphrase()) + + return lobby +} + func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) { l.Game.AddPlayersToGame(player) if l.IsFull() { diff --git a/main.go b/main.go index 4f91fa8..d809267 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,8 @@ func main() { router := gin.Default() router.GET("/api/random", registerForRandomGame) + router.GET("/api/hostPrivate", hostPrivateGame) + router.GET("/api/joinPrivate", joinPrivateGame) router.GET("/api/ws", registerWebSocketConnection) if hostname == "mbook" { @@ -65,6 +67,21 @@ func registerForRandomGame(c *gin.Context) { c.IndentedJSON(http.StatusOK, info) } +func hostPrivateGame(c *gin.Context) { + player := chess.NewPlayer(uuid.New()) + usher.GetUsher() + + mut.Lock() + lobby := usher.NewUsher().FindNewPrivateLobby(player) + mut.Unlock() + + c.IndentedJSON(http.StatusOK, lobby.Passphrase) +} + +func joinPrivateGame(c *gin.Context) { + +} + func registerWebSocketConnection(c *gin.Context) { webSocketConn, err := websocket.Accept(c.Writer, c.Request, &websocket.AcceptOptions{OriginPatterns: []string{"chess.sw-gross.de", "localhost:*"}}) if err != nil { diff --git a/usher/usher.go b/usher/usher.go index 6a90b64..2110b93 100644 --- a/usher/usher.go +++ b/usher/usher.go @@ -26,6 +26,11 @@ func (u *Usher) WelcomeNewPlayer(player *chess.Player) *lobbies.Lobby { return lobby } +func (u*Usher) FindNewPrivateLobby(player *chess.Player) *lobbies.Lobby { + lobby := lobbies.NewEmptyLobbyWithPassphrase() + return lobby +} + func (u *Usher) AddPlayerToLobbyAndStartGameIfFull(player *chess.Player, lobby *lobbies.Lobby) { lobby.AddPlayerAndStartGameIfFull(player) } diff --git a/utils/passphrase.go b/utils/passphrase.go new file mode 100644 index 0000000..9a175ee --- /dev/null +++ b/utils/passphrase.go @@ -0,0 +1,96 @@ +package utils + +import ( + "strings" + + "github.com/tjarratt/babble" +) + +type Passphrase string + +func NewPassphrase() Passphrase { + var phrase string + var retries int + var word string + var words int = 3 + + for words > 0 { + retries = 20 + for { + word = getCleanWord() + if isAccecpable(word) { + phrase = phrase + word + " " + break + } + if retries == 1 { //this is our last try, we take any word + phrase = phrase + word + " " + } + retries -= 1 + } + words -= 1 + } + return Passphrase(strings.TrimSpace(phrase)) +} + +func (p Passphrase) String() string { + return string(p) +} + +func isAccecpable(s string) bool { + l := len(s) + if l > 8 || l < 3 { + return false + } + + for _, rune := range s { + if !isEnglishLetter(rune) { + return false + } + } + return true +} + +func isProfanity(s string) bool { + contains := []string{ + "nigg", + "fagg", + } + startsWith := []string{ + "spic", + "chin", + "cunt", + } + + for _, word := range contains { + if strings.Contains(s, word) { + return true + } + } + for _, word := range startsWith { + if strings.HasPrefix(s, word) { + return true + } + } + return false +} + +func isEnglishLetter(r rune) bool { + if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') { + return false + } + return true +} + +func getCleanWord() string { + var word string + + babbler := babble.NewBabbler() + babbler.Count = 1 + + for { + word = babbler.Babble() + if !isProfanity(word) { + return word + } + } +}