feat: game lobby service
This commit is contained in:
@@ -13,8 +13,16 @@ import (
|
||||
|
||||
var base32NoPadding = base32.StdEncoding.WithPadding(base32.NoPadding)
|
||||
|
||||
// userNameSuffixAlphabet is the Crockford lowercase Base32 alphabet with
|
||||
// `i`, `l`, `o`, and `u` excluded to avoid visual confusables. The chosen
|
||||
// 32 characters also keep each byte pair aligned with a 5-bit group so the
|
||||
// 5-byte random source encodes into exactly eight suffix characters.
|
||||
const userNameSuffixAlphabet = "0123456789abcdefghjkmnpqrstvwxyz"
|
||||
|
||||
const userNameSuffixLength = 8
|
||||
|
||||
// IDGenerator creates opaque stable user identifiers and generated initial
|
||||
// race names.
|
||||
// user names.
|
||||
type IDGenerator struct{}
|
||||
|
||||
// NewUserID returns one newly generated opaque user identifier.
|
||||
@@ -32,20 +40,21 @@ func (IDGenerator) NewUserID() (common.UserID, error) {
|
||||
return userID, nil
|
||||
}
|
||||
|
||||
// NewInitialRaceName returns one generated race name in the `player-<shortid>`
|
||||
// form.
|
||||
func (IDGenerator) NewInitialRaceName() (common.RaceName, error) {
|
||||
token, err := randomToken(5)
|
||||
// NewUserName returns one generated user name in the `player-<suffix>` form.
|
||||
// The suffix is eight characters drawn from the Crockford lowercase Base32
|
||||
// alphabet (confusable-free: `i`, `l`, `o`, `u` are excluded).
|
||||
func (IDGenerator) NewUserName() (common.UserName, error) {
|
||||
suffix, err := randomSuffix(userNameSuffixLength)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("generate initial race name: %w", err)
|
||||
return "", fmt.Errorf("generate user name: %w", err)
|
||||
}
|
||||
|
||||
raceName := common.RaceName("player-" + token)
|
||||
if err := raceName.Validate(); err != nil {
|
||||
return "", fmt.Errorf("generate initial race name: %w", err)
|
||||
userName := common.UserName("player-" + suffix)
|
||||
if err := userName.Validate(); err != nil {
|
||||
return "", fmt.Errorf("generate user name: %w", err)
|
||||
}
|
||||
|
||||
return raceName, nil
|
||||
return userName, nil
|
||||
}
|
||||
|
||||
// NewEntitlementRecordID returns one generated entitlement history record
|
||||
@@ -103,3 +112,31 @@ func randomToken(size int) (string, error) {
|
||||
|
||||
return strings.ToLower(base32NoPadding.EncodeToString(buffer)), nil
|
||||
}
|
||||
|
||||
// randomSuffix returns a length-character suffix encoded from crypto-random
|
||||
// bytes through the userNameSuffixAlphabet. Each character consumes five
|
||||
// random bits, so the caller receives `ceil(length * 5 / 8)` bytes of
|
||||
// entropy in the underlying buffer.
|
||||
func randomSuffix(length int) (string, error) {
|
||||
byteCount := (length*5 + 7) / 8
|
||||
buffer := make([]byte, byteCount)
|
||||
if _, err := rand.Read(buffer); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
encoded := make([]byte, length)
|
||||
for index := range encoded {
|
||||
bitOffset := index * 5
|
||||
byteIndex := bitOffset / 8
|
||||
shift := bitOffset % 8
|
||||
|
||||
value := uint16(buffer[byteIndex]) << 8
|
||||
if byteIndex+1 < len(buffer) {
|
||||
value |= uint16(buffer[byteIndex+1])
|
||||
}
|
||||
|
||||
encoded[index] = userNameSuffixAlphabet[(value>>(16-5-shift))&0x1F]
|
||||
}
|
||||
|
||||
return string(encoded), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user