62 lines
1.7 KiB
Go
62 lines
1.7 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 HuntingRank struct {
|
|
modules.MCompModel
|
|
service core.IService
|
|
DbName string
|
|
}
|
|
|
|
//组件初始化接口
|
|
func (this *HuntingRank) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
this.DbName = comm.TableHuntingRank
|
|
this.TableName = comm.TableHuntingRankList
|
|
this.MCompModel.Init(service, module, comp, options)
|
|
this.service = service
|
|
return
|
|
}
|
|
|
|
func (this *HuntingRank) Start() (err error) {
|
|
err = this.MCompModel.Start()
|
|
cron.AddFunc("*/60 * * * * ?", this.Timer) //每60s执行一次
|
|
return
|
|
}
|
|
|
|
// 处理排行榜排序
|
|
func (this *HuntingRank) Timer() {
|
|
data := make([]interface{}, 0) // options.Find().SetLimit(comm.MaxRankList)
|
|
for i := 1; i <= 4; i++ { // boss 类型 1 2 3 4 后面封装 // 时间参数战斗调完后再加进来
|
|
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.DBHuntingRank{}
|
|
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")
|
|
}
|
|
}
|
|
}
|
|
}
|