mchess-server/utils/passphrase.go
Marco d7c4f28f3a Fix endpoint for getting lobby id from passphrase
Had to add several helpers (e.g. passphrase ones) to make the endpoint
for getting lobby id work.

Moved all handler functions into handler package.

Added test for getting lobby from passphrase.
2024-05-09 22:29:48 +02:00

58 lines
1019 B
Go

package utils
import (
"strings"
"unicode"
)
type Passphrase string
func NewPassphrase() Passphrase {
var phrase string
var word string
var words = 3
//TODO make sure passphrases are unique
for words > 0 {
word = getCleanWord()
phrase = phrase + word + " "
words -= 1
}
return Passphrase(strings.TrimSpace(phrase))
}
func NewPassphraseFromString(s string) Passphrase {
return Passphrase(s)
}
func (p Passphrase) AsURLParam() string {
var result string
phraseAsString := p.String()
segments := strings.Split(phraseAsString, " ")
for _, segment := range segments {
runes := []rune(segment)
runes[0] = unicode.ToUpper(runes[0])
result += string(runes)
}
return result
}
func ConvertToPassphraseWithSpaces(s string) Passphrase {
result := ""
for _, rune := range s {
if unicode.IsUpper(rune) {
result += " "
}
result += strings.ToLower(string(rune))
}
return NewPassphraseFromString(strings.Trim(result, " "))
}
func (p Passphrase) String() string {
return string(p)
}