Add mechanic for passphrases.
This commit is contained in:
parent
63e973a3be
commit
1ea2c597be
3
README
Normal file
3
README
Normal file
@ -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.
|
3
go.mod
3
go.mod
@ -6,6 +6,7 @@ require (
|
|||||||
github.com/gin-gonic/autotls v0.0.5
|
github.com/gin-gonic/autotls v0.0.5
|
||||||
github.com/gin-gonic/gin v1.9.0
|
github.com/gin-gonic/gin v1.9.0
|
||||||
github.com/google/uuid v1.3.0
|
github.com/google/uuid v1.3.0
|
||||||
|
github.com/samber/lo v1.38.1
|
||||||
nhooyr.io/websocket v1.8.7
|
nhooyr.io/websocket v1.8.7
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -27,9 +28,9 @@ require (
|
|||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
|
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // 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/objx v0.5.0 // indirect
|
||||||
github.com/stretchr/testify v1.8.4 // 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/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||||
github.com/ugorji/go/codec v1.2.9 // indirect
|
github.com/ugorji/go/codec v1.2.9 // indirect
|
||||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
|
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
|
||||||
|
2
go.sum
2
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.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
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/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 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
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=
|
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
||||||
|
@ -2,16 +2,16 @@ package lobby_registry
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"mchess_server/chess"
|
"mchess_server/chess"
|
||||||
|
"mchess_server/utils"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Lobby struct {
|
type Lobby struct {
|
||||||
Uuid uuid.UUID
|
Uuid uuid.UUID
|
||||||
|
|
||||||
Game *chess.Game
|
Game *chess.Game
|
||||||
|
|
||||||
PlayerJoined chan bool
|
PlayerJoined chan bool
|
||||||
|
Passphrase string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEmptyLobbyWithUUID(uuid uuid.UUID) *Lobby {
|
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) {
|
func (l *Lobby) AddPlayerAndStartGameIfFull(player *chess.Player) {
|
||||||
l.Game.AddPlayersToGame(player)
|
l.Game.AddPlayersToGame(player)
|
||||||
if l.IsFull() {
|
if l.IsFull() {
|
||||||
|
17
main.go
17
main.go
@ -31,6 +31,8 @@ func main() {
|
|||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
router.GET("/api/random", registerForRandomGame)
|
router.GET("/api/random", registerForRandomGame)
|
||||||
|
router.GET("/api/hostPrivate", hostPrivateGame)
|
||||||
|
router.GET("/api/joinPrivate", joinPrivateGame)
|
||||||
router.GET("/api/ws", registerWebSocketConnection)
|
router.GET("/api/ws", registerWebSocketConnection)
|
||||||
|
|
||||||
if hostname == "mbook" {
|
if hostname == "mbook" {
|
||||||
@ -65,6 +67,21 @@ func registerForRandomGame(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusOK, info)
|
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) {
|
func registerWebSocketConnection(c *gin.Context) {
|
||||||
webSocketConn, err := websocket.Accept(c.Writer, c.Request, &websocket.AcceptOptions{OriginPatterns: []string{"chess.sw-gross.de", "localhost:*"}})
|
webSocketConn, err := websocket.Accept(c.Writer, c.Request, &websocket.AcceptOptions{OriginPatterns: []string{"chess.sw-gross.de", "localhost:*"}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -26,6 +26,11 @@ func (u *Usher) WelcomeNewPlayer(player *chess.Player) *lobbies.Lobby {
|
|||||||
return 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) {
|
func (u *Usher) AddPlayerToLobbyAndStartGameIfFull(player *chess.Player, lobby *lobbies.Lobby) {
|
||||||
lobby.AddPlayerAndStartGameIfFull(player)
|
lobby.AddPlayerAndStartGameIfFull(player)
|
||||||
}
|
}
|
||||||
|
96
utils/passphrase.go
Normal file
96
utils/passphrase.go
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user