| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package redis
- import (
- "f1-game/internal/data"
- nameLua "f1-game/internal/name/lua"
- "f1-game/internal/pb"
- "fmt"
- cstring "github.com/cherry-game/cherry/extend/string"
- clog "github.com/cherry-game/cherry/logger"
- cprofile "github.com/cherry-game/cherry/profile"
- )
- type (
- center struct {
- *client
- bindUIDKey string // 根据openid获取uid
- lastLoginKey string // type:hash 根据pid获取玩家最后登录列表
- }
- )
- func newCenter() *center {
- return ¢er{}
- }
- func (p *center) setKey() {
- prefix := fmt.Sprintf("%s-center:", cprofile.Env())
- p.bindUIDKey = prefix + "bind-uid:%d" // type = hash key = bind-uid:{pid} field = openID value = uid
- p.lastLoginKey = prefix + "last-login:%d" // center:last-login:{pid} field:openid
- }
- func (p *center) ConfigID() string {
- return "center_redis_id"
- }
- func (p *center) load(client *client) {
- p.client = client
- p.setKey()
- }
- func (p *center) GetUser(pid int32, openID string) int64 {
- uidKey := p.buildBindUIDKey(pid)
- val, _ := p.HGetString(uidKey, openID)
- if cstring.IsBlank(val) {
- return -1
- }
- return cstring.ToInt64D(val)
- }
- func (p *center) BindUser(pid int32, openID string) (int64, bool) {
- if cstring.IsBlank(openID) {
- clog.Warnf("[BindUser] OpenID is error. openID = %v", openID)
- return 0, false
- }
- if pid < 1 {
- clog.Warnf("[BindUser] PID is error. pid = %v", pid)
- return 0, false
- }
- script, found := data.Lua.Get(nameLua.Bind_Uid)
- if !found {
- return 0, false
- }
- var (
- bindUidKey = p.buildBindUIDKey(pid)
- )
- cmd := script.Run(p.Context(), p.Client, []string{GUID.uidKey, bindUidKey}, openID)
- result, err := cmd.Result()
- if err != nil {
- clog.Warnf("[BindUser] error. err = %v", err.Error())
- return 0, false
- }
- return result.(int64), true
- }
- func (p *center) buildBindUIDKey(pid int32) string {
- return fmt.Sprintf(p.bindUIDKey, pid)
- }
- func (p *center) GetLastLoginPlayer(pid int32, openID string) []*pb.LastPlayer {
- key := p.BuildLastLoginKey(pid)
- var list []*pb.LastPlayer
- err := p.HGetJSON(key, openID, &list)
- if err != nil {
- clog.Warnf("pid = %d, openID = %s, err = %v", pid, openID, err)
- }
- return list
- }
- func (p *center) BuildLastLoginKey(pid int32) string {
- return fmt.Sprintf(p.lastLoginKey, pid)
- }
|