userlist.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package gamelogic
  2. import (
  3. "sort"
  4. "sync"
  5. "bet24.com/log"
  6. "bet24.com/servers/games/luckyfruit_table/common"
  7. userservices "bet24.com/servers/micros/userservices/proto"
  8. )
  9. type BetArea struct {
  10. Area int
  11. Count int
  12. }
  13. type luckyfruitUser 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. betAreas []BetArea //下注区域 统计经常下注的两个区域
  23. winAmount int //实际赢金 输了不计
  24. cumulativeWinAmount int //累计赢金 输了不计
  25. winAmountOnly int //只计算赢的金额
  26. isBet bool
  27. }
  28. type userlist struct {
  29. users []*luckyfruitUser
  30. lock *sync.RWMutex
  31. }
  32. func newUserList() *userlist {
  33. ret := new(userlist)
  34. ret.lock = &sync.RWMutex{}
  35. return ret
  36. }
  37. func (ul *userlist) addUser( userId int, nickName string,
  38. faceId int, faceUrl string, vipLevel, vipExpire int, decorations []userservices.UserDecoration) {
  39. ul.lock.Lock()
  40. defer ul.lock.Unlock()
  41. for _, v := range ul.users {
  42. if v.userId == userId {
  43. log.Release("userlist.addUser UserId %d already exist", userId)
  44. return
  45. }
  46. }
  47. u := new(luckyfruitUser)
  48. u.userId = userId
  49. u.nickName = nickName
  50. u.faceId = faceId
  51. u.faceUrl = faceUrl
  52. u.vipLevel = vipLevel
  53. u.vipExpire = vipExpire
  54. u.decorations = decorations
  55. ul.users = append(ul.users, u)
  56. }
  57. func (ul *userlist) removeUser(userId int) {
  58. ul.lock.Lock()
  59. defer ul.lock.Unlock()
  60. for k, v := range ul.users {
  61. if v.userId == userId {
  62. ul.users = append(ul.users[:k], ul.users[k+1:]...)
  63. return
  64. }
  65. }
  66. log.Release("userlist.removeUser %d not found", userId)
  67. }
  68. // 结束的时候清理状态,并且根据未下注进行连胜扣减
  69. func (ul *userlist) clearBetStatus() {
  70. ul.lock.Lock()
  71. defer ul.lock.Unlock()
  72. for _, v := range ul.users {
  73. if !v.isBet {
  74. if v.winAmount > 0 {
  75. v.winAmount = 0
  76. }
  77. v.winAmountOnly = 0
  78. } else {
  79. v.isBet = false
  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. updateBetAreas(v, betId)
  114. return
  115. }
  116. }
  117. log.Release("userlist.addBet %d not found", userId)
  118. }
  119. func (ul *userlist) clearBet(userId, amount int) {
  120. ul.lock.Lock()
  121. defer ul.lock.Unlock()
  122. for _, v := range ul.users {
  123. if v.userId == userId {
  124. v.totalBet -= amount
  125. v.isBet = v.totalBet > 0
  126. //撤销下注,不需要更新下注区域
  127. return
  128. }
  129. }
  130. log.Release("userlist.addBet %d not found", userId)
  131. }
  132. func updateBetAreas(u *luckyfruitUser, area int) {
  133. for i := range u.betAreas {
  134. if u.betAreas[i].Area == area {
  135. u.betAreas[i].Count++
  136. return
  137. }
  138. }
  139. //如果没有找到,添加
  140. u.betAreas = append(u.betAreas, BetArea{Area: area, Count: 1})
  141. }
  142. // 幸运星
  143. // func (ul *userlist) getLuckyStarUsers(count int) []common.ScoreUser {
  144. // ul.lock.Lock()
  145. // defer ul.lock.Unlock()
  146. // sort.Slice(ul.users, func(i, j int) bool {
  147. // return ul.users[i].cumulativeWinAmount > ul.users[j].cumulativeWinAmount
  148. // })
  149. // ret := []common.ScoreUser{}
  150. // for k, v := range ul.users {
  151. // if k >= count {
  152. // break
  153. // }
  154. // if v.cumulativeWinAmount <= 0 {
  155. // break
  156. // }
  157. // u := common.ScoreUser{UserId: v.userId, Score: v.cumulativeWinAmount}
  158. // ret = append(ret, u)
  159. // }
  160. // return ret
  161. // }
  162. // 每回合赢金排行
  163. func (ul *userlist) getWinGoldRankingUsers(count int) []common.ScoreUser {
  164. ul.lock.Lock()
  165. defer ul.lock.Unlock()
  166. //排序
  167. sort.Slice(ul.users, func(i, j int) bool {
  168. return ul.users[i].winAmountOnly > ul.users[j].winAmountOnly
  169. })
  170. ret := []common.ScoreUser{}
  171. for k, v := range ul.users {
  172. if k >= count {
  173. break
  174. }
  175. if v.winAmountOnly <= 0 {
  176. break
  177. }
  178. info := make([]int, 0, 1)
  179. info = append(info, v.winAmountOnly)
  180. u := common.ScoreUser{UserId: v.userId, NickName: v.nickName,
  181. FaceId: v.faceId,
  182. FaceUrl: v.faceUrl,
  183. VipLevel: v.vipLevel,
  184. VipExpire: v.vipExpire,
  185. Decorations: v.decorations,
  186. Info: info}
  187. ret = append(ret, u)
  188. }
  189. return ret
  190. }
  191. // 传递0表示不限制数量
  192. func (ul *userlist) getBetGoldRankingUsers(count int) []common.ScoreUser {
  193. ul.lock.Lock()
  194. defer ul.lock.Unlock()
  195. //排序
  196. sort.Slice(ul.users, func(i, j int) bool {
  197. return ul.users[i].totalBet > ul.users[j].totalBet
  198. })
  199. ret := []common.ScoreUser{}
  200. for k, v := range ul.users {
  201. if k >= count && count != 0 {
  202. break
  203. }
  204. if v.totalBet < 0 {
  205. break
  206. }
  207. //添加热衷下注区域
  208. betArea := getTopTwoBetAreas(v.betAreas)
  209. u := common.ScoreUser{UserId: v.userId, NickName: v.nickName,
  210. FaceId: v.faceId,
  211. FaceUrl: v.faceUrl,
  212. VipLevel: v.vipLevel,
  213. VipExpire: v.vipExpire,
  214. Decorations: v.decorations,
  215. BetAmount: v.totalBet,
  216. Info: betArea}
  217. ret = append(ret, u)
  218. }
  219. return ret
  220. }
  221. func getTopTwoBetAreas(betAreas []BetArea) []int {
  222. // 对下注区域按照Count值从大到小排序 如果数量相同,按照Area值从小到大排序
  223. sort.Slice(betAreas, func(i, j int) bool {
  224. if betAreas[i].Count == betAreas[j].Count {
  225. return betAreas[i].Area < betAreas[j].Area
  226. }
  227. return betAreas[i].Count > betAreas[j].Count
  228. })
  229. // 取前两个下注区域
  230. topTwoBetAreas := make([]int, 0, 2)
  231. for i := 0; i < len(betAreas) && len(topTwoBetAreas) < 2; i++ {
  232. topTwoBetAreas = append(topTwoBetAreas, betAreas[i].Area)
  233. }
  234. // log.Release("下注区域betAreas:%v ,topTwoBetAreas:%v", betAreas, topTwoBetAreas)
  235. return topTwoBetAreas
  236. }