package dragon import ( "fmt" "go_dreamfactory/comm" "go_dreamfactory/modules" "go_dreamfactory/sys/configure" cfg "go_dreamfactory/sys/configure/structs" "strconv" "sync" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" ) const moduleName = "dragon" const ( dragon_trainlv = "game_trainlv.json" dragon_play = "game_dragonplay.json" game_buzkashimount = "game_buzkashimount.json" game_dragonlvitem = "game_dragonlvitem.json" ) // /配置管理组件 type configureComp struct { modules.MCompConfigure module *Dragon hlock sync.RWMutex dragon map[string]*cfg.GameTrainlvData play map[string]*cfg.GameDragonPlayData mount map[string]*cfg.GameBuzkashiMountData lvItem map[string]*cfg.GameDragonLvItemData attribute map[string]struct{} } // 组件初始化接口 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.(*Dragon) err = this.LoadMultiConfigure(map[string]interface{}{ //dragon_trainlv: cfg.NewGameTrainlv, }) configure.RegisterConfigure(dragon_trainlv, cfg.NewGameTrainlv, this.LoadDragon) configure.RegisterConfigure(dragon_play, cfg.NewGameDragonPlay, this.LoadDragonPlay) configure.RegisterConfigure(game_buzkashimount, cfg.NewGameBuzkashiMount, this.LoadDragonMount) configure.RegisterConfigure(game_dragonlvitem, cfg.NewGameDragonLvItem, this.LoadDragonLvItem) return } func (this *configureComp) LoadDragon() { if v, err := this.GetConfigure(dragon_trainlv); err == nil { this.hlock.Lock() defer this.hlock.Unlock() this.dragon = make(map[string]*cfg.GameTrainlvData) if _configure, ok := v.(*cfg.GameTrainlv); ok { for _, v := range _configure.GetDataList() { this.dragon[v.Id+"-"+strconv.Itoa(int(v.Lv))] = v } return } } else { err = fmt.Errorf("%T no is *cfg.GameTrainlv", v) } } func (this *configureComp) LoadDragonPlay() { if v, err := this.GetConfigure(dragon_play); err == nil { this.hlock.Lock() defer this.hlock.Unlock() this.play = make(map[string]*cfg.GameDragonPlayData) if _configure, ok := v.(*cfg.GameDragonPlay); ok { for _, v := range _configure.GetDataList() { this.play[v.Id+"-"+strconv.Itoa(int(v.Type))+"-"+strconv.Itoa(int(v.Interact))] = v } return } } else { err = fmt.Errorf("%T no is *cfg.GameDragonPlay", v) } } func (this *configureComp) LoadDragonMount() { if v, err := this.GetConfigure(game_buzkashimount); err == nil { this.hlock.Lock() defer this.hlock.Unlock() this.mount = make(map[string]*cfg.GameBuzkashiMountData) if _configure, ok := v.(*cfg.GameBuzkashiMount); ok { for _, v := range _configure.GetDataList() { this.mount[v.Id+"-"+strconv.Itoa(int(v.Type))] = v } return } } else { err = fmt.Errorf("%T no is *cfg.GameBuzkashiMountData", v) } } //加载多个配置文件 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) GetDragonConfById(id string, lv int32) (conf *cfg.GameTrainlvData, err error) { key := id + "-" + strconv.Itoa(int(lv)) this.hlock.RLock() defer this.hlock.RUnlock() ok := false if conf, ok = this.dragon[key]; ok { return } err = comm.NewNotFoundConfErr(moduleName, dragon_trainlv, fmt.Sprintf("id:%s,lv:%d", id, lv)) return } func (this *configureComp) GetDragonPlayConfById(id string, grow int32, itype int32) (conf *cfg.GameDragonPlayData, err error) { key := id + "-" + strconv.Itoa(int(grow)) + "-" + strconv.Itoa(int(itype)) this.hlock.RLock() defer this.hlock.RUnlock() ok := false if conf, ok = this.play[key]; ok { return } err = comm.NewNotFoundConfErr(moduleName, dragon_trainlv, fmt.Sprintf("id:%s,grow:%d,itype:%d", id, grow, itype)) return } func (this *configureComp) GetDragonMount(id string, itype int32) (conf *cfg.GameBuzkashiMountData, err error) { key := id + "-" + strconv.Itoa(int(itype)) this.hlock.RLock() defer this.hlock.RUnlock() ok := false if conf, ok = this.mount[key]; ok { return } err = comm.NewNotFoundConfErr(moduleName, dragon_trainlv, fmt.Sprintf("id:%s,lv:%d", id, itype)) return } func (this *configureComp) LoadDragonLvItem() { if v, err := this.GetConfigure(game_dragonlvitem); err == nil { this.hlock.Lock() defer this.hlock.Unlock() this.attribute = make(map[string]struct{}, 0) this.lvItem = make(map[string]*cfg.GameDragonLvItemData) if _configure, ok := v.(*cfg.GameDragonLvItem); ok { for _, v := range _configure.GetDataList() { this.lvItem[v.Attribute+"-"+strconv.Itoa(int(v.Lv))] = v if _, ok := this.attribute[v.Attribute]; !ok { this.attribute[v.Attribute] = struct{}{} } } return } } else { err = fmt.Errorf("%T no is *cfg.GameBuzkashiLvData", v) } } func (this *configureComp) GetDragonLvItemConf(attribute string, lv int32) (conf *cfg.GameDragonLvItemData, err error) { key := attribute + "-" + strconv.Itoa(int(lv)) this.hlock.RLock() defer this.hlock.RUnlock() ok := false if conf, ok = this.lvItem[key]; ok { return } err = comm.NewNotFoundConfErr(moduleName, dragon_trainlv, fmt.Sprintf("id:%s,lv:%d", attribute, lv)) return } func (this *configureComp) GetDragonAttributeConf() (conf map[string]struct{}) { this.hlock.RLock() defer this.hlock.RUnlock() return this.attribute }