go_dreamfactory/modules/items/configure.go

126 lines
3.2 KiB
Go

package items
import (
"fmt"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
"sync"
"go_dreamfactory/lego/core"
)
const (
game_item = "game_item.json"
game_propsgroup = "game_propsgroup.json"
game_propsgrouplist = "game_propsgrouplist.json"
)
///背包配置管理组件
type ConfigureComp struct {
modules.MCompConfigure
module *Items
lock sync.RWMutex
propsgroup map[int32][]*cfg.Game_propsgroupData
}
//组件初始化接口
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.(*Items)
this.propsgroup = make(map[int32][]*cfg.Game_propsgroupData)
err = this.LoadConfigure(game_item, cfg.NewGame_item)
err = configure.RegisterConfigure(game_propsgroup, cfg.NewGame_propsgroup, func() {
if v, err := this.GetConfigure(game_propsgroup); err == nil {
if configure, ok := v.(*cfg.Game_propsgroup); ok {
this.lock.Lock()
defer this.lock.Unlock()
for _, value := range configure.GetDataList() {
if _, ok = this.propsgroup[value.Group]; !ok {
this.propsgroup[value.Group] = make([]*cfg.Game_propsgroupData, 0)
}
this.propsgroup[value.Group] = append(this.propsgroup[value.Group], value)
}
return
}
} else {
this.module.Errorln(err)
}
})
return
}
//读取物品配置
func (this *ConfigureComp) GetItemsConfigure() (items map[string]*cfg.Game_itemData, err error) {
var (
v interface{}
)
if v, err = this.GetConfigure(game_item); err != nil {
this.module.Errorf("err:%v", err)
return
} else {
items = v.(*cfg.Game_item).GetDataMap()
}
return
}
//读取物品配置
func (this *ConfigureComp) GetItemConfigure(id string) (item *cfg.Game_itemData, err error) {
var (
v interface{}
ok bool
)
if v, err = this.GetConfigure(game_item); err != nil {
this.module.Errorf("err:%v", err)
return
} else {
if item, ok = v.(*cfg.Game_item).GetDataMap()[id]; !ok {
err = fmt.Errorf("no found item:%s configure", id)
this.module.Errorf("err:%v", err)
return
}
}
return
}
//获取指定类型的物品列表
func (this *ConfigureComp) GetPackItemByType(itmes []*pb.DB_UserItemData, bagtype int32) (result []*pb.DB_UserItemData) {
result = make([]*pb.DB_UserItemData, 0, len(itmes))
var (
v interface{}
table *cfg.Game_item
item *cfg.Game_itemData
ok bool
err error
)
if v, err = this.GetConfigure(game_item); err != nil {
this.module.Errorf("err:%v", err)
return
} else {
table = v.(*cfg.Game_item)
for _, v := range itmes {
if item, ok = table.GetDataMap()[v.ItemId]; ok {
if item.Bagtype == bagtype {
result = append(result, v)
}
} else {
this.module.Errorf("no found itemConfigure:%d", v.ItemId)
}
}
}
return
}
//获取道具礼包组
func (this *ConfigureComp) GetPropsgroupConfigure(gid int32) (result []*cfg.Game_propsgroupData, err error) {
var ok bool
this.lock.RLock()
result, ok = this.propsgroup[gid]
this.lock.RUnlock()
if !ok {
err = fmt.Errorf("no found gid:%d", gid)
}
return
}