go_dreamfactory/modules/viking/comp_configure.go
2022-08-26 17:04:27 +08:00

89 lines
2.3 KiB
Go

package viking
import (
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/core/cbase"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
"sync"
)
const (
game_vikingboss = "game_vikingboss.json"
)
///配置管理基础组件
type configureComp struct {
cbase.ModuleCompBase
hlock sync.RWMutex
modules.MCompConfigure
_vikingMap map[int64]*cfg.GameVikingBossData
}
//组件初始化接口
func (this *configureComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
err = this.ModuleCompBase.Init(service, module, comp, options)
this._vikingMap = make(map[int64]*cfg.GameVikingBossData, 0)
configure.RegisterConfigure(game_vikingboss, cfg.NewGameVikingBoss, func() {
if v, err := this.GetConfigure(game_vikingboss); err == nil {
if configure, ok := v.(*cfg.GameVikingBoss); ok {
this.hlock.Lock()
defer this.hlock.Unlock()
for _, value := range configure.GetDataList() {
this._vikingMap[int64(value.Type<<16)+int64(value.Difficulty)] = value
}
return
}
}
log.Errorf("get game_pagoda conf err:%v", err)
return
})
return
}
// 参数: boss类型 难度
func (this *configureComp) GetVikingBossConfigData(bossType int32, difficulty int32) (data *cfg.GameVikingBossData) {
return this._vikingMap[int64(bossType<<16)+int64(difficulty)]
}
//加载多个配置文件
func (this *configureComp) LoadMultiConfigure(confs map[string]interface{}) (err error) {
for k, v := range confs {
err = configure.RegisterConfigure(k, v, nil)
if err != nil {
log.Errorf("配置文件:%s解析失败!", k)
break
}
}
return
}
//读取配置数据
func (this *configureComp) GetConfigure(name string) (v interface{}, err error) {
return configure.GetConfigure(name)
}
// get boss Type
func (this *configureComp) GetVikingBossTypeConfigData() (mapType map[int32]struct{}) {
mapType = make(map[int32]struct{}, 0)
if v, err := this.GetConfigure(game_vikingboss); err == nil {
if configure, ok := v.(*cfg.GameVikingBoss); ok {
this.hlock.Lock()
defer this.hlock.Unlock()
for _, value := range configure.GetDataList() {
if _, ok := mapType[value.Type]; !ok {
mapType[value.Type] = struct{}{}
}
}
}
}
return
}