extend_drop_pool.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package data
  2. import (
  3. "f1-game/internal/code"
  4. "f1-game/internal/extend/rand"
  5. )
  6. var (
  7. dropPoolWeightMap map[int32]*rand.Weight[*DropPoolRow]
  8. )
  9. func (p *dropPoolConfig) OnAfterLoad(_ bool) {
  10. loadMap := map[int32]*rand.Weight[*DropPoolRow]{}
  11. for poolID, rows := range p.idListMaps {
  12. weight := rand.NewSliceWeight(rows, func(v *DropPoolRow) int32 {
  13. return v.Weight
  14. })
  15. loadMap[poolID] = weight
  16. }
  17. dropPoolWeightMap = loadMap
  18. }
  19. func (p *dropPoolConfig) Random(dropPoolID int32) (*DropPoolRow, int32) {
  20. weighter, found := p.GetWeighter(dropPoolID)
  21. if !found {
  22. //道具池找不到
  23. return nil, code.ConfigNotFound_DropPool
  24. }
  25. // 随机取一个奖励
  26. row := weighter.SelectOne()
  27. if row == nil {
  28. return nil, code.HeroCannotRecruit
  29. }
  30. return row, code.OK
  31. }
  32. func (p *dropPoolConfig) GetWeighter(itemPoolID int32) (*rand.Weight[*DropPoolRow], bool) {
  33. value, found := dropPoolWeightMap[itemPoolID]
  34. return value, found
  35. }
  36. func (p *dropPoolConfig) GetDropPoolRow(poolID, itemID int32) (*DropPoolRow, bool) {
  37. rows, found := p.ListByID(poolID)
  38. if !found {
  39. return nil, false
  40. }
  41. for _, row := range rows {
  42. if row.ItemID == itemID {
  43. return row, true
  44. }
  45. }
  46. return nil, false
  47. }