65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package arena
|
|
|
|
import (
|
|
"context"
|
|
"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 *Arena
|
|
}
|
|
|
|
//组件初始化接口
|
|
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.(*Arena)
|
|
|
|
return
|
|
}
|
|
|
|
//更新排名
|
|
func (this *modelRank) updateArenaRank(users ...*pb.ArenaPlayer) (err error) {
|
|
var (
|
|
pipe *pipe.RedisPipe = this.DBModel.Redis.RedisPipe(context.TODO())
|
|
menbers []*redis.Z
|
|
cmd *redis.IntCmd
|
|
menbersCmd []*redis.IntCmd
|
|
rank int64
|
|
)
|
|
menbers = make([]*redis.Z, len(users))
|
|
for i, v := range users {
|
|
menbers[i] = &redis.Z{Score: float64(v.Integral), Member: v.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.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
|
|
}
|