邀請(qǐng)碼生成

游戲社交部分往往需要邀請(qǐng)碼。以下是我的實(shí)現(xiàn)。供參考。其中有一些依賴的框架的特定代碼,但不影響理解邏輯。值得強(qiáng)調(diào)的點(diǎn)是,去掉了幾個(gè)在顯示時(shí)容易混淆的字符。

import "math/rand"

const (
    invitationCodeMinLength = 6
    invitationCodeMaxLength = 10
    // 排除的字符包括:數(shù)字0和1,大寫(xiě)字母D、I和O,小寫(xiě)字母l和o
    invitationCodeChars = `23456789ABCEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz`
)

func GenInvitationCode() (string, err2.AppError) {
    for i := invitationCodeMinLength; i <= invitationCodeMaxLength; i++ {
        for j := 0; j < 3; j++ {
            code := randCodeWithLen(i)
            u, err := isUnique(code)
            if err != nil {
                return "", err
            }
            if u {
                return code, nil
            }
        }
    }
    return "", err2.AppErrorFromProtoEnum(msg.ErrorCode_SERVICE_INTERNAL_ERROR)
}

func randCodeWithLen(length int) string {
    cs := make([]byte, 0, length)
    for i := 0; i < length; i++ {
        n := rand.Intn(len(invitationCodeChars))
        c := invitationCodeChars[n]
        cs = append(cs, c)
    }
    return string(cs)
}

func isUnique(code string) (bool, err2.AppError) {
    // 查詢數(shù)據(jù)庫(kù)
}

下面是單元測(cè)試。

func Test_randCodeWithLen_dup(t *testing.T) {
    a := assert.New(t)
    const count int = 1e8
    codes := make(map[string]struct{}, count)
    var dupCount int
    for j := 0; j < count; j++ {
        var dup bool
        for i := invitationCodeMinLength; i <= invitationCodeMaxLength; i++ {
            code := randCodeWithLen(i)
            a.Equal(i, len(code))
            if _, ok := codes[code]; ok {
                dup = true
            } else {
                dup = false
                codes[code] = struct{}{}
                break
            }
        }
        if dup {
            dupCount++
        }
    }
    t.Logf("count=%d, dupCount=%d", count, dupCount)
}

一億次生成,重復(fù)次數(shù)為0??梢哉f(shuō)明重復(fù)的概率很低。
建表腳本如下。

CREATE TABLE IF NOT EXISTS `invitation_code` (
    `code` VARCHAR(10) NOT NULL,
    `uid` BIGINT UNSIGNED NOT NULL DEFAULT 0,
    PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容