84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
package entertainment
|
|
|
|
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"
|
|
)
|
|
|
|
type modelRank struct {
|
|
modules.MCompModel
|
|
module *Entertainment
|
|
}
|
|
|
|
// 组件初始化接口
|
|
func (this *modelRank) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
|
|
this.TableName = comm.TableEntertainRank
|
|
this.MCompModel.Init(service, module, comp, opt)
|
|
this.module = module.(*Entertainment)
|
|
|
|
return
|
|
}
|
|
|
|
// 更新排名
|
|
func (this *modelRank) updateXxlRank(users ...*pb.XxlPlayer) (err error) {
|
|
var (
|
|
pipe *pipe.RedisPipe = this.DBModel.Redis.RedisPipe(context.TODO())
|
|
menbers []*redis.Z
|
|
cmd *redis.IntCmd
|
|
menbersCmd []*redis.IntCmd = make([]*redis.IntCmd, len(users))
|
|
rank int64
|
|
)
|
|
if len(users) == 0 {
|
|
return
|
|
}
|
|
menbers = make([]*redis.Z, len(users))
|
|
for i, v := range users {
|
|
menbers[i] = &redis.Z{Score: float64(v.Consumeexp), Member: v.Uinfo.Uid}
|
|
}
|
|
if cmd = pipe.ZAdd(this.TableName, menbers...); err != nil {
|
|
this.module.Errorln(err)
|
|
}
|
|
for i, v := range users {
|
|
menbersCmd[i] = pipe.ZRevRank(this.TableName, v.Uinfo.Uid)
|
|
}
|
|
if _, err = pipe.Exec(); err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
if _, err = cmd.Result(); err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
for i, v := range menbersCmd {
|
|
if rank, err = v.Result(); err != nil {
|
|
this.module.Errorln(err)
|
|
return
|
|
}
|
|
users[i].Rank = int32(rank + 1)
|
|
}
|
|
return
|
|
}
|
|
|
|
// 获取排行榜前50的用户名单
|
|
func (this *modelRank) queryRankUser() (ranks []string, err error) {
|
|
var (
|
|
result []string
|
|
)
|
|
if result, err = this.DBModel.Redis.ZRevRange(fmt.Sprintf("%s-%s", this.DBModel.ServiceId, this.TableName), 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
|
|
}
|