go_dreamfactory/modules/timer/season.go
2022-10-20 18:03:33 +08:00

110 lines
3.0 KiB
Go

package timer
import (
"context"
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
"time"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"go_dreamfactory/lego/sys/cron"
"go_dreamfactory/lego/sys/log"
"go.mongodb.org/mongo-driver/bson"
)
var (
game_seasonloop = "game_seasonloop.json"
)
type SeasonPagoda struct {
cbase.ModuleCompBase
modules.MCompConfigure
modules.MCompModel
service core.IService
module *Timer
}
//组件初始化接口
func (this *SeasonPagoda) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.MCompConfigure.Init(service, module, comp, options)
this.TableName = comm.TableSeasonPagoda
this.MCompModel.Init(service, module, comp, options)
this.service = service
this.module = module.(*Timer)
return
}
func (this *SeasonPagoda) Start() (err error) {
err = this.MCompModel.Start()
err = this.MCompConfigure.Start()
configure.RegisterConfigure(game_seasonloop, cfg.NewGameSeasonLoop, nil)
cron.AddFunc("0 0 23 L * ?", this.TimerSeasonOver) //每月最后一天23点执行一次
cron.AddFunc("0 0 5 /* * ?", this.TimerSeasonStar) //每月第一天5点执行一次
// _data := this.GetSeasonLoop(2)
// this.module.Debugf("%v", _data)
this.TimerSeasonOver()
return
}
func (this *SeasonPagoda) GetSeasonLoop(id int32) *cfg.GameSeasonLoopData {
if v, err := this.GetConfigure(game_seasonloop); err != nil {
log.Errorf("get global conf err:%v", err)
return nil
} else {
if configure, ok := v.(*cfg.GameSeasonLoop); ok {
return configure.Get(id)
}
log.Errorf("%T no is *cfg.Game_global", v)
return nil
}
}
// // 赛季塔结束
func (this *SeasonPagoda) TimerSeasonOver() {
this.module.Debugf("TimerSeasonOver:%d", time.Now().Unix())
conf := this.GetSeasonLoop(comm.SeasonType) // 获取赛季塔重置配置
rest, err := this.DB.Find(comm.TableServerData, bson.M{}) // 查询服务器记录的赛季塔信息
if err != nil {
serverData := &pb.DBServerData{}
for rest.Next(context.TODO()) {
rest.Decode(serverData)
if serverData.FixedLoop == 0 {
if len(conf.DisposableLoop) >= int(serverData.DisposableLoop) && len(conf.FixedLoop) > 0 { // 开始执行循环逻辑
serverData.FixedLoop = 1
serverData.DisposableLoop = 0
serverData.SeasonType = conf.FixedLoop[int(serverData.FixedLoop)-1]
} else {
serverData.DisposableLoop++
serverData.SeasonType = conf.DisposableLoop[int(serverData.DisposableLoop)-1]
}
} else { // 循环
if len(conf.FixedLoop) >= int(serverData.FixedLoop) {
serverData.FixedLoop = 1
} else {
serverData.FixedLoop++
}
serverData.SeasonType = conf.FixedLoop[int(serverData.FixedLoop)-1]
}
fmt.Printf("%v", serverData)
break
}
this.DB.UpdateOne(comm.TableServerData, bson.M{}, serverData)
}
}
// 赛季塔开始
func (this *SeasonPagoda) TimerSeasonStar() {
this.module.Debugf("TimerSeasonStar:%d", time.Now().Unix())
}