scoremanager.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package slotscore
  2. import (
  3. "fmt"
  4. "strconv"
  5. "sync"
  6. "bet24.com/log"
  7. "bet24.com/servers/common"
  8. chip "bet24.com/servers/micros/money/proto"
  9. notification "bet24.com/servers/micros/notification/proto"
  10. )
  11. var mgr *scoremanager
  12. func createScoreManager() {
  13. mgr = new(scoremanager)
  14. mgr.ctor()
  15. }
  16. type scoremanager struct {
  17. exchange *slotexchange
  18. lock *sync.RWMutex
  19. userscores map[int]int
  20. isDirty bool
  21. historyMgr *historymanager
  22. }
  23. func (sc *scoremanager) ctor() {
  24. sc.exchange = newSlotExchange()
  25. sc.lock = &sync.RWMutex{}
  26. sc.userscores = make(map[int]int)
  27. sc.loadUserScoreFromRedis()
  28. sc.flush()
  29. sc.historyMgr = newHistoryManager()
  30. }
  31. func (sc *scoremanager) getExchangeList() string {
  32. return sc.exchange.getList()
  33. }
  34. func (sc *scoremanager) addScore(userId int, score int) int {
  35. if !sc.exchange.isExchangeVaid() {
  36. return 0
  37. }
  38. sc.lock.Lock()
  39. sc.userscores[userId] += score
  40. ret := sc.userscores[userId]
  41. sc.isDirty = true
  42. sc.lock.Unlock()
  43. notification.AddNotification(userId, notification.Notification_SlotScore, fmt.Sprintf("%d", ret))
  44. return ret
  45. }
  46. func (sc *scoremanager) getScore(userId int) int {
  47. sc.lock.RLock()
  48. defer sc.lock.RUnlock()
  49. return sc.userscores[userId]
  50. }
  51. func (sc *scoremanager) reduceScore(userId int, score int) bool {
  52. sc.lock.Lock()
  53. defer sc.lock.Unlock()
  54. if sc.userscores[userId] < score {
  55. return false
  56. }
  57. sc.userscores[userId] -= score
  58. sc.isDirty = true
  59. return true
  60. }
  61. func (sc *scoremanager) exchangeChip(userId int, productId int) bool {
  62. item := sc.exchange.getItem(productId)
  63. if item == nil {
  64. log.Release("scoremanager.exchangeChip item[%d] not exist", productId)
  65. return false
  66. }
  67. if !sc.reduceScore(userId, item.Score) {
  68. log.Release("scoremanager.exchangeChip userId[%d],score[%d] need [%d]", userId, sc.getScore(userId), item.Score)
  69. return false
  70. }
  71. // 加元宝
  72. chip.GiveChip(userId, item.Chip, common.LOGTYPE_SLOT_SCORE, "slotscore", "slotexchange", "")
  73. notification.AddNotification(userId, notification.Notification_SlotScore, fmt.Sprintf("%d", sc.getScore(userId)))
  74. sc.historyMgr.addHistory(userId, item.Chip)
  75. return true
  76. }
  77. func (sc *scoremanager) dumpSys() {
  78. log.Release("-------------------------------")
  79. log.Release("scoremanager.dumpSys")
  80. defer func() {
  81. log.Release("+++++++++++++++++++++++++++++++")
  82. log.Release("")
  83. }()
  84. log.Release(" %v", sc.getExchangeList())
  85. }
  86. func (sc *scoremanager) dumpUser(userIdStr string) {
  87. log.Release("-------------------------------")
  88. log.Release("scoremanager.dumpUser[%s]", userIdStr)
  89. defer func() {
  90. log.Release("+++++++++++++++++++++++++++++++")
  91. log.Release("")
  92. }()
  93. userId, err := strconv.Atoi(userIdStr)
  94. if err != nil {
  95. log.Release(" atoi error %v", err)
  96. return
  97. }
  98. log.Release(" score:%d", sc.getScore(userId))
  99. log.Release(" history:%s", sc.getExchangeHistory(userId))
  100. }
  101. func (sc *scoremanager) getExchangeHistory(userId int) string {
  102. return sc.historyMgr.getHistory(userId)
  103. }
  104. func (sc *scoremanager) doFlush() {
  105. sc.historyMgr.flush()
  106. sc.flush()
  107. }