awardmgr.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package award
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "sync"
  6. "bet24.com/servers/coreservice/agent"
  7. "bet24.com/log"
  8. "bet24.com/servers/common"
  9. inventory "bet24.com/servers/micros/item_inventory/proto"
  10. item "bet24.com/servers/micros/item_inventory/proto"
  11. )
  12. type awardMgr struct {
  13. lock *sync.RWMutex
  14. list []*awardInfo
  15. }
  16. // 奖励信息
  17. type awardInfo struct {
  18. ActiveID int // 活动ID
  19. ActiveName string // 活动名称
  20. TotalTimes int // 总次数
  21. PerDay int // 每隔几天
  22. Awards []awards // 奖励
  23. }
  24. // 奖励内容
  25. type awards struct {
  26. Chance int // 概率(以万为基数)
  27. Items []item.ItemPack // 道具
  28. }
  29. func newAwardMgr() *awardMgr {
  30. ret := new(awardMgr)
  31. ret.lock = &sync.RWMutex{}
  32. ret.loadData()
  33. log.Debug("award manager running")
  34. return ret
  35. }
  36. func (this *awardMgr) loadData() {
  37. list := getSysInfo()
  38. for i := 0; i < len(list); i++ {
  39. chance := 0
  40. for j := 0; j < len(list[i].Awards); j++ {
  41. chance += list[i].Awards[j].Chance * 100
  42. list[i].Awards[j].Chance = chance
  43. }
  44. }
  45. this.lock.Lock()
  46. defer this.lock.Unlock()
  47. this.list = list
  48. for _, v := range this.list {
  49. log.Debug("awardmgr.loadData ActiveId[%d] ActiveName[%s] ==> %+v", v.ActiveID, v.ActiveName, v)
  50. }
  51. }
  52. // 获取信息
  53. func (this *awardMgr) getInfo(activeId int) *awardInfo {
  54. this.lock.RLock()
  55. defer this.lock.RUnlock()
  56. for _, v := range this.list {
  57. if v.ActiveID == activeId {
  58. return v
  59. }
  60. }
  61. return nil
  62. }
  63. // 发送奖励
  64. func (this *awardMgr) sendAward(userId, activeId, hallType int, ipAddress string) (int, []item.ItemPack) {
  65. info := this.getInfo(activeId)
  66. if info == nil {
  67. log.Debug("award.awardmgr.sendAward userId=%d activeId=%d is not exist", userId, activeId)
  68. return 11, nil
  69. }
  70. if info.TotalTimes <= 0 {
  71. log.Debug("award.awardmgr.sendAward userId=%d activeId=%d info=%v is invalid", userId, activeId, info)
  72. return 12, nil
  73. }
  74. logType := 0
  75. // 代理活动
  76. if activeId == ActiveAgentShare {
  77. // 判断是否是代理
  78. if g := agent.Info(userId); g.Enabled != 1 {
  79. return 11, nil
  80. }
  81. logType = common.LOGTYPE_AWARD_AGENT_SHARE
  82. } else if activeId == ActiveTaskAnswer {
  83. logType = common.LOGTYPE_AWARD_ANSWER
  84. }
  85. retCode := sendAward(userId, info.ActiveID, info.TotalTimes, info.PerDay, ipAddress)
  86. if retCode != 1 {
  87. return retCode, nil
  88. }
  89. // 生成随机数
  90. rnd := rand.Intn(10000) + 1
  91. // 抽中的道具
  92. var hitItems []item.ItemPack
  93. for _, v := range info.Awards {
  94. hitItems = v.Items
  95. if rnd < v.Chance {
  96. break
  97. }
  98. }
  99. // log.Debug("awardmgr.sendAward userId=%d activeId=%d hallType=%d rnd=%d hitItems=%+v",
  100. // userId, activeId, hallType, rnd, hitItems)
  101. var items []item.ItemPack
  102. // 3=不区分大厅类型
  103. if hallType == 3 {
  104. items = hitItems
  105. } else {
  106. for _, v := range hitItems {
  107. if hallType == 1 { // 1=金币大厅
  108. if v.ItemId == item.Item_Gold {
  109. items = append(items, v)
  110. }
  111. } else if hallType == 2 { // 2=元宝大厅
  112. if v.ItemId == item.Item_Chip {
  113. items = append(items, v)
  114. }
  115. }
  116. }
  117. }
  118. remark := fmt.Sprintf("奖励模块[%s]", info.ActiveName)
  119. go inventory.AddItems(userId, items, remark, logType)
  120. return retCode, items
  121. }