monthlycardmgr.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package monthlycard
  2. import (
  3. "sync"
  4. "bet24.com/log"
  5. "bet24.com/servers/common"
  6. inventory "bet24.com/servers/micros/item_inventory/proto"
  7. item "bet24.com/servers/micros/item_inventory/proto"
  8. )
  9. type monthlycardmgr struct {
  10. user_list map[int]*usermonthlycard
  11. sys_list []*sysInfo
  12. lock *sync.RWMutex
  13. }
  14. type sysInfo struct {
  15. ProductID string //产品ID
  16. Name string //名称
  17. Price int //价格
  18. Awards []item.ItemPack //立即赠送的物品
  19. DayItems []item.ItemPack //每天物品
  20. Memo string //备注
  21. }
  22. func NewMonthlyCardMgr() *monthlycardmgr {
  23. obj := new(monthlycardmgr)
  24. obj.user_list = make(map[int]*usermonthlycard)
  25. obj.lock = &sync.RWMutex{}
  26. obj.loadItems()
  27. log.Debug("monthlycard manager running")
  28. return obj
  29. }
  30. func (mc *monthlycardmgr) loadItems() {
  31. list := getSysInfo()
  32. mc.lock.Lock()
  33. defer mc.lock.Unlock()
  34. mc.sys_list = append(mc.sys_list, list...)
  35. }
  36. func (mc *monthlycardmgr) getUserInfo(userId int) *usermonthlycard {
  37. mc.lock.RLock()
  38. user, ok := mc.user_list[userId]
  39. mc.lock.RUnlock()
  40. if !ok {
  41. user = newUserMonthlyCard(userId)
  42. }
  43. user.checkMonthlyStatus()
  44. return user
  45. }
  46. func (mc *monthlycardmgr) getSysList() []*sysInfo {
  47. mc.lock.RLock()
  48. defer mc.lock.RUnlock()
  49. return mc.sys_list
  50. }
  51. func (mc *monthlycardmgr) getInfo(productId string) *sysInfo {
  52. for _, v := range mc.sys_list {
  53. if v.ProductID == productId {
  54. return v
  55. }
  56. }
  57. return nil
  58. }
  59. func (mc *monthlycardmgr) buyMonth(userId int) (int, []item.ItemPack) {
  60. u := mc.getUserInfo(userId)
  61. if u == nil {
  62. log.Debug("monthlycard.buyMonth user[%d] is not exist", userId)
  63. u = newUserMonthlyCard(userId)
  64. }
  65. //购买
  66. if ret := u.buyMonth(); ret != 1 {
  67. return 0, nil
  68. }
  69. return 1, nil
  70. }
  71. func (mc *monthlycardmgr) giftMonth(userId int) (int, []item.ItemPack) {
  72. u := mc.getUserInfo(userId)
  73. if u == nil {
  74. //log.Debug("monthlycard.giftMonth user[%d] is not exist", userId)
  75. return 0, nil
  76. }
  77. //领取
  78. retCode := u.giftMonth()
  79. if retCode != 1 {
  80. return retCode, nil
  81. }
  82. info := mc.getInfo(Month_ProductId)
  83. inventory.AddItems(userId, info.DayItems, "领取月卡福利", common.LOGTYPE_SEND_MONTHLYCARD)
  84. return 1, info.DayItems
  85. }
  86. func (mc *monthlycardmgr) checkMonthTip(userId int) bool {
  87. u := mc.getUserInfo(userId)
  88. if u == nil {
  89. log.Debug("monthlycard.giftMonth user[%d] is not exist", userId)
  90. return false
  91. }
  92. return u.checkMonthTip()
  93. }
  94. func (mc *monthlycardmgr) buyWeek(userId int) (int, []item.ItemPack) {
  95. u := mc.getUserInfo(userId)
  96. if u == nil {
  97. log.Debug("monthlycard.buyWeek user[%d] is not exist", userId)
  98. u = newUserMonthlyCard(userId)
  99. }
  100. //购买
  101. if ret := u.buyWeek(); ret != 1 {
  102. return 0, nil
  103. }
  104. return 1, nil
  105. }
  106. func (mc *monthlycardmgr) giftWeek(userId int) (int, []item.ItemPack) {
  107. u := mc.getUserInfo(userId)
  108. if u == nil {
  109. log.Debug("monthlycard.giftWeek user[%d] is not exist", userId)
  110. return 0, nil
  111. }
  112. //领取
  113. retCode := u.giftWeek()
  114. if retCode != 1 {
  115. return 0, nil
  116. }
  117. info := mc.getInfo(Week_ProductId)
  118. //领取
  119. inventory.AddItems(userId, info.DayItems, "领取周卡福利", common.LOGTYPE_SEND_MONTHLYCARD)
  120. return 1, info.DayItems
  121. }
  122. func (mc *monthlycardmgr) onUserEnter(userId int) {
  123. mc.lock.RLock()
  124. _, ok := mc.user_list[userId]
  125. mc.lock.RUnlock()
  126. if !ok {
  127. o := newUserMonthlyCard(userId)
  128. mc.lock.Lock()
  129. mc.user_list[userId] = o
  130. mc.lock.Unlock()
  131. }
  132. }
  133. func (mc *monthlycardmgr) onUserExit(userId int) {
  134. mc.lock.Lock()
  135. defer mc.lock.Unlock()
  136. delete(mc.user_list, userId)
  137. }