extend_drop_pool.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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) RandomOne(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) Random(dropPoolID int32, num int32) ([]*DropPoolRow, int32) {
  33. weighter, found := p.GetWeighter(dropPoolID)
  34. if !found {
  35. //道具池找不到
  36. return nil, code.ConfigNotFound_DropPool
  37. }
  38. // 随机num个奖励
  39. rows := weighter.Select(int(num))
  40. if rows == nil {
  41. return nil, code.HeroCannotRecruit
  42. }
  43. return rows, code.OK
  44. }
  45. func (p *dropPoolConfig) GetWeighter(itemPoolID int32) (*rand.Weight[*DropPoolRow], bool) {
  46. value, found := dropPoolWeightMap[itemPoolID]
  47. return value, found
  48. }
  49. func (p *dropPoolConfig) GetDropPoolRow(poolID, itemID int32) (*DropPoolRow, bool) {
  50. rows, found := p.ListByID(poolID)
  51. if !found {
  52. return nil, false
  53. }
  54. for _, row := range rows {
  55. if row.ItemID == itemID {
  56. return row, true
  57. }
  58. }
  59. return nil, false
  60. }