go_dreamfactory/modules/timer/vikingrank.go

62 lines
1.6 KiB
Go

package timer
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/cron"
"go_dreamfactory/lego/sys/log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
type VikingRank struct {
modules.MCompModel
service core.IService
dbName string
}
//组件初始化接口
func (this *VikingRank) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.dbName = comm.TableVikingRank
this.TableName = comm.TableVikingRankList
this.MCompModel.Init(service, module, comp, options)
this.service = service
return
}
func (this *VikingRank) Start() (err error) {
err = this.MCompModel.Start()
cron.AddFunc("*/60 * * * * ?", this.Timer) //每60s执行一次
return
}
// 处理排行榜排序
func (this *VikingRank) Timer() {
data := make([]interface{}, 0) // options.Find().SetLimit(comm.MaxRankList)
for i := 1; i <= 3; i++ { // boss 类型 1 2 3 后面封装 // 时间参数战斗调完后再加进来
if _data, err := this.DB.Find(core.SqlTable(this.dbName), bson.M{"bosstype": i}, options.Find().SetSort(bson.M{"difficulty": -1}).SetLimit(comm.MaxRankList)); err == nil {
for _data.Next(context.TODO()) {
temp := &pb.DBVikingRank{}
if err = _data.Decode(temp); err == nil {
data = append(data, temp)
}
}
}
}
if len(data) > 0 {
err := this.Redis.RPush(this.TableName, data...)
if err == nil {
err = this.Redis.Ltrim(this.TableName, -1*len(data), -1) //对一个列表进行修剪
if err != nil {
log.Errorf("delete failed")
}
}
}
}