package redis import ( "f1-game/internal/data" nameLua "f1-game/internal/name/lua" "fmt" clog "github.com/cherry-game/cherry/logger" cprofile "github.com/cherry-game/cherry/profile" ) const ( guidInitValue = 1048576 uidInitValue = 1048576 chatInitValue = 1024 leagueInitValue = 1874 ) type ( guid struct { *client guidKey string // 全局 ID uidKey string // 全局 uid chatIDKey string // 全局聊天室唯一ID leagueIDKey string // 全局联盟唯一ID } ) func newGUID() *guid { return &guid{} } func (p *guid) setKey() { prefix := fmt.Sprintf("%s-guid:", cprofile.Env()) p.guidKey = prefix + "guid" // type = string key = guid p.uidKey = prefix + "uid" // type = string key = uid p.chatIDKey = prefix + "chat-id" // type = string key = chat-id p.leagueIDKey = prefix + "league-id" // type = string key = league-id } func (p *guid) ConfigID() string { return "guid_redis_id" } func (p *guid) load(client *client) { p.client = client p.setKey() // 检查 guid key p.checkKey() } func (p *guid) checkKey() { current := p.checkGUID(p.guidKey, guidInitValue) clog.Infof("check guid. [current = %d]", current) current = p.checkGUID(p.uidKey, uidInitValue) clog.Infof("check uid. [current = %d]", current) current = p.checkGUID(p.chatIDKey, chatInitValue) clog.Infof("check chatid. [current = %d]", current) current = p.checkGUID(p.leagueIDKey, leagueInitValue) clog.Infof("check leagueid. [current = %d]", current) } func (p *guid) NewGUID() int64 { intCmd := p.Incr(p.Context(), p.guidKey) return intCmd.Val() } func (p *guid) NewUID() int64 { intCmd := p.Incr(p.Context(), p.uidKey) return intCmd.Val() } func (p *guid) NewChatID() int64 { intCmd := p.Incr(p.Context(), p.chatIDKey) return intCmd.Val() } func (p *guid) NewLeagueID() int64 { intCmd := p.Incr(p.Context(), p.leagueIDKey) return intCmd.Val() } func (p *guid) checkGUID(key string, initValue int64) int64 { script, found := data.Lua.Get(nameLua.Check_Guid) if !found { return 0 } cmd := script.Run(p.Context(), p.client, []string{key}, initValue) result, err := cmd.Result() if err != nil { clog.Warnf("checkGUID error. key = %s, initValue = %d, err = %v", key, initValue, err.Error()) return 0 } return result.(int64) }