package entertainment import ( "fmt" "go_dreamfactory/comm" "go_dreamfactory/modules" "go_dreamfactory/sys/configure" cfg "go_dreamfactory/sys/configure/structs" "sync" "go_dreamfactory/lego/core" ) const ( game_block = "game_block.json" game_consumehero = "game_consumehero.json" game_integral = "game_integral.json" game_consumeIntegral = "game_consumeintegral.json" game_playingmethod = "game_playingmethod.json" consume_box = "game_consumebox.json" ) // /配置管理组件 type configureComp struct { modules.MCompConfigure module *Entertainment lock sync.RWMutex block map[int32]*cfg.GameBlockData } const moduleName = "entertainment" 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.(*Entertainment) err = this.LoadMultiConfigure(map[string]interface{}{ game_consumehero: cfg.NewGameConsumeHero, game_integral: cfg.NewGameIntegral, game_consumeIntegral: cfg.NewGameConsumeIntegral, game_playingmethod: cfg.NewGamePlayingMethod, consume_box: cfg.NewGameConsumeBox, }) configure.RegisterConfigure(game_block, cfg.NewGameBlock, this.LoadGameBlock) return } func (this *configureComp) LoadGameBlock() { var ( v interface{} configure *cfg.GameBlock err error ok bool ) if v, err = this.GetConfigure(game_block); err != nil { this.module.Errorln(err) return } block := make(map[int32]*cfg.GameBlockData) if configure, ok = v.(*cfg.GameBlock); ok { for _, v := range configure.GetDataList() { if v.Belongto == 2 { continue } key := v.Color<<8 + v.Type block[key] = v } } this.lock.Lock() this.block = block this.lock.Unlock() return } func (this *configureComp) GetGameBlock(color int32, iType int32) (conf *cfg.GameBlockData, err error) { var ( ok bool key int32 ) key = color<<8 + iType this.lock.RLock() defer this.lock.RUnlock() if conf, ok = this.block[key]; !ok { err = comm.NewNotFoundConfErr(string(this.module.GetType()), game_block, fmt.Sprintf("color:%d,itype:%d", color, key)) } return } func (this *configureComp) GetGameConsumeHero(heroid string) (conf *cfg.GameConsumeHeroData, err error) { var ( v interface{} ) if v, err = this.GetConfigure(game_consumehero); err == nil { if configure, ok := v.(*cfg.GameConsumeHero); ok { if conf = configure.Get(heroid); conf != nil { return } } } err = comm.NewNotFoundConfErr(moduleName, game_consumehero, heroid) return } func (this *configureComp) GetGameConsumeintegral(key int32) (conf *cfg.GameIntegralData, err error) { var ( v interface{} configure *cfg.GameIntegral ok bool ) if v, err = this.GetConfigure(game_integral); err == nil { if configure, ok = v.(*cfg.GameIntegral); ok { for pos, v := range configure.GetDataList() { if v.Key >= key { if pos == 0 { conf = configure.GetDataList()[1] return } conf = v return } } } } if configure.GetDataList()[len(configure.GetDataList())-1].Key < key { // 设置最大值 conf = configure.GetDataList()[len(configure.GetDataList())-1] return } err = comm.NewNotFoundConfErr(moduleName, game_integral, key) return } func (this *configureComp) GetRobotGameConsumeHero() (cardid string) { if v, err := this.GetConfigure(game_consumehero); err == nil { if configure, ok := v.(*cfg.GameConsumeHero); ok { for _, v := range configure.GetDataList() { cardid = v.Key break } } } return } func (this *configureComp) GetGameConsumeintegralReward(key int32) (conf *cfg.GameIntegralData, err error) { var ( v interface{} ) if v, err = this.GetConfigure(game_integral); err == nil { if configure, ok := v.(*cfg.GameIntegral); ok { if conf = configure.Get(key); conf != nil { return } } } err = comm.NewNotFoundConfErr(moduleName, game_integral, key) return } func (this *configureComp) GetInitGameConsumeHero() (cardid []string) { if v, err := this.GetConfigure(game_consumehero); err == nil { if configure, ok := v.(*cfg.GameConsumeHero); ok { for _, v := range configure.GetDataList() { if v.Type == 1 { cardid = append(cardid, v.Key) } } } } return } func (this *configureComp) GetGameConsumeIntegral() (itype []*cfg.GamePlayingMethodData) { if v, err := this.GetConfigure(game_playingmethod); err == nil { if configure, ok := v.(*cfg.GamePlayingMethod); ok { for _, v := range configure.GetDataList() { if v.Type == 2 { itype = append(itype, v) } } } } return } func (this *configureComp) GetGameConsumeIntegralByKey(key int32) (conf *cfg.GamePlayingMethodData, err error) { var ( v interface{} ) if v, err = this.GetConfigure(game_playingmethod); err == nil { if configure, ok := v.(*cfg.GamePlayingMethod); ok { if conf = configure.Get(key); conf != nil { return } } } err = comm.NewNotFoundConfErr(moduleName, game_playingmethod, key) return } func (this *configureComp) GetGameConsumeBoxConf(boxid int32) (conf *cfg.GameConsumeBoxData, err error) { var ( v interface{} ) if v, err = this.GetConfigure(consume_box); err == nil { if configure, ok := v.(*cfg.GameConsumeBox); ok { if conf = configure.Get(boxid); conf != nil { return } } } err = comm.NewNotFoundConfErr(moduleName, consume_box, boxid) return } func (this *configureComp) GetGameRandomConsumeBoxConf() (conf *cfg.GameConsumeBoxData, err error) { var ( v interface{} szWeight []int32 boxid []int32 ) if v, err = this.GetConfigure(consume_box); err == nil { if configure, ok := v.(*cfg.GameConsumeBox); ok { for _, v1 := range configure.GetDataList() { szWeight = append(szWeight, v1.Weight) boxid = append(boxid, v1.Boxid) } } } conf, err = this.GetGameConsumeBoxConf(boxid[comm.GetRandW(szWeight)]) return } func (this *configureComp) GetGameBlockByKey(key int32) (conf *cfg.GameBlockData, err error) { var ( v interface{} ) if v, err = this.GetConfigure(game_block); err == nil { if configure, ok := v.(*cfg.GameBlock); ok { if conf = configure.Get(key); conf != nil { return } } } err = comm.NewNotFoundConfErr(moduleName, game_block, key) return } // 通过权重获取一个普通元素 func (this *configureComp) GetGameNormalElem() (rd int32) { var ( v interface{} szWeight []int32 err error ) if v, err = this.GetConfigure(game_block); err == nil { if configure, ok := v.(*cfg.GameBlock); ok { for _, v := range configure.GetDataList() { if v.Key > 6 { break } szWeight = append(szWeight, v.Weight) } } } rd = comm.GetRandW(szWeight) + 1 return }