go_dreamfactory/modules/gourmet/comp_configure.go
2022-08-18 11:35:47 +08:00

146 lines
4.4 KiB
Go

package gourmet
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_gourmet = "game_gourmet.json"
game_gourmetskill = "game_gourmetskill.json"
game_drop = "game_drop.json"
)
///配置管理基础组件
type configureComp struct {
cbase.ModuleCompBase
hlock sync.RWMutex
modules.MCompConfigure
_gourmetMap map[int64]*cfg.GameGourmetData
_gourmetSkillMap map[int64]*cfg.GameGourmetSkillData
_dropMap map[int32][]*cfg.GameDropData // 掉落表 key 是DiropId
}
//组件初始化接口
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._gourmetMap = make(map[int64]*cfg.GameGourmetData, 0)
configure.RegisterConfigure(game_gourmet, cfg.NewGameGourmet, func() {
if v, err := this.GetConfigure(game_gourmet); err == nil {
if configure, ok := v.(*cfg.GameGourmet); ok {
this.hlock.Lock()
defer this.hlock.Unlock()
for _, value := range configure.GetDataList() {
this._gourmetMap[int64(value.Type<<16)+int64(value.Level)] = value
}
return
}
}
log.Errorf("get game_pagoda conf err:%v", err)
return
})
this._gourmetSkillMap = make(map[int64]*cfg.GameGourmetSkillData, 0)
configure.RegisterConfigure(game_gourmetskill, cfg.NewGameGourmetSkill, func() {
if v, err := this.GetConfigure(game_gourmetskill); err == nil {
if configure, ok := v.(*cfg.GameGourmetSkill); ok {
this.hlock.Lock()
defer this.hlock.Unlock()
for _, value := range configure.GetDataList() {
this._gourmetSkillMap[int64(value.Type<<16)+int64(value.Level)] = value
}
return
}
}
log.Errorf("get game_pagoda conf err:%v", err)
return
})
this._dropMap = make(map[int32][]*cfg.GameDropData, 0)
configure.RegisterConfigure(game_drop, cfg.NewGameDrop, func() {
if v, err := this.GetConfigure(game_drop); err == nil {
if configure, ok := v.(*cfg.GameDrop); ok {
this.hlock.Lock()
defer this.hlock.Unlock()
for _, value := range configure.GetDataList() {
this._dropMap[value.Dropid] = append(this._dropMap[value.Dropid], value)
}
return
}
}
log.Errorf("get game_pagoda conf err:%v", err)
return
})
// _data := this.GetGourmetConfigData(1002, 4) // 测试配置文件读取
// _dataskill := this.GetGourmetSkillConfigData(1001, 4)
// _data := this.GetDropData(1001)
// fmt.Printf("%v", _data)
return
}
// 获取美食馆配置数据
func (this *configureComp) GetGourmetConfigData(gourmetType int32, level int32) (data *cfg.GameGourmetData) {
return this._gourmetMap[int64(gourmetType<<16)+int64(level)]
}
// 获取美食馆配置数据
func (this *configureComp) GetGourmetSkillConfigData(gourmetType int32, level int32) (data *cfg.GameGourmetSkillData) {
return this._gourmetSkillMap[int64(gourmetType<<16)+int64(level)]
}
// 通过技能id 查询技能配置表
func (this *configureComp) GetGourmetSkillConfigBySkillID(skillId int) (data *cfg.GameGourmetSkillData) {
if v, err := this.GetConfigure(game_gourmetskill); err == nil {
if configure, ok := v.(*cfg.GameGourmetSkill); ok {
data = configure.Get(int32(skillId))
return
}
}
return
}
func (this *configureComp) GetGourmetSkillConfigBySkillType(skillType int) (szSkill []int32) {
szSkill = make([]int32, 0)
if v, err := this.GetConfigure(game_gourmetskill); err == nil {
if configure, ok := v.(*cfg.GameGourmetSkill); ok {
for _, data := range configure.GetDataList() {
if skillType == int(data.SkillType) && data.Initial == 1 {
szSkill = append(szSkill, data.Type)
}
}
//data = configure.Get(int32(skillId))
return
}
}
return
}
//加载多个配置文件
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)
}
func (this *configureComp) GetDropData(dropId int32) (data []*cfg.GameDropData) {
data = this._dropMap[dropId]
return
}