package realarena import ( "context" "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/redis/pipe" "go_dreamfactory/modules" "go_dreamfactory/sys/db" "github.com/go-redis/redis/v8" ) // /竞技场 数据组件 type modelRank struct { modules.MCompModel module *RealArena } // 组件初始化接口 func (this *modelRank) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) { this.TableName = comm.TableArenaRank this.MCompModel.Init(service, module, comp, opt) this.module = module.(*RealArena) return } func (this *modelRank) rankey() string { return fmt.Sprintf("%s-%s", this.DBModel.ServiceId, this.TableName) } // 更新排名 func (this *modelRank) updateArenaRank(uid string, integral int32) (rank int64, err error) { var ( pipe *pipe.RedisPipe = this.DBModel.Redis.RedisPipe(context.TODO()) cmd *redis.IntCmd menbersCmd *redis.IntCmd ) if cmd = pipe.ZAdd(this.rankey(), &redis.Z{Score: float64(integral), Member: uid}); err != nil { this.module.Errorln(err) } menbersCmd = pipe.ZRevRank(this.rankey(), uid) if _, err = pipe.Exec(); err != nil { this.module.Errorln(err) return } if _, err = cmd.Result(); err != nil { this.module.Errorln(err) return } if rank, err = menbersCmd.Result(); err != nil { this.module.Errorln(err) return } rank = rank + 1 return } //获取指定区间的玩家人数 func (this *modelRank) queryRankByScoreArea(min, max int32) (count int64, err error) { if count, err = this.DBModel.Redis.ZCount(this.rankey(), fmt.Sprintln(min), fmt.Sprintln(max)); err != nil { this.module.Errorln(err) return } return } // 获取排行榜前50的用户名单 func (this *modelRank) queryRankUser() (ranks []string, err error) { var ( result []string ) if result, err = this.DBModel.Redis.ZRevRange(this.rankey(), 0, 50).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) raceSettlement() { // if isopen := this.module.modelArena.isInMaintenance(); isopen { this.DBModel.Del(this.rankey(), db.SetDBMgoLog(false)) // } }