go_dreamfactory/modules/stonehenge/modelStonehenge.go
2023-07-26 19:07:46 +08:00

111 lines
2.7 KiB
Go

package stonehenge
import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/event"
"go_dreamfactory/lego/sys/mgo"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/utils"
"sync"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/x/bsonx"
)
const (
StoneBossKey = "StoneBossKey"
)
type DBStoneBoss struct {
BossStage map[int32]*StageData
}
type StageData struct {
MaineBuff []int32 // 我方buff
EnemyBuff []int32 // 敌方buff
Roomid int32 // 传送门id
Rtime int64 // 刷新时间
}
// 石阵秘境
type MStonehenge struct {
modules.MCompModel
module *Stonehenge
lock sync.RWMutex
bossStage map[int32]*StageData
}
//组件初始化接口
func (this *MStonehenge) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
this.TableName = comm.TableStonehenge
this.MCompModel.Init(service, module, comp, opt)
this.module = module.(*Stonehenge)
this.bossStage = make(map[int32]*StageData)
//创建uid索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{
Keys: bsonx.Doc{{Key: "uid", Value: bsonx.Int32(1)}},
})
return
}
func (this *MStonehenge) Start() (err error) {
err = this.MCompModel.Start()
event.RegisterGO(core.Event_ServiceStartEnd, func() {
err = this.loadStoneBoos()
})
return
}
func (this *MStonehenge) GetStonehengeData(uid string) *pb.DBStonehenge {
stone := &pb.DBStonehenge{}
if err := this.Get(uid, stone); err != nil && mgo.MongodbNil == err { // 创建一条初始的数据
stone.Id = primitive.NewObjectID().Hex()
stone.Uid = uid
stone.Rooms = nil // 注意初始房间为空
stone.Userbuff = make(map[int32]int32, 0)
stone.Reward = make(map[int32]bool, 0)
stone.Addweight = make(map[int32]int32, 0)
stone.Etime = utils.WeekIntervalTime(0)
this.Add(uid, stone)
return nil
}
return stone
}
// 修改石阵信息
func (this *MStonehenge) ChangeStonehengeData(uid string, update map[string]interface{}) (err error) {
return this.Change(uid, update)
}
func (this *MStonehenge) loadStoneBoos() (err error) {
var (
bNewData bool
)
s := &DBStoneBoss{}
this.module.ModuleTools.GetGlobalData(StoneBossKey, s)
if len(s.BossStage) == 0 {
bNewData = true
} else { // 校验时间
for _, v := range s.BossStage {
if utils.WeekIntervalTime(0) != v.Rtime {
bNewData = true
break
}
}
}
if bNewData {
this.lock.Lock()
this.bossStage = this.module.configure.CheckStage()
this.lock.Unlock()
this.module.ModuleTools.UpdateGlobalData(StoneBossKey, map[string]interface{}{
"BossStage": this.bossStage,
})
}
return
}