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

191 lines
5.4 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"
"time"
"go_dreamfactory/lego/base"
"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"
"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 base.IRPCXService
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.(base.IRPCXService)
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点执行一次
if db.IsCross() {
return
}
conn, err := db.Cross()
if err == nil {
model := db.NewDBModel(comm.TableServerData, 0, conn)
_len, err1 := model.DB.CountDocuments(comm.TableServerData, bson.M{})
if err1 == nil && _len == 0 {
fmt.Printf("%v,%v", _len, err1)
server := &pb.DBServerData{
Id: primitive.NewObjectID().Hex(),
ServerState: 1,
DisposableLoop: 1,
FixedLoop: 0,
SeasonType: 201,
OpenTime: time.Now().Unix(),
}
conf := this.GetSeasonLoop(comm.SeasonType)
if len(conf.DisposableLoop) > 0 {
server.SeasonType = conf.DisposableLoop[0]
}
model.DB.InsertOne(comm.TableServerData, server)
}
}
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 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)
}
}
}
if _, err := this.service.RpcGo(context.Background(),
comm.Service_Worker,
string(comm.Rpc_ModuleSeasonPagodaReward),
pb.EmptyReq{},
nil,
); err != nil {
this.module.Errorln(err)
}
}
// 赛季塔开始
func (this *SeasonPagoda) TimerSeasonStar() {
this.module.Debugf("TimerSeasonStar:%d", configure.Now().Unix())
//star := configure.Now()
if !db.IsCross() { // 删除本服的赛季塔数据
conn, err := db.Cross() // 获取跨服的链接对象
if err == nil {
model := db.NewDBModel(comm.TableServerData, 0, conn)
model.DB.DeleteMany(comm.TableSeasonPagoda, bson.M{}, options.Delete())
model.DB.DeleteMany(comm.TableSeasonRecord, bson.M{}, options.Delete())
for pos := 0; pos < comm.MaxRankNum; pos++ {
key1 := fmt.Sprintf("pagodaList%d", pos)
if err := model.Redis.Delete(key1); err != nil {
log.Errorf("delete failed")
}
}
if err := model.Redis.Delete("pagodaSeasonRank"); err != nil {
log.Errorf("delete failed")
}
}
this.DB.DeleteMany(comm.TableSeasonRecord, bson.M{}, options.Delete())
}
//this.module.Debugf("=====%d,", time.Since(star).Milliseconds())
}