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.GamePropsgroupData } //组件初始化接口 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.GamePropsgroupData) err = this.LoadConfigure(game_item, cfg.NewGameItem) err = configure.RegisterConfigure(game_propsgroup, cfg.NewGamePropsgroup, func() { if v, err := this.GetConfigure(game_propsgroup); err == nil { if configure, ok := v.(*cfg.GamePropsgroup); 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.GamePropsgroupData, 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.GameItemData, 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.GameItem).GetDataMap() } return } //读取物品配置 func (this *ConfigureComp) GetItemConfigure(id string) (item *cfg.GameItemData, 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.GameItem).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.GameItem item *cfg.GameItemData ok bool err error ) if v, err = this.GetConfigure(game_item); err != nil { this.module.Errorf("err:%v", err) return } else { table = v.(*cfg.GameItem) 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.GamePropsgroupData, 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 }