slotuser.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package usermanager
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/utils"
  5. "sync"
  6. )
  7. const (
  8. SlotReturnLevel_Low = iota
  9. SlotReturnLevel_High
  10. SlotReturnLevel_Newbie
  11. SlotReturnLevel_Free
  12. SlotReturnLevel_WhiteList
  13. )
  14. // 用户不同游戏的统计数据
  15. type usergame struct {
  16. gameId int
  17. betAmount int // 总下注额
  18. winAmount int // 总返还
  19. betCount int // 总局数
  20. freeCount int
  21. currentLevel int
  22. totalBetCount int
  23. noneFreeContinueCount int // 多少局没出现free了
  24. }
  25. func newUserGame(userId, gameId int) *usergame {
  26. ug := new(usergame)
  27. // 读取总局数
  28. ug.gameId = gameId
  29. ug.totalBetCount = mgr.userTotalCountMgr.getTotalCount(userId, gameId)
  30. return ug
  31. }
  32. func (ug *usergame) isNewbie(betAmount int) bool {
  33. return mgr.isNewbie(betAmount, ug.totalBetCount)
  34. }
  35. func (ug *usergame) isFree() bool {
  36. maxNoneFree := mgr.getMaxNoneFreeCount(ug.gameId)
  37. if maxNoneFree == 0 {
  38. return false
  39. }
  40. return ug.noneFreeContinueCount >= maxNoneFree
  41. }
  42. func (ug *usergame) getReturnLevel(betAmount int) int {
  43. if ug.isFree() {
  44. return SlotReturnLevel_Free
  45. }
  46. if ug.isNewbie(betAmount) {
  47. return SlotReturnLevel_Newbie
  48. }
  49. if ug.betCount < 10 || ug.betAmount == 0 {
  50. return ug.currentLevel
  51. }
  52. if ug.betCount%10 != 0 {
  53. return ug.currentLevel
  54. }
  55. // 每10局做个变换
  56. rate := ug.winAmount * 100 / ug.betAmount
  57. if rate >= 95 {
  58. ug.currentLevel = SlotReturnLevel_Low
  59. } else if rate < 80 {
  60. ug.currentLevel = SlotReturnLevel_High
  61. }
  62. return ug.currentLevel
  63. }
  64. type slotuser struct {
  65. userId int
  66. games map[int]*usergame
  67. lock *sync.RWMutex
  68. }
  69. func newSlotUser(userId int) *slotuser {
  70. if userId == 0 {
  71. return nil
  72. }
  73. ret := new(slotuser)
  74. ret.userId = userId
  75. ret.lock = &sync.RWMutex{}
  76. ret.games = make(map[int]*usergame)
  77. // 把已有局数加载进来
  78. log.Debug("newSlotUser %d loading games", userId)
  79. ret.lock.Lock()
  80. for _, v := range slot_ids {
  81. ret.games[v] = newUserGame(userId, v)
  82. }
  83. ret.lock.Unlock()
  84. return ret
  85. }
  86. func (u *slotuser) addResult(gameId int, betAmount int, winAmount int, isFree bool) {
  87. u.lock.Lock()
  88. ug, ok := u.games[gameId]
  89. if !ok {
  90. ug = newUserGame(u.userId, gameId)
  91. u.games[gameId] = ug
  92. }
  93. if !isFree {
  94. ug.betAmount += betAmount
  95. } else {
  96. ug.freeCount++
  97. }
  98. ug.winAmount += winAmount
  99. ug.betCount++
  100. ug.totalBetCount++
  101. if isFree {
  102. ug.noneFreeContinueCount = 0
  103. } else {
  104. ug.noneFreeContinueCount++
  105. }
  106. u.lock.Unlock()
  107. mgr.userTotalCountMgr.addTotalCount(u.userId, gameId)
  108. }
  109. func (u *slotuser) getReturnLevel(gameId int, betAmount int) int {
  110. u.lock.RLock()
  111. ug, ok := u.games[gameId]
  112. u.lock.RUnlock()
  113. if !ok {
  114. return SlotReturnLevel_Low
  115. }
  116. // 这里增加判断条件
  117. return ug.getReturnLevel(betAmount)
  118. }
  119. func (u *slotuser) dump() {
  120. log.Release(" slotuser.dump %d", u.userId)
  121. u.lock.RLock()
  122. for k, v := range u.games {
  123. log.Release(" gameId[%d] level[%d]", k, v.getReturnLevel(100))
  124. log.Release(" total[%d],bet[%s],win[%s],betCount[%d],freeCount[%d]", v.totalBetCount, utils.FormatScore(v.betAmount),
  125. utils.FormatScore(v.winAmount), v.betCount, v.freeCount)
  126. }
  127. u.lock.RUnlock()
  128. log.Release(" ---------------------")
  129. }
  130. func (u *slotuser) isNewbie(gameId int) bool {
  131. u.lock.RLock()
  132. ug, ok := u.games[gameId]
  133. u.lock.RUnlock()
  134. if !ok {
  135. return true
  136. }
  137. return ug.isNewbie(0)
  138. }