go_dreamfactory/modules/timer/season.go
2023-01-30 16:28:57 +08:00

272 lines
7.5 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"
"math"
"strconv"
"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_dreamfactory/lego/sys/redis/pipe"
"github.com/go-redis/redis/v8"
"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
}
//time.Sleep(time.Second * 1)
//this.DbTest()
conn, err := db.Cross()
if err == nil {
//this.DbTest()
model := db.NewDBModel(comm.TableSeasonData, 0, conn)
_len, err1 := model.DB.CountDocuments(comm.TableSeasonData, 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.TableSeasonData, 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.TableSeasonData, 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.TableSeasonData, 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.TableSeasonData, 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())
}
func (this *SeasonPagoda) DbTest() {
conn, _ := db.Cross()
model1 := db.NewDBModel(comm.TableVikingRank, 0, conn)
model1.Redis.Delete("vikingRank2")
_d, err := model1.Redis.Keys("vikingrank:*")
if err == nil {
for _, v := range _d {
model1.Redis.Delete(v)
}
fmt.Printf("%v", _d)
}
//star := configure.Now()
for i := 1; i < 10; i++ {
new := &pb.DBVikingRank{
Id: primitive.NewObjectID().Hex(),
Uid: "mmmxxx" + strconv.Itoa(i),
Difficulty: int32(i) % 10,
Bosstype: 2,
Nickname: "helo",
Icon: "",
Lv: 120,
Leadpos: 1,
Line: make([]*pb.LineUp, 5),
CostTime: 12000 + int32(i),
}
model1.AddList(new.Uid, new.Id, new)
//model1.Add(new.Uid, new)
var (
pipe *pipe.RedisPipe = conn.Redis.RedisPipe(context.TODO())
menbers *redis.Z
tableName string
score int64
)
score = int64(i)<<31 + int64(math.MaxInt32-new.CostTime)
tableName = "vikingRank" + strconv.Itoa(int(new.Bosstype))
strKey := "vikingrank:" + new.Uid + "-" + new.Id // 自定义key new.Uid //
menbers = &redis.Z{Score: float64(score), Member: strKey}
if cmd := pipe.ZAdd(tableName, menbers); cmd != nil {
dock, err1 := cmd.Result()
if err1 != nil {
this.module.Errorln(dock, err1)
}
}
if _, err := pipe.Exec(); err != nil {
this.module.Errorln(err)
}
}
//this.module.Debugf("=====%d,", time.Since(star).Milliseconds())
if true {
var (
pipe *pipe.RedisPipe = conn.Redis.RedisPipe(context.TODO())
szRank []*pb.DBVikingRank
)
// 降序
rd := pipe.ZRevRange("vikingRank"+strconv.Itoa(2), 0, -1)
if _, err := pipe.Exec(); err != nil {
this.module.Errorln(err)
return
}
_dataList := rd.Val()
for _, v := range _dataList {
result := &pb.DBVikingRank{}
if err := model1.Redis.HGetAll(v, result); err == nil {
szRank = append(szRank, result)
}
}
this.module.Debugf("%v", szRank)
}
}