go_dreamfactory/modules/viking/comp_configure.go
2024-01-12 18:35:45 +08:00

153 lines
4.1 KiB
Go

package viking
import (
"fmt"
"go_dreamfactory/comm"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
"sync"
)
var moduleName = "viking"
const (
game_vikingboss = "game_vikingboss.json"
// game_challenge = "game_vikingchallenge.json"
viking_reward = "game_vikingreward.json" //全局配置表
game_sweep = "game_sweep.json"
)
///配置管理基础组件
type configureComp struct {
modules.MCompConfigure
module core.IModule
hlock sync.RWMutex
_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.MCompConfigure.Init(service, module, comp, options)
this.module = module
err = this.LoadConfigure(viking_reward, cfg.NewGameVikingReward)
err = this.LoadConfigure(game_sweep, cfg.NewGameSweep)
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.Bossid<<16)+int64(value.Difficulty)] = value
}
return
}
}
log.Errorf("get game_viking conf err:%v", err)
return
})
this.GetVikingRewardConf()
return
}
// 参数: boss类型 难度
func (this *configureComp) GetVikingBossConfigData(bossId int32, difficulty int32) (data *cfg.GameVikingBossData, err error) {
this.hlock.RLock()
if data = this._vikingMap[int64(bossId<<16)+int64(difficulty)]; data == nil {
err = comm.NewNotFoundConfErr(moduleName, game_vikingboss, fmt.Errorf("bossId:%d,difficulty:%d", bossId, difficulty))
}
this.hlock.RUnlock()
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)
}
// get boss Type
func (this *configureComp) GetVikingBossTypeConfigData() 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.Bossid]; !ok {
ipos := value.Bossid
mapType[ipos] = struct{}{}
}
}
}
}
return mapType
}
// 警告: 此接口只允许GM调用
func (this *configureComp) GetVikingBossAllData() (data map[int32]int32) {
data = make(map[int32]int32)
if v, err := this.GetConfigure(game_vikingboss); err == nil {
if configure, ok := v.(*cfg.GameVikingBoss); ok {
for _, value := range configure.GetDataList() {
if v1, ok := data[value.Bossid]; !ok {
data[value.Bossid] = value.Difficulty
} else {
if v1 < value.Difficulty {
data[value.Bossid] = value.Difficulty
}
}
}
}
}
return
}
func (this *configureComp) GetVikingRewardConf() (conf []*cfg.GameVikingRewardData) {
if v, err := this.GetConfigure(viking_reward); err == nil {
if configure, ok := v.(*cfg.GameVikingReward); ok {
conf = configure.GetDataList()
return
}
}
return
}
func (this *configureComp) GetGameSweepData(id int32, round int32) (conf *cfg.GameSweepData, err error) {
var (
v interface{}
)
if v, err = this.GetConfigure(game_sweep); err != nil {
return
} else {
for _, v := range v.(*cfg.GameSweep).GetDataList() {
if v.Group == id && round >= v.Low && round <= v.Up {
conf = v
return
}
}
err = comm.NewNotFoundConfErr(string(this.module.GetType()), game_sweep, id)
return
}
return
}