go_dreamfactory/modules/practice/configure.go
2023-07-05 10:07:57 +08:00

247 lines
6.2 KiB
Go

package practice
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"
)
const (
game_pandamasmz = "game_pandamasmz.json"
game_pandamasjs = "game_pandamasjs.json"
game_pandamasjx = "game_pandamasjx.json"
game_pandamasyxjx = "game_pandamasyxjx.json"
game_pandamasmryl = "game_pandamasmryl.json"
game_pandamasbuff = "game_pandamasbuff.json"
game_dispatch_battle = "game_dispatch_battle.json"
)
// /背包配置管理组件
type configureComp struct {
modules.MCompConfigure
module *Practice
lock sync.RWMutex
buffs map[int32][]*cfg.GamePandamasBuffData
}
// 组件初始化接口
func (this *configureComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.MCompConfigure.Init(service, module, comp, options)
this.module = module.(*Practice)
this.LoadConfigure(game_pandamasmz, cfg.NewGamePandamasMz)
this.LoadConfigure(game_pandamasjs, cfg.NewGamePandamasJs)
this.LoadConfigure(game_pandamasyxjx, cfg.NewGamePandamasYxjx)
this.LoadConfigure(game_pandamasmryl, cfg.NewGamePandamasMryl)
this.LoadConfigure(game_dispatch_battle, cfg.NewGameDispatch_Battle)
configure.RegisterConfigure(game_pandamasbuff, cfg.NewGamePandamasBuff, func() {
this.lock.Lock()
if v, err := this.GetConfigure(game_pandamasbuff); err != nil {
this.module.Errorf("err:%v", err)
this.lock.Unlock()
return
} else {
this.buffs = make(map[int32][]*cfg.GamePandamasBuffData)
for _, v := range v.(*cfg.GamePandamasBuff).GetDataList() {
if this.buffs[v.BuffGroup] == nil {
this.buffs[v.Id] = make([]*cfg.GamePandamasBuffData, 0)
}
this.buffs[v.BuffGroup] = append(this.buffs[v.BuffGroup], v)
}
}
this.lock.Unlock()
})
configure.RegisterConfigure(game_pandamasjx, cfg.NewGamePandamasJx, func() {
if v, err := this.GetConfigure(game_pandamasjx); err != nil {
this.module.Errorf("err:%v", err)
this.lock.Unlock()
return
} else {
for _, v := range v.(*cfg.GamePandamasJx).GetDataList() {
if len(v.Ants) > 0 && (len(v.Num) != 2 || v.Num[1] < v.Num[0]) {
this.module.Error("配置错误!", log.Field{Key: "Num", Value: v.Num})
}
}
}
})
return
}
func (this *configureComp) getGamePandamasMz(lv int32) (configure *cfg.GamePandamasMzData, err error) {
var (
v interface{}
ok bool
)
if v, err = this.GetConfigure(game_pandamasmz); err != nil {
this.module.Errorln(err)
return
} else {
if configure, ok = v.(*cfg.GamePandamasMz).GetDataMap()[lv]; !ok {
err = fmt.Errorf("not found:%d ", lv)
this.module.Errorln(err)
return
}
}
return
}
func (this *configureComp) getGamePandamasJs() (configure []*cfg.GamePandamasJsData, err error) {
var (
v interface{}
)
if v, err = this.GetConfigure(game_pandamasjs); err != nil {
this.module.Errorln(err)
return
} else {
configure = v.(*cfg.GamePandamasJs).GetDataList()
}
return
}
func (this *configureComp) getGamePandamasJx(id string) (configure *cfg.GamePandamasJxData, err error) {
var (
v interface{}
ok bool
)
if v, err = this.GetConfigure(game_pandamasjx); err != nil {
this.module.Errorln(err)
return
} else {
if configure, ok = v.(*cfg.GamePandamasJx).GetDataMap()[id]; !ok {
err = fmt.Errorf("not found:%s ", id)
this.module.Errorln(err)
return
}
}
return
}
func (this *configureComp) getGamePandamasYxjx(id int32) (configure *cfg.GamePandamasYxjxData, err error) {
var (
v interface{}
ok bool
)
if v, err = this.GetConfigure(game_pandamasyxjx); err != nil {
this.module.Errorln(err)
return
} else {
if configure, ok = v.(*cfg.GamePandamasYxjx).GetDataMap()[id]; !ok {
err = fmt.Errorf("not found:%d", id)
this.module.Errorln(err)
return
}
}
return
}
func (this *configureComp) getGamePandamasMryls() (configure []*cfg.GamePandamasMrylData, err error) {
var (
v interface{}
)
if v, err = this.GetConfigure(game_pandamasmryl); err != nil {
this.module.Errorln(err)
return
} else {
configure = v.(*cfg.GamePandamasMryl).GetDataList()
}
return
}
func (this *configureComp) getPandamasMryl(id int32) (configure *cfg.GamePandamasMrylData, err error) {
var (
v interface{}
ok bool
)
if v, err = this.GetConfigure(game_pandamasmryl); err != nil {
this.module.Errorln(err)
return
} else {
if configure, ok = v.(*cfg.GamePandamasMryl).GetDataMap()[id]; !ok {
err = fmt.Errorf("not found:%d ", id)
this.module.Errorln(err)
return
}
}
return
}
// 随机获取buff权重
func (this *configureComp) getGamePandamasBuff(groupid int32) (configure *cfg.GamePandamasBuffData, err error) {
var (
group []*cfg.GamePandamasBuffData
weight []int32
index int32
ok bool
)
this.lock.RLock()
group, ok = this.buffs[groupid]
this.lock.RUnlock()
if !ok {
err = fmt.Errorf("no found groupid:%d", groupid)
this.module.Errorln(err)
return
}
weight = make([]int32, len(group))
for i, v := range group {
weight[i] = v.P
}
index = comm.GetRandW(weight)
configure = group[index]
return
}
// 获取踢馆npc配置
func (this *configureComp) getDispatchBattle() (configure []*cfg.GameDispatch_BattleData, err error) {
var (
v interface{}
)
if v, err = this.GetConfigure(game_dispatch_battle); err != nil {
this.module.Errorln(err)
return
} else {
configure = v.(*cfg.GameDispatch_Battle).GetDataList()
}
return
}
// 获取踢馆npc配置
func (this *configureComp) getDispatchBattleData(id int32) (configure *cfg.GameDispatch_BattleData, err error) {
var (
v interface{}
ok bool
)
if v, err = this.GetConfigure(game_dispatch_battle); err != nil {
this.module.Errorln(err)
return
} else {
if configure, ok = v.(*cfg.GameDispatch_Battle).GetDataMap()[id]; !ok {
err = fmt.Errorf("not found:%d ", id)
this.module.Errorln(err)
return
}
}
return
}
func (this *configureComp) getAllGamePandamasJx() (res []string, err error) {
var (
v interface{}
)
if v, err = this.GetConfigure(game_pandamasjx); err == nil {
if configure, ok := v.(*cfg.GamePandamasJx); ok {
for _, v := range configure.GetDataList() {
res = append(res, v.Id)
}
return
}
}
this.module.Errorln(err)
return
}