package enchant import ( "context" "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/redis/pipe" "go_dreamfactory/modules" "go_dreamfactory/pb" "github.com/go-redis/redis/v8" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/x/bsonx" ) type ModelRank struct { modules.MCompModel module *Enchant } func (this *ModelRank) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) { this.TableName = comm.TableEnchantRank // 挑战记录 err = this.MCompModel.Init(service, module, comp, options) this.module = module.(*Enchant) this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{ Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}}, }) return } func (this *ModelRank) rankKey(boosType int32) string { return fmt.Sprintf("%s-%s-%d", this.module.service.GetTag(), this.TableName, boosType) } func (this *ModelRank) getEnchantRankList(uid string) []*pb.DBEnchantRank { ranks := make([]*pb.DBEnchantRank, 0) err := this.GetList(uid, &ranks) if err != nil { return nil } return ranks } // 获取排行榜前50的用户名单 func (this *ModelRank) queryRankUser(boos int32) (ranks []string, err error) { var ( result []string ) if result, err = this.DBModel.Redis.ZRevRange(this.rankKey(boos), 0, comm.MaxRankList).Result(); err != nil { this.module.Errorln(err) return } ranks = make([]string, 0) for i := 0; i < len(result); i += 1 { ranks = append(ranks, result[i]) } return } // 更新排名 func (this *ModelRank) updateRank(Score int32, uid string, boos int32) (err error) { var ( pipe *pipe.RedisPipe = this.DBModel.Redis.RedisPipe(context.TODO()) menbers *redis.Z cmd *redis.IntCmd ) menbers = &redis.Z{Score: float64(Score), Member: uid} if cmd = pipe.ZAdd(this.rankKey(boos), menbers); err != nil { this.module.Errorln(err) } if _, err = pipe.Exec(); err != nil { this.module.Errorln(err) return } if _, err = cmd.Result(); err != nil { this.module.Errorln(err) return } return }