package stonehenge import ( "fmt" "go_dreamfactory/comm" "go_dreamfactory/lego/core" "go_dreamfactory/lego/sys/log" "go_dreamfactory/modules" "go_dreamfactory/sys/configure" cfg "go_dreamfactory/sys/configure/structs" "sync" ) const ( game_bufflottery = "game_bufflottery.json" game_eventlottery = "game_eventlottery.json" game_roomlottery = "game_roomlottery.json" ) ///背包配置管理组件 type configureComp struct { modules.MCompConfigure module *Stonehenge hlock sync.RWMutex // 房间随机 _groupR map[int64][]int32 // key 小组ID value cid // 类型为1 的数据 该大组中的小组为权重掉落,必定从N个小组中随机出1个小组 _lotteryType1R map[int32][]int32 // key 大组ID value cid // 类型为2 的数据 有多个小组ID _lotteryType2R map[int32][]int32 // key 大组ID value 小组ID // 小组类型为1 _groupType1R map[int64][]int32 //value cid // 小组类型为2 _groupType2R map[int64][]int32 //value cid BtypeR map[int32]int32 StypeR map[int64]int32 // subtype SNumR map[int64]int32 // 小组产出数量 // event 随机 _groupE map[int64][]int32 // key 小组ID value cid // 类型为1 的数据 该大组中的小组为权重掉落,必定从N个小组中随机出1个小组 _lotteryType1E map[int32][]int32 // key 大组ID value cid // 类型为2 的数据 有多个小组ID _lotteryType2E map[int32][]int32 // key 大组ID value 小组ID // 小组类型为1 _groupType1E map[int64][]int32 //value cid // 小组类型为2 _groupType2E map[int64][]int32 //value cid BtypeE map[int32]int32 StypeE map[int64]int32 // subtype SNumE map[int64]int32 // 小组产出数量 // buff 随机 _group map[int64][]int32 // key 小组ID value cid // 类型为1 的数据 该大组中的小组为权重掉落,必定从N个小组中随机出1个小组 _lotteryType1 map[int32][]int32 // key 大组ID value cid // 类型为2 的数据 有多个小组ID _lotteryType2 map[int32][]int32 // key 大组ID value 小组ID // 小组类型为1 _groupType1 map[int64][]int32 //value cid // 小组类型为2 _groupType2 map[int64][]int32 //value cid Btype map[int32]int32 Stype map[int64]int32 // subtype SNum map[int64]int32 // 小组产出数量 } //组件初始化接口 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.(*Stonehenge) err = this.LoadConfigure(game_bufflottery, cfg.NewGameBufflottery) configure.RegisterConfigure(game_bufflottery, cfg.NewGameBufflottery, this.LoadBUffGroupData) err = this.LoadConfigure(game_eventlottery, cfg.NewGameEventlottery) configure.RegisterConfigure(game_eventlottery, cfg.NewGameEventlottery, this.LoadEventGroupData) err = this.LoadConfigure(game_roomlottery, cfg.NewGameRoomlottery) configure.RegisterConfigure(game_roomlottery, cfg.NewGameRoomlottery, this.LoadRoomGroupData) //测试 接口 _d := this.GetBuffGroupDataByLottery(100001) _d = this.GetEventGroupDataByLottery(1001001) _d = this.GetRoomGroupDataByLottery(100001) _d = this.GetBuffGroupDataByLottery(100002) _d = this.GetRoomGroupDataByLottery(100002) fmt.Printf("%v", _d) return } func (this *configureComp) LoadEventGroupData() { if v, err := this.GetConfigure(game_eventlottery); err == nil { if configure, ok := v.(*cfg.GameEventlottery); ok { this.hlock.Lock() defer this.hlock.Unlock() this._groupE = make(map[int64][]int32, 0) this._lotteryType1E = make(map[int32][]int32, 0) this._lotteryType2E = make(map[int32][]int32, 0) this._groupType1E = make(map[int64][]int32, 0) this._groupType2E = make(map[int64][]int32, 0) this.BtypeE = make(map[int32]int32, 0) this.StypeE = make(map[int64]int32, 0) this.SNumE = make(map[int64]int32, 0) for _, value := range configure.GetDataList() { key := int64(value.GroupId)<<31 + int64(value.SubGroupId) this._groupE[key] = append(this._groupE[key], value.Id) if _, ok := this.BtypeE[value.GroupId]; !ok { this.BtypeE[value.GroupId] = value.GroupType } if _, ok := this.StypeE[key]; !ok { this.StypeE[key] = value.SubGroupType } if _, ok := this.SNumE[key]; !ok { this.SNumE[key] = value.SubGroupNum } if this.BtypeE[value.GroupId] == 1 { this._lotteryType1E[value.GroupId] = append(this._lotteryType1E[value.GroupId], value.Id) } else if this.BtypeE[value.GroupId] == 2 { this._lotteryType2E[value.GroupId] = append(this._lotteryType2E[value.GroupId], value.Id) } if this.StypeE[key] == 1 { // 小组ID为1 this._groupType1E[key] = append(this._groupType1E[key], value.Id) } else if this.StypeE[key] == 2 { this._groupType2E[key] = append(this._groupType2E[key], value.Id) } } return } } else { log.Errorf("get NewGameBufflottery conf err:%v", err) } return } func (this *configureComp) GetEventLotterConfById(id int32) (data *cfg.GameEventlotteryData) { if v, err := this.GetConfigure(game_eventlottery); err == nil { if configure, ok := v.(*cfg.GameEventlottery); ok { return configure.Get(id) } } return } // 实际掉落逻辑 (传入 掉落组ID vip等级 玩家等级 返回获得的道具) func (this *configureComp) GetEventGroupDataByLottery(lotteryId int32) (buff []int32) { if _, ok := this._lotteryType1E[lotteryId]; !ok { if _, ok := this._lotteryType2E[lotteryId]; !ok { fmt.Printf("not found config lotterId:%d", lotteryId) return } } // 优先校验大组ID 的类型 if this.BtypeE[lotteryId] == 1 { // 该大组中的小组为权重掉落,必定从N个小组中随机出1个小组 var ( szW []int32 // 权重数组 szID []int32 // 小组ID 数组 groupID int32 gourp map[int32]int32 // key 小组ID value 权重 ) gourp = make(map[int32]int32, 0) // 随机小组id for _, v := range this._lotteryType1E[lotteryId] { if _data := this.GetEventLotterConfById(v); _data != nil { if _, ok := gourp[_data.SubGroupId]; !ok { gourp[_data.SubGroupId] = _data.SubGroupWt // 小组ID 权重赋值 szW = append(szW, _data.SubGroupWt) szID = append(szID, _data.SubGroupId) } } } groupID = szID[comm.GetRandW(szW)] // 获得小组ID //fmt.Printf("大组类型为1的,获得小组ID :%d,dropID:%d", groupID, lotteryId) key := int64(lotteryId)<<31 + int64(groupID) // 小组ID 类型判断 if this.StypeE[key] == 1 { // 该小组的道具为权重掉落,必定从N个道具中随机出1个道具 for i := 0; i < int(this.SNumE[key]); i++ { szW = make([]int32, 0) szID = make([]int32, 0) gourp = make(map[int32]int32, 0) for _, v := range this._groupType1E[key] { if _data := this.GetEventLotterConfById(v); _data != nil { // 权重赋值 if _, ok := gourp[_data.SubGroupId]; !ok { szW = append(szW, _data.EventWt) szID = append(szID, _data.Id) } } } index := comm.GetRandW(szW) _data := this.GetEventLotterConfById(szID[index]) buff = append(buff, _data.EventID) } return } else if this.StypeE[key] == 2 { // 该小组中的道具为概率掉落,每个道具都会随机一次是否会掉落(单位为千分比) var wt int32 for _, v := range this._groupType2E[key] { if _data := this.GetEventLotterConfById(v); _data != nil { // 权重赋值 if _data.EventWt != 0 { wt = _data.EventWt } //fmt.Printf("大组类型1小组类型2获得道具 :%v, 该道具Cid:%d", _data.Itemid, v) if wt >= comm.GetRandNum(0, 1000) { // 命中 buff = append(buff, _data.EventID) } } } return } } else if this.BtypeE[lotteryId] == 2 { // 该大组中的小组为概率掉落,每个小组都会随机一次是否会掉落(单位为千分比) // 每个小组id 都随机取一次 var szGroupID []int32 // 获得的权重数组 gourp := make([]*cfg.GameEventlotteryData, 0) // key 小组ID value 权重 for _, v := range this._lotteryType2E[lotteryId] { if _data := this.GetEventLotterConfById(v); _data != nil { gourp = append(gourp, _data) } } var wt int32 // 类型为2 可能会同时获得多个组id for _, v := range gourp { if v.SubGroupWt != 0 { wt = v.SubGroupWt } k := v.SubGroupId //fmt.Printf("大组类型为2的,获得小组ID :%d,dropID:%d", k, v.Id) if wt >= comm.GetRandNum(0, 1000) { // 命中 szGroupID = append(szGroupID, k) key := int64(lotteryId)<<31 + int64(k) if this.StypeE[key] == 1 { // 随机一组数据 for i := 0; i < int(this.SNumE[key]); i++ { szW := make([]int32, 0) szID := make([]int32, 0) gourp := make(map[int32]int32, 0) for _, v := range this._groupType1E[key] { if _data := this.GetEventLotterConfById(v); _data != nil { // 权重赋值 if _, ok := gourp[_data.SubGroupId]; !ok { szW = append(szW, _data.EventWt) szID = append(szID, _data.Id) } } } index := comm.GetRandW(szW) _data := this.GetEventLotterConfById(szID[index]) buff = append(buff, _data.EventID) } } else if this.StypeE[key] == 2 { for _, v := range this._groupType2E[key] { var wt int32 if _data := this.GetEventLotterConfById(v); _data != nil { // 权重赋值 if _data.EventWt != 0 { wt = _data.EventWt } if wt >= comm.GetRandNum(1, 1000) { // 命中 buff = append(buff, _data.EventID) } } } } } } } return } func (this *configureComp) LoadRoomGroupData() { if v, err := this.GetConfigure(game_roomlottery); err == nil { if configure, ok := v.(*cfg.GameRoomlottery); ok { this.hlock.Lock() defer this.hlock.Unlock() this._groupR = make(map[int64][]int32, 0) this._lotteryType1R = make(map[int32][]int32, 0) this._lotteryType2R = make(map[int32][]int32, 0) this._groupType1R = make(map[int64][]int32, 0) this._groupType2R = make(map[int64][]int32, 0) this.BtypeR = make(map[int32]int32, 0) this.StypeR = make(map[int64]int32, 0) this.SNumR = make(map[int64]int32, 0) for _, value := range configure.GetDataList() { key := int64(value.GroupId)<<31 + int64(value.SubGroupId) this._groupR[key] = append(this._groupR[key], value.Id) if _, ok := this.BtypeR[value.GroupId]; !ok { this.BtypeR[value.GroupId] = value.GroupType } if _, ok := this.StypeR[key]; !ok { this.StypeR[key] = value.SubGroupType } if _, ok := this.SNumR[key]; !ok { this.SNumR[key] = value.SubGroupNum } if this.BtypeR[value.GroupId] == 1 { this._lotteryType1R[value.GroupId] = append(this._lotteryType1R[value.GroupId], value.Id) } else if this.BtypeR[value.GroupId] == 2 { this._lotteryType2R[value.GroupId] = append(this._lotteryType2R[value.GroupId], value.Id) } if this.StypeR[key] == 1 { // 小组ID为1 this._groupType1R[key] = append(this._groupType1R[key], value.Id) } else if this.StypeR[key] == 2 { this._groupType2R[key] = append(this._groupType2R[key], value.Id) } } return } } else { log.Errorf("get NewGameBufflottery conf err:%v", err) } return } func (this *configureComp) GetRoomLotterConfById(id int32) (data *cfg.GameRoomlotteryData) { if v, err := this.GetConfigure(game_roomlottery); err == nil { if configure, ok := v.(*cfg.GameRoomlottery); ok { return configure.Get(id) } } return } // 实际掉落逻辑 (传入 掉落组ID vip等级 玩家等级 返回获得的道具) func (this *configureComp) GetRoomGroupDataByLottery(lotteryId int32) (buff []int32) { if _, ok := this._lotteryType1R[lotteryId]; !ok { if _, ok := this._lotteryType2R[lotteryId]; !ok { fmt.Printf("not found config lotterId:%d", lotteryId) return } } // 优先校验大组ID 的类型 if this.BtypeR[lotteryId] == 1 { // 该大组中的小组为权重掉落,必定从N个小组中随机出1个小组 var ( szW []int32 // 权重数组 szID []int32 // 小组ID 数组 groupID int32 gourp map[int32]int32 // key 小组ID value 权重 ) gourp = make(map[int32]int32, 0) // 随机小组id for _, v := range this._lotteryType1R[lotteryId] { if _data := this.GetRoomLotterConfById(v); _data != nil { if _, ok := gourp[_data.SubGroupId]; !ok { gourp[_data.SubGroupId] = _data.SubGroupWt // 小组ID 权重赋值 szW = append(szW, _data.SubGroupWt) szID = append(szID, _data.SubGroupId) } } } groupID = szID[comm.GetRandW(szW)] // 获得小组ID //fmt.Printf("大组类型为1的,获得小组ID :%d,dropID:%d", groupID, lotteryId) key := int64(lotteryId)<<31 + int64(groupID) // 小组ID 类型判断 if this.StypeR[key] == 1 { // 该小组的道具为权重掉落,必定从N个道具中随机出1个道具 for i := 0; i < int(this.SNumR[key]); i++ { szW = make([]int32, 0) szID = make([]int32, 0) gourp = make(map[int32]int32, 0) for _, v := range this._groupType1R[key] { if _data := this.GetRoomLotterConfById(v); _data != nil { // 权重赋值 if _, ok := gourp[_data.SubGroupId]; !ok { szW = append(szW, _data.RoomWt) szID = append(szID, _data.Id) } } } index := comm.GetRandW(szW) _data := this.GetRoomLotterConfById(szID[index]) buff = append(buff, _data.RoomID) } return } else if this.StypeR[key] == 2 { // 该小组中的道具为概率掉落,每个道具都会随机一次是否会掉落(单位为千分比) var wt int32 for _, v := range this._groupType2R[key] { if _data := this.GetRoomLotterConfById(v); _data != nil { // 权重赋值 if _data.RoomWt != 0 { wt = _data.RoomWt } //fmt.Printf("大组类型1小组类型2获得道具 :%v, 该道具Cid:%d", _data.Itemid, v) if wt >= comm.GetRandNum(0, 1000) { // 命中 buff = append(buff, _data.RoomID) } } } return } } else if this.BtypeR[lotteryId] == 2 { // 该大组中的小组为概率掉落,每个小组都会随机一次是否会掉落(单位为千分比) // 每个小组id 都随机取一次 var szGroupID []int32 // 获得的权重数组 gourp := make([]*cfg.GameRoomlotteryData, 0) // key 小组ID value 权重 for _, v := range this._lotteryType2R[lotteryId] { if _data := this.GetRoomLotterConfById(v); _data != nil { gourp = append(gourp, _data) } } var wt int32 // 类型为2 可能会同时获得多个组id for _, v := range gourp { if v.SubGroupWt != 0 { wt = v.SubGroupWt } k := v.SubGroupId //fmt.Printf("大组类型为2的,获得小组ID :%d,dropID:%d", k, v.Id) if wt >= comm.GetRandNum(0, 1000) { // 命中 szGroupID = append(szGroupID, k) key := int64(lotteryId)<<31 + int64(k) if this.StypeR[key] == 1 { // 随机一组数据 for i := 0; i < int(this.SNumR[key]); i++ { szW := make([]int32, 0) szID := make([]int32, 0) gourp := make(map[int32]int32, 0) for _, v := range this._groupType1R[key] { if _data := this.GetRoomLotterConfById(v); _data != nil { // 权重赋值 if _, ok := gourp[_data.SubGroupId]; !ok { szW = append(szW, _data.RoomWt) szID = append(szID, _data.Id) } } } index := comm.GetRandW(szW) _data := this.GetRoomLotterConfById(szID[index]) buff = append(buff, _data.RoomID) } } else if this.StypeR[key] == 2 { for _, v := range this._groupType2R[key] { var wt int32 if _data := this.GetRoomLotterConfById(v); _data != nil { // 权重赋值 if _data.RoomWt != 0 { wt = _data.RoomWt } if wt >= comm.GetRandNum(1, 1000) { // 命中 buff = append(buff, _data.RoomID) } } } } } } } return } func (this *configureComp) LoadBUffGroupData() { if v, err := this.GetConfigure(game_bufflottery); err == nil { if configure, ok := v.(*cfg.GameBufflottery); ok { this.hlock.Lock() defer this.hlock.Unlock() this._group = make(map[int64][]int32, 0) this._lotteryType1 = make(map[int32][]int32, 0) this._lotteryType2 = make(map[int32][]int32, 0) this._groupType1 = make(map[int64][]int32, 0) this._groupType2 = make(map[int64][]int32, 0) this.Btype = make(map[int32]int32, 0) this.Stype = make(map[int64]int32, 0) this.SNum = make(map[int64]int32, 0) for _, value := range configure.GetDataList() { key := int64(value.GroupId)<<31 + int64(value.SubGroupId) this._group[key] = append(this._group[key], value.Id) if _, ok := this.Btype[value.GroupId]; !ok { this.Btype[value.GroupId] = value.GroupType } if _, ok := this.Stype[key]; !ok { this.Stype[key] = value.SubGroupType } if _, ok := this.SNum[key]; !ok { this.SNum[key] = value.SubGroupNum } if this.Btype[value.GroupId] == 1 { this._lotteryType1[value.GroupId] = append(this._lotteryType1[value.GroupId], value.Id) } else if this.Btype[value.GroupId] == 2 { this._lotteryType2[value.GroupId] = append(this._lotteryType2[value.GroupId], value.Id) } if this.Stype[key] == 1 { // 小组ID为1 this._groupType1[key] = append(this._groupType1[key], value.Id) } else if this.Stype[key] == 2 { this._groupType2[key] = append(this._groupType2[key], value.Id) } } return } } else { log.Errorf("get NewGameBufflottery conf err:%v", err) } return } func (this *configureComp) GetLotterConfById(id int32) (data *cfg.GameBufflotteryData) { if v, err := this.GetConfigure(game_bufflottery); err == nil { if configure, ok := v.(*cfg.GameBufflottery); ok { return configure.Get(id) } } return } // 实际掉落逻辑 (传入 掉落组ID vip等级 玩家等级 返回获得的道具) func (this *configureComp) GetBuffGroupDataByLottery(lotteryId int32) (buff []int32) { if _, ok := this._lotteryType1[lotteryId]; !ok { if _, ok := this._lotteryType2[lotteryId]; !ok { fmt.Printf("not found config lotterId:%d", lotteryId) return } } // 优先校验大组ID 的类型 if this.Btype[lotteryId] == 1 { // 该大组中的小组为权重掉落,必定从N个小组中随机出1个小组 var ( szW []int32 // 权重数组 szID []int32 // 小组ID 数组 groupID int32 gourp map[int32]int32 // key 小组ID value 权重 ) gourp = make(map[int32]int32, 0) // 随机小组id for _, v := range this._lotteryType1[lotteryId] { if _data := this.GetLotterConfById(v); _data != nil { if _, ok := gourp[_data.SubGroupId]; !ok { gourp[_data.SubGroupId] = _data.SubGroupWt // 小组ID 权重赋值 szW = append(szW, _data.SubGroupWt) szID = append(szID, _data.SubGroupId) } } } groupID = szID[comm.GetRandW(szW)] // 获得小组ID //fmt.Printf("大组类型为1的,获得小组ID :%d,dropID:%d", groupID, lotteryId) key := int64(lotteryId)<<31 + int64(groupID) // 小组ID 类型判断 if this.Stype[key] == 1 { // 该小组的道具为权重掉落,必定从N个道具中随机出1个道具 for i := 0; i < int(this.SNum[key]); i++ { szW = make([]int32, 0) szID = make([]int32, 0) gourp = make(map[int32]int32, 0) for _, v := range this._groupType1[key] { if _data := this.GetLotterConfById(v); _data != nil { // 权重赋值 if _, ok := gourp[_data.SubGroupId]; !ok { szW = append(szW, _data.BuffWt) szID = append(szID, _data.Id) } } } index := comm.GetRandW(szW) _data := this.GetLotterConfById(szID[index]) buff = append(buff, _data.BuffID) } return } else if this.Stype[key] == 2 { // 该小组中的道具为概率掉落,每个道具都会随机一次是否会掉落(单位为千分比) var wt int32 for _, v := range this._groupType2[key] { if _data := this.GetLotterConfById(v); _data != nil { // 权重赋值 if _data.BuffWt != 0 { wt = _data.BuffWt } //fmt.Printf("大组类型1小组类型2获得道具 :%v, 该道具Cid:%d", _data.Itemid, v) if wt >= comm.GetRandNum(0, 1000) { // 命中 buff = append(buff, _data.BuffID) } } } return } } else if this.Btype[lotteryId] == 2 { // 该大组中的小组为概率掉落,每个小组都会随机一次是否会掉落(单位为千分比) // 每个小组id 都随机取一次 var szGroupID []int32 // 获得的权重数组 gourp := make([]*cfg.GameBufflotteryData, 0) // key 小组ID value 权重 for _, v := range this._lotteryType2[lotteryId] { if _data := this.GetLotterConfById(v); _data != nil { gourp = append(gourp, _data) } } var wt int32 // 类型为2 可能会同时获得多个组id for _, v := range gourp { if v.SubGroupWt != 0 { wt = v.SubGroupWt } k := v.SubGroupId //fmt.Printf("大组类型为2的,获得小组ID :%d,dropID:%d", k, v.Id) if wt >= comm.GetRandNum(0, 1000) { // 命中 szGroupID = append(szGroupID, k) key := int64(lotteryId)<<31 + int64(k) if this.Stype[key] == 1 { // 随机一组数据 for i := 0; i < int(this.SNum[key]); i++ { szW := make([]int32, 0) szID := make([]int32, 0) gourp := make(map[int32]int32, 0) for _, v := range this._groupType1[key] { if _data := this.GetLotterConfById(v); _data != nil { // 权重赋值 if _, ok := gourp[_data.SubGroupId]; !ok { szW = append(szW, _data.BuffWt) szID = append(szID, _data.Id) } } } index := comm.GetRandW(szW) _data := this.GetLotterConfById(szID[index]) buff = append(buff, _data.BuffID) } } else if this.Stype[key] == 2 { for _, v := range this._groupType2[key] { var wt int32 if _data := this.GetLotterConfById(v); _data != nil { // 权重赋值 if _data.BuffWt != 0 { wt = _data.BuffWt } if wt >= comm.GetRandNum(1, 1000) { // 命中 buff = append(buff, _data.BuffID) } } } } } } } return }