183 lines
5.1 KiB
Go
183 lines
5.1 KiB
Go
package timer
|
|
|
|
import (
|
|
"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/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"
|
|
)
|
|
|
|
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点执行一次
|
|
|
|
newData := &pb.DBPagoda{
|
|
Id: primitive.NewObjectID().Hex(),
|
|
Uid: "sdsd",
|
|
PagodaId: 2,
|
|
Reward: map[int32]bool{},
|
|
Type: 101,
|
|
}
|
|
if conn, err := db.Cross(); err == nil {
|
|
rst, _ := conn.Mgo.InsertOne(comm.TableSeasonPagoda, newData)
|
|
|
|
fmt.Printf("%v", rst)
|
|
}
|
|
|
|
return
|
|
}
|
|
func (this *SeasonPagoda) CreatTestData(index int) {
|
|
seasonPagoda := &pb.DBSeasonPagoda{}
|
|
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) 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())
|
|
|
|
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 := time.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", time.Now().Unix())
|
|
}
|
|
|
|
// 测试用 后面删
|
|
func (this *SeasonPagoda) TestFunc() {
|
|
// newData1 := &pb.DBSeasonPagoda{
|
|
// Id: primitive.NewObjectID().Hex(),
|
|
// Uid: "sdsdassd",
|
|
// PagodaId: 1,
|
|
// Reward: map[int32]bool{},
|
|
// Type: 201,
|
|
// }
|
|
// if conn, err := db.Cross(); err == nil {
|
|
// rst, err := conn.Mgo.InsertOne(comm.TableSeasonPagoda, newData)
|
|
|
|
// fmt.Printf("%v,%v", rst, err)
|
|
// }
|
|
// _data := this.GetSeasonLoop(2)
|
|
// this.module.Debugf("%v", _data)
|
|
// for i := 0; i < 10000; i++ {
|
|
// this.CreatTestData(i)
|
|
// }
|
|
|
|
// if conn, err := db.Cross(); err == nil {
|
|
// rst := conn.Mgo.FindOne(comm.TableServerData, bson.M{})
|
|
// server := &pb.DBServerData{}
|
|
// rst.Decode(server)
|
|
// fmt.Printf("%v", server)
|
|
// }
|
|
|
|
// newData := &pb.DBPagodaRecord{
|
|
// Id: primitive.NewObjectID().Hex(),
|
|
// Uid: "sdsd",
|
|
// PagodaId: 1,
|
|
// Type: 2,
|
|
// Nickname: "userinfo.Name",
|
|
// Icon: "", // icon 暂无
|
|
// Lv: 12,
|
|
// CostTime: 12000,
|
|
// }
|
|
// // 数据写到跨服中
|
|
// if !db.IsCross() {
|
|
// if conn, err := db.Cross(); err == nil {
|
|
// conn.Mgo.InsertOne(comm.TableSeasonRecord, newData)
|
|
// }
|
|
// }
|
|
//this.TimerSeasonOver()
|
|
}
|