go_dreamfactory/modules/timer/season.go
2022-11-09 14:18:03 +08:00

168 lines
4.8 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"
"go_dreamfactory/sys/db"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"go_dreamfactory/lego/sys/cron"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/sys/redis/pipe"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
game_seasonloop = "game_seasonloop.json"
game_seasonreward = "game_pagodaseasonreward.json"
)
type SeasonPagoda struct {
cbase.ModuleCompBase
modules.MCompConfigure
modules.MCompModel
service core.IService
module *Timer
mail comm.Imail
}
//组件初始化接口
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)
if module1, err := this.service.GetModule(comm.ModuleMail); err == nil {
this.mail = module1.(comm.Imail)
}
return
}
func (this *SeasonPagoda) Start() (err error) {
err = this.MCompModel.Start()
err = this.MCompConfigure.Start()
configure.RegisterConfigure(game_seasonloop, cfg.NewGameSeasonLoop, nil)
configure.RegisterConfigure(game_seasonreward, cfg.NewGamePagodaSeasonReward, nil)
cron.AddFunc("0 0 23 L * ?", this.TimerSeasonOver) //每月最后一天23点执行一次
cron.AddFunc("0 0 5 /* * ?", this.TimerSeasonStar) //每月第一天5点执行一次
return
}
func (this *SeasonPagoda) CreatTestData(index int) {
seasonPagoda := &pb.DBPagoda{}
seasonPagoda.Id = primitive.NewObjectID().Hex()
seasonPagoda.Uid = "dfmxf_634f8f28609d489230fb40fa"
seasonPagoda.PagodaId = int32(index) // 初始数据0层
seasonPagoda.Type = 201 // TODO 新的塔数据根据配置文件获取
if err := this.Add(seasonPagoda.Uid, seasonPagoda); err != nil {
this.module.Errorf("err:%v", err)
return
}
}
func (this *SeasonPagoda) GetSeasonReward() []int32 {
sz := make([]int32, 0)
if v, err := this.GetConfigure(game_seasonreward); err != nil {
log.Errorf("get global conf err:%v", err)
return sz
} else {
if configure, ok := v.(*cfg.GamePagodaSeasonReward); ok {
for _, v := range configure.GetDataList() {
sz = append(sz, v.Ranking[0])
}
}
log.Errorf("%T no is *cfg.Game_global", v)
return sz
}
}
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", configure.Now().Unix())
if conn, err := db.Cross(); err == nil {
var (
pipe *pipe.RedisPipe = conn.Redis.RedisPipe(context.TODO())
)
rd := pipe.ZRange("pagodaSeasonRank", 0, 50)
if _, err = pipe.Exec(); err != nil {
this.module.Errorln(err)
return
}
_data3 := rd.Val()
this.module.Debugf("%v", _data3)
//sz := this.GetSeasonReward()
// this.mail.SendNewMail(&pb.DBMailData{
// CreateTime: uint64(configure.Now().Unix()),
// Items: nil,
// }, _data3...)
}
if db.IsCross() {
if conn, err := db.Cross(); err == nil {
if rst := conn.Mgo.FindOne(comm.TableServerData, bson.M{}); rst != nil {
serverData := &pb.DBServerData{}
rst.Decode(serverData)
conf := this.GetSeasonLoop(comm.SeasonType) // 获取赛季塔重置配置
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]
}
this.DB.UpdateOne(comm.TableServerData, bson.M{}, serverData)
fmt.Printf("%v", serverData)
}
}
}
//star := configure.Now()
this.DB.DeleteMany(comm.TableSeasonPagoda, bson.M{}, options.Delete())
//this.module.Debugf("=====%d,", time.Since(star).Milliseconds())
}
// 赛季塔开始
func (this *SeasonPagoda) TimerSeasonStar() {
this.module.Debugf("TimerSeasonStar:%d", configure.Now().Unix())
}