growthpackmgr.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package giftpack
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "sync"
  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 growthpackmgr struct {
  13. packs []*GrowthPack
  14. userTerms map[int][]*UserGrowthTerm
  15. lock *sync.RWMutex
  16. }
  17. func newGrowthPackManager() *growthpackmgr {
  18. gpm := new(growthpackmgr)
  19. gpm.userTerms = make(map[int][]*UserGrowthTerm)
  20. gpm.lock = &sync.RWMutex{}
  21. gpm.loadPacks()
  22. return gpm
  23. }
  24. func (gpm *growthpackmgr) onUserEnter(userId int) {
  25. list := gpm.loadUserTerms(userId)
  26. gpm.lock.Lock()
  27. defer gpm.lock.Unlock()
  28. gpm.userTerms[userId] = list
  29. }
  30. func (gpm *growthpackmgr) onUserExit(userId int) {
  31. gpm.lock.Lock()
  32. defer gpm.lock.Unlock()
  33. delete(gpm.userTerms, userId)
  34. }
  35. func (gpm *growthpackmgr) getGrowthPacks() []*GrowthPack {
  36. gpm.lock.RLock()
  37. defer gpm.lock.RUnlock()
  38. return gpm.packs
  39. }
  40. func (gpm *growthpackmgr) getGrowthPack(packId int) *GrowthPack {
  41. gpm.lock.RLock()
  42. defer gpm.lock.RUnlock()
  43. for _, v := range gpm.packs {
  44. if v.Id == packId {
  45. return v
  46. }
  47. }
  48. return nil
  49. }
  50. func (gpm *growthpackmgr) getGrowthPackByProduct(productId string) *GrowthPack {
  51. gpm.lock.RLock()
  52. defer gpm.lock.RUnlock()
  53. for _, v := range gpm.packs {
  54. if v.ProductId == productId {
  55. return v
  56. }
  57. }
  58. return nil
  59. }
  60. func (gpm *growthpackmgr) getUserGrowthTerms(userId int) []*UserGrowthTerm {
  61. gpm.lock.RLock()
  62. ret, ok := gpm.userTerms[userId]
  63. gpm.lock.RUnlock()
  64. if !ok {
  65. ret = gpm.loadUserTerms(userId)
  66. gpm.lock.Lock()
  67. gpm.userTerms[userId] = ret
  68. gpm.lock.Unlock()
  69. }
  70. return ret
  71. }
  72. func (gpm *growthpackmgr) buyGrowthPack(userId int, productId string) (int, string, []item.ItemPack) {
  73. var items []item.ItemPack
  74. pack := gpm.getGrowthPackByProduct(productId)
  75. if pack == nil {
  76. log.Debug("growthpackmgr.buyGrowthPack userId[%d] productId[%s] invalid", userId, productId)
  77. return 0, "无效的礼包ID", items
  78. }
  79. userTerms := gpm.getUserGrowthTerms(userId)
  80. for _, v := range userTerms {
  81. if v.GrowthPackId == pack.Id {
  82. log.Debug("growthpackmgr.buyGrowthPack userId[%d] packId[%d] perchased", userId, pack.Id)
  83. return 0, "该礼包已经购买过了", items
  84. }
  85. }
  86. for _, v := range pack.Terms {
  87. userTerms = append(userTerms, &UserGrowthTerm{
  88. UserId: userId,
  89. GrowthPackId: pack.Id,
  90. TermIndex: v.TermIndex,
  91. Status: 0,
  92. Bullet: v.Bullet,
  93. })
  94. //items = append(items, v.Items...)
  95. }
  96. gpm.lock.Lock()
  97. gpm.userTerms[userId] = userTerms
  98. gpm.lock.Unlock()
  99. gpm.updateUserTerms(userTerms)
  100. return 1, "购买成功", items
  101. }
  102. func (gpm *growthpackmgr) getGrowthTerm(growthPackId, index int) (GrowthTerm, bool) {
  103. gpm.lock.RLock()
  104. defer gpm.lock.RUnlock()
  105. for _, pack := range gpm.packs {
  106. if growthPackId != pack.Id {
  107. continue
  108. }
  109. for _, term := range pack.Terms {
  110. if term.TermIndex == index {
  111. return term, true
  112. }
  113. }
  114. }
  115. return GrowthTerm{}, false
  116. }
  117. func (gpm *growthpackmgr) userAwardTerm(userId int, growthPackId, index int) (bool, string) {
  118. bought := false
  119. term, ok := gpm.getGrowthTerm(growthPackId, index)
  120. if !ok {
  121. log.Debug("growthpackmgr.userAwardTerm invalid params %d.%d", growthPackId, index)
  122. return false, "未找到礼包"
  123. }
  124. userTerms := gpm.getUserGrowthTerms(userId)
  125. for _, v := range userTerms {
  126. if v.GrowthPackId != growthPackId {
  127. continue
  128. }
  129. bought = true
  130. if v.TermIndex != index {
  131. continue
  132. }
  133. if v.Status != GrowthTermStatus_complete {
  134. return false, "不符合领取条件"
  135. } else {
  136. inventory.AddItems(userId, term.Items, fmt.Sprintf("成长礼包[%d]", growthPackId), common.LOGTYPE_PACK_GROWTH)
  137. v.Status = GrowthTermStatus_awarded
  138. gpm.updateUserTerm(v)
  139. return true, "领取成功"
  140. }
  141. }
  142. if bought {
  143. return false, "领取失败"
  144. } else {
  145. return false, "未购买礼包"
  146. }
  147. }
  148. func (gpm *growthpackmgr) loadPacks() {
  149. // 操作数据库
  150. list := getGrowthPackList()
  151. gpm.lock.Lock()
  152. defer gpm.lock.Unlock()
  153. gpm.packs = list
  154. }
  155. func (gpm *growthpackmgr) loadUserTerms(userId int) []*UserGrowthTerm {
  156. var ret []*UserGrowthTerm
  157. // 操作数据库
  158. ret = getUserGrowthPackList(userId)
  159. return ret
  160. }
  161. func (gpm *growthpackmgr) updateUserTerms(userTerms []*UserGrowthTerm) {
  162. // 操作数据库
  163. for _, v := range userTerms {
  164. gpm.updateUserTerm(v)
  165. }
  166. }
  167. func (gpm *growthpackmgr) updateUserTerm(userTerm *UserGrowthTerm) {
  168. go updateUserGrowthPack(userTerm)
  169. }
  170. func (gpm *growthpackmgr) dumpSys(param string) {
  171. log.Release("-------------------------------")
  172. log.Release("growthpackmgr.dumpSys %s", param)
  173. defer func() {
  174. log.Release("+++++++++++++++++++++++++++++++")
  175. log.Release("")
  176. }()
  177. d, _ := json.Marshal(gpm.packs)
  178. log.Release(string(d))
  179. }
  180. func (gpm *growthpackmgr) dumpUser(param string) {
  181. log.Release("-------------------------------")
  182. log.Release("growthpackmgr.dumpUser %s", param)
  183. defer func() {
  184. log.Release("+++++++++++++++++++++++++++++++")
  185. log.Release("")
  186. }()
  187. var userId int
  188. var err error
  189. if userId, err = strconv.Atoi(param); err != nil {
  190. log.Release("atoi error %v", err)
  191. return
  192. }
  193. si := gpm.getUserGrowthTerms(userId)
  194. if si == nil {
  195. log.Release("user %d not exist", userId)
  196. return
  197. }
  198. d, _ := json.Marshal(si)
  199. log.Release(string(d))
  200. }