userlist.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package gamelogic
  2. import (
  3. "sort"
  4. "sync"
  5. "bet24.com/log"
  6. "bet24.com/servers/games/greedy/common"
  7. userservices "bet24.com/servers/micros/userservices/proto"
  8. )
  9. type BetArea struct {
  10. Area int
  11. Count int
  12. }
  13. type greedyUser struct {
  14. userId int
  15. nickName string
  16. faceId int
  17. faceUrl string
  18. vipLevel int
  19. vipExpire int
  20. decorations []userservices.UserDecoration
  21. totalBet int
  22. winAmount int //实际赢金 输了不计
  23. cumulativeWinAmount int //累计赢金 输了不计
  24. winAmountOnly int //只计算赢的金额
  25. isBet bool
  26. }
  27. type userlist struct {
  28. users []*greedyUser
  29. lock *sync.RWMutex
  30. }
  31. func newUserList() *userlist {
  32. ret := new(userlist)
  33. ret.lock = &sync.RWMutex{}
  34. return ret
  35. }
  36. func (ul *userlist) addUser(userId int, nickName string,
  37. faceId int, faceUrl string, vipLevel, vipExpire int, decorations []userservices.UserDecoration) {
  38. ul.lock.Lock()
  39. defer ul.lock.Unlock()
  40. for _, v := range ul.users {
  41. if v.userId == userId {
  42. log.Release("userlist.addUser %d already exist", userId)
  43. return
  44. }
  45. }
  46. u := new(greedyUser)
  47. u.userId = userId
  48. u.nickName = nickName
  49. u.faceId = faceId
  50. u.faceUrl = faceUrl
  51. u.vipLevel = vipLevel
  52. u.vipExpire = vipExpire
  53. u.decorations = decorations
  54. ul.users = append(ul.users, u)
  55. }
  56. func (ul *userlist) removeUser(userId int) {
  57. ul.lock.Lock()
  58. defer ul.lock.Unlock()
  59. for k, v := range ul.users {
  60. if v.userId == userId {
  61. ul.users = append(ul.users[:k], ul.users[k+1:]...)
  62. return
  63. }
  64. }
  65. log.Release("userlist.removeUser %d not found", userId)
  66. }
  67. // 结束的时候清理状态,并且根据未下注进行连胜扣减
  68. func (ul *userlist) clearBetStatus() {
  69. ul.lock.Lock()
  70. defer ul.lock.Unlock()
  71. for _, v := range ul.users {
  72. if !v.isBet {
  73. if v.winAmount > 0 {
  74. v.winAmount = 0
  75. }
  76. v.winAmountOnly = 0
  77. } else {
  78. v.isBet = false
  79. v.winAmountOnly = 0
  80. }
  81. }
  82. }
  83. func (ul *userlist) addResult(userId, winAmount, winAmountOnly int) {
  84. log.Debug("userlist.addResult %d,%d", userId, winAmount)
  85. ul.lock.Lock()
  86. defer ul.lock.Unlock()
  87. for _, v := range ul.users {
  88. if v.userId == userId {
  89. if winAmount < 0 {
  90. v.winAmount = 0
  91. }
  92. if winAmountOnly > 0 {
  93. v.winAmountOnly = winAmountOnly
  94. } else {
  95. v.winAmountOnly = 0
  96. }
  97. if winAmount > 0 {
  98. v.winAmount = winAmount
  99. v.cumulativeWinAmount = v.cumulativeWinAmount + winAmount
  100. }
  101. return
  102. }
  103. }
  104. log.Release("userlist.addResult %d not found", userId)
  105. }
  106. func (ul *userlist) addBet(userId, amount, betId int) {
  107. ul.lock.Lock()
  108. defer ul.lock.Unlock()
  109. for _, v := range ul.users {
  110. if v.userId == userId {
  111. v.totalBet += amount
  112. v.isBet = v.totalBet > 0
  113. return
  114. }
  115. }
  116. log.Release("userlist.addBet %d not found", userId)
  117. }
  118. func (ul *userlist) clearBet(userId, amount int) {
  119. ul.lock.Lock()
  120. defer ul.lock.Unlock()
  121. for _, v := range ul.users {
  122. if v.userId == userId {
  123. v.totalBet -= amount
  124. v.isBet = v.totalBet > 0
  125. //撤销下注,不需要更新下注区域
  126. return
  127. }
  128. }
  129. log.Release("userlist.addBet %d not found", userId)
  130. }
  131. // 每回合赢金排行
  132. func (ul *userlist) getWinGoldRankingUsers(count int) []common.ScoreUser {
  133. ul.lock.Lock()
  134. defer ul.lock.Unlock()
  135. //排序
  136. sort.Slice(ul.users, func(i, j int) bool {
  137. return ul.users[i].winAmountOnly > ul.users[j].winAmountOnly
  138. })
  139. ret := []common.ScoreUser{}
  140. for k, v := range ul.users {
  141. if k >= count {
  142. break
  143. }
  144. if v.winAmountOnly <= 0 {
  145. break
  146. }
  147. info := make([]int, 0, 1)
  148. info = append(info, v.winAmountOnly)
  149. u := common.ScoreUser{UserId: v.userId, NickName: v.nickName,
  150. FaceId: v.faceId,
  151. FaceUrl: v.faceUrl,
  152. VipLevel: v.vipLevel,
  153. VipExpire: v.vipExpire,
  154. Decorations: v.decorations,
  155. Info: info}
  156. ret = append(ret, u)
  157. }
  158. return ret
  159. }