consecutive_manager.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package ladder
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/common"
  5. inventory "bet24.com/servers/micros/item_inventory/proto"
  6. item "bet24.com/servers/micros/item_inventory/proto"
  7. "strconv"
  8. "sync"
  9. "time"
  10. )
  11. var conMgr *consecutiveMgr
  12. func getConsecutiveMgr() *consecutiveMgr {
  13. if conMgr == nil {
  14. conMgr = new(consecutiveMgr)
  15. conMgr.ctor()
  16. }
  17. return conMgr
  18. }
  19. type consecutiveMgr struct {
  20. lock *sync.RWMutex
  21. userRecords map[int]*userConsecutiveRecord
  22. }
  23. func (cm *consecutiveMgr) ctor() {
  24. log.Release("ladder.consecutiveMgr.ctor()")
  25. cm.lock = &sync.RWMutex{}
  26. cm.userRecords = make(map[int]*userConsecutiveRecord)
  27. }
  28. func (cm *consecutiveMgr) checkExpireUser() {
  29. time.AfterFunc(10*time.Minute, cm.checkExpireUser)
  30. var toRemove []int
  31. cm.lock.RLock()
  32. for k, v := range cm.userRecords {
  33. if v.isExpired() {
  34. toRemove = append(toRemove, k)
  35. }
  36. }
  37. cm.lock.RUnlock()
  38. if len(toRemove) == 0 {
  39. return
  40. }
  41. log.Release("consecutiveMgr.checkExpireUser removing %v", toRemove)
  42. cm.lock.Lock()
  43. for _, v := range toRemove {
  44. delete(cm.userRecords, v)
  45. }
  46. cm.lock.Unlock()
  47. }
  48. func (cm *consecutiveMgr) loadUserRecord(userId int) *userConsecutiveRecord {
  49. return newUserConsecutiveRecord(userId)
  50. }
  51. func (cm *consecutiveMgr) getUserRecord(userId int) *userConsecutiveRecord {
  52. cm.lock.RLock()
  53. ur, ok := cm.userRecords[userId]
  54. cm.lock.RUnlock()
  55. if !ok {
  56. ur = cm.loadUserRecord(userId)
  57. cm.lock.Lock()
  58. cm.userRecords[userId] = ur
  59. cm.lock.Unlock()
  60. }
  61. return ur
  62. }
  63. func (cm *consecutiveMgr) getUserConsecutiveWinCount(userId int, gameId int) int {
  64. ur := cm.getUserRecord(userId)
  65. if ur == nil {
  66. log.Release("consecutiveMgr.getUserConsecutiveWinCount[%d] user not exist", userId)
  67. return 0
  68. }
  69. return ur.getConsecutiveWinCount(gameId)
  70. }
  71. func (cm *consecutiveMgr) addUserRecord(userId int, gameId int, score int) {
  72. ur := cm.getUserRecord(userId)
  73. if ur == nil {
  74. log.Release("consecutiveMgr.addUserRecord[%d] user not exist", userId)
  75. return
  76. }
  77. ur.addRecord(score, gameId)
  78. }
  79. func (cm *consecutiveMgr) removeLastLost(userId int, gameId int) bool {
  80. ur := cm.getUserRecord(userId)
  81. if ur == nil {
  82. log.Release("consecutiveMgr.removeLastLost[%d] user not exist", userId)
  83. return false
  84. }
  85. // 检查道具是否有
  86. price := getLadderConfig().ConsecutiveCardPrice
  87. if price.Count > 0 {
  88. ok, errMsg := inventory.Consume(userId, price.ItemId, common.LOGTYPE_LADDER_CONSECUTIVE_CARD, price.Count, 0)
  89. if !ok {
  90. log.Release("consecutiveMgr.removeLastLost[%d] Consume failed %s", userId, errMsg)
  91. return false
  92. }
  93. }
  94. removeOk := ur.removeLastLost(gameId)
  95. if !removeOk {
  96. // 道具加回去
  97. if price.Count > 0 {
  98. inventory.AddItems(userId, []item.ItemPack{price}, "CONSECUTIVE_CARD return", common.LOGTYPE_LADDER_CONSECUTIVE_CARD)
  99. }
  100. }
  101. return removeOk
  102. }
  103. func (cm *consecutiveMgr) dump(param string) {
  104. log.Release("-------------------------------")
  105. log.Release("consecutiveMgr.dump")
  106. defer func() {
  107. log.Release("+++++++++++++++++++++++++++++++")
  108. log.Release("")
  109. }()
  110. var userId int
  111. var err error
  112. if userId, err = strconv.Atoi(param); err != nil {
  113. log.Release("atoi error %v", err)
  114. return
  115. }
  116. ur := cm.getUserRecord(userId)
  117. if ur == nil {
  118. log.Release(" no record")
  119. return
  120. }
  121. ur.dump()
  122. }