package questionnaire import ( "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/modules" "go_dreamfactory/sys/configure" cfg "go_dreamfactory/sys/configure/structs" "sync" ) const ( game_askall = "game_askall.json" game_asklibrary = "game_asklibrary.json" ) type configureComp struct { modules.MCompConfigure module *Questionnaire lock sync.RWMutex groupAsk map[int32][]*cfg.GameAskLibraryData //key 条件ID } 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.(*Questionnaire) err = this.LoadMultiConfigure(map[string]interface{}{ game_askall: cfg.NewGameAskAll, }) configure.RegisterConfigure(game_asklibrary, cfg.NewGameAskLibrary, this.updateconfigure) return } // 更新任务配置表 func (this *configureComp) updateconfigure() { var ( v interface{} conf *cfg.GameAskLibrary ok bool err error ) if v, err = this.GetConfigure(game_asklibrary); err != nil { return } if conf, ok = v.(*cfg.GameAskLibrary); !ok { this.module.Error("日常任务配置异常!") return } groupTasksConf := make(map[int32][]*cfg.GameAskLibraryData) for _, v := range conf.GetDataList() { if _, ok := groupTasksConf[v.ExaminationGroup]; !ok { groupTasksConf[v.ExaminationGroup] = make([]*cfg.GameAskLibraryData, 0) } groupTasksConf[v.ExaminationGroup] = append(groupTasksConf[v.ExaminationGroup], v) } this.lock.Lock() this.groupAsk = groupTasksConf this.lock.Unlock() } // 获取装备配置数据 func (this *configureComp) getAskAllData(id int32) (configure *cfg.GameAskAllData, err error) { var ( v interface{} ok bool ) if v, err = this.GetConfigure(game_askall); err != nil { this.module.Errorf("err:%v", err) return } else { if configure, ok = v.(*cfg.GameAskAll).GetDataMap()[id]; !ok { err = comm.NewNotFoundConfErr(string(this.module.GetType()), game_askall, id) this.module.Errorf("err:%v", err) return } } return } // 获取装备配置数据 func (this *configureComp) getGameAskLibraryDataById(id int32) (configure *cfg.GameAskLibraryData, err error) { var ( v interface{} ok bool ) if v, err = this.GetConfigure(game_asklibrary); err != nil { this.module.Errorf("err:%v", err) return } else { if configure, ok = v.(*cfg.GameAskLibrary).GetDataMap()[id]; !ok { err = comm.NewNotFoundConfErr(string(this.module.GetType()), game_asklibrary, id) this.module.Errorf("err:%v", err) return } } return } // 随机任务 func (this *configureComp) getGameAskLibraryData(group, num int32) (results []*cfg.GameAskLibraryData, err error) { var ( asks []*cfg.GameAskLibraryData rands []int ok bool ) this.lock.RLock() asks, ok = this.groupAsk[group] this.lock.RUnlock() if !ok { err = fmt.Errorf("no found group:%d", group) return } if num > int32(len(asks)) { num = int32(len(asks)) } results = make([]*cfg.GameAskLibraryData, 0) rands = comm.RandShuffle(len(asks)) for i := 0; i < int(num); i++ { results = append(results, asks[rands[i]]) } return }