incomemgr.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package income
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/micros/audioroom/handler/config"
  5. pb "bet24.com/servers/micros/audioroom/proto"
  6. userservices "bet24.com/servers/micros/userservices/proto"
  7. "strconv"
  8. "sync"
  9. "time"
  10. )
  11. var mgr *incomemgr
  12. // 收益
  13. type incomemgr struct {
  14. user_list map[int]*userIncome
  15. lock *sync.RWMutex
  16. onRoomInfo func(roomId int) *pb.RoomInfo
  17. }
  18. func getIncomeManager() *incomemgr {
  19. if mgr == nil {
  20. mgr = new(incomemgr)
  21. mgr.ctor()
  22. }
  23. return mgr
  24. }
  25. func (this *incomemgr) ctor() {
  26. mgr.user_list = make(map[int]*userIncome)
  27. mgr.lock = &sync.RWMutex{}
  28. mgr.doCheck()
  29. log.Debug("incomemgr.ctor starting ...")
  30. return
  31. }
  32. // 轮询
  33. func (this *incomemgr) doCheck() {
  34. ticker := time.NewTicker(60 * time.Second)
  35. go func(t *time.Ticker) {
  36. for {
  37. select {
  38. case <-t.C:
  39. go this.checkUserExpire()
  40. }
  41. }
  42. }(ticker)
  43. }
  44. // 检查过期用户
  45. func (this *incomemgr) checkUserExpire() {
  46. this.lock.Lock()
  47. defer this.lock.Unlock()
  48. // 遍历所有用户
  49. for uid, v := range this.user_list {
  50. // 判断是否过期
  51. if !v.isExpire() {
  52. continue
  53. }
  54. // 删除
  55. delete(this.user_list, uid)
  56. log.Debug("incomemgr.checkUserExpire userId=%d 已完成清理", uid)
  57. }
  58. return
  59. }
  60. // 获取用户信息
  61. func (this *incomemgr) getUser(userId int) *userIncome {
  62. this.lock.RLock()
  63. u, ok := this.user_list[userId]
  64. this.lock.RUnlock()
  65. if ok {
  66. u.updateTimeStamp()
  67. return u
  68. }
  69. u = newUserIncome(userId)
  70. this.lock.Lock()
  71. this.user_list[userId] = u
  72. this.lock.Unlock()
  73. return u
  74. }
  75. // 获取收益信息
  76. func (this *incomemgr) getIncomeInfo(userId, gameId int) (incomeLevel int, goldIncomeRatio, chipIncomeRatio float64) {
  77. u := this.getUser(userId)
  78. if u == nil {
  79. log.Debug("incomemgr.getIncomeInfo userId=%d not exist", userId)
  80. return
  81. }
  82. return u.getIncomeInfo(gameId)
  83. }
  84. // 触发游戏收益(仅房主)
  85. func (this *incomemgr) triggerGameIncome(userId int, data pb.TriggerData) {
  86. log.Debug("incomemgr.TriggerGameIncome userId=%d data=%+v", userId, data)
  87. u := this.getUser(data.RoomOwner)
  88. if u == nil {
  89. log.Debug("incomemgr.TriggerGameIncome userId=%d not exist", data.RoomOwner)
  90. return
  91. }
  92. u.triggerGameIncome(userId, data.RoomId, data.GameId, data.ItemType, data.Amount)
  93. }
  94. // 触发礼物收益(接收者、房主)
  95. func (this *incomemgr) triggerGiftIncome(userId int, data pb.TriggerData) {
  96. log.Debug("incomemgr.TriggerGiftIncome userId=%d data=%+v", userId, data)
  97. // 接收者收益
  98. if receiver := this.getUser(data.Receiver); receiver != nil {
  99. receiver.triggerGiftIncome(userId, data.RoomId, pb.UserType_Receiver, data.ItemType, data.Amount)
  100. } else {
  101. log.Debug("incomemgr.TriggerGiftIncome userId=%d not exist", data.Receiver)
  102. }
  103. // 房主收益
  104. if roomOwner := this.getUser(data.RoomOwner); roomOwner != nil {
  105. roomOwner.triggerGiftIncome(userId, data.RoomId, pb.UserType_RoomOwner, data.ItemType, data.Amount)
  106. } else {
  107. log.Debug("incomemgr.TriggerGiftIncome userId=%d not exist", data.RoomOwner)
  108. }
  109. return
  110. }
  111. // 获取收益记录
  112. func (this *incomemgr) getIncomeLog(roomId, toUserId, fromUserId, itemType int, beginTime, endTime string, pageIndex, pageSize int) (recordCount int, list []pb.IncomeLog) {
  113. u := this.getUser(toUserId)
  114. if u == nil {
  115. log.Debug("incomemgr.GetLog userId=%d not exist", toUserId)
  116. return
  117. }
  118. recordCount, list = u.getIncomeLog(roomId, fromUserId, itemType, beginTime, endTime, pageIndex, pageSize)
  119. for i := 0; i < len(list); i++ {
  120. if u := userservices.GetUserInfo(list[i].FromUserId); u != nil {
  121. list[i].FromNickName = u.NickName
  122. list[i].FaceUrl = u.FaceUrl
  123. list[i].FaceId = u.FaceId
  124. }
  125. }
  126. return
  127. }
  128. // 获取用户收益统计
  129. func (this *incomemgr) getUserIncomeStat(roomId, toUserId, fromUserId, itemType, sortType int, beginTime, endTime string, pageIndex, pageSize int) (recordCount int,
  130. totalGameProfit, totalGiftProfit float64, list []pb.UserIncomeStat) {
  131. u := this.getUser(toUserId)
  132. if u == nil {
  133. log.Debug("incomemgr.GetUserIncomeStat userId=%d not exist", toUserId)
  134. return
  135. }
  136. recordCount, totalGameProfit, totalGiftProfit, list = u.getUserIncomeStat(roomId, fromUserId, itemType, sortType, beginTime, endTime, pageIndex, pageSize)
  137. for i := 0; i < len(list); i++ {
  138. if u := userservices.GetUserInfo(list[i].FromUserId); u != nil {
  139. list[i].FromNickName = u.NickName
  140. list[i].FaceUrl = u.FaceUrl
  141. list[i].FaceId = u.FaceId
  142. }
  143. }
  144. return
  145. }
  146. // 获取游戏收益统计
  147. func (this *incomemgr) getGameIncomeStat(roomId, toUserId, itemType, sortType int, beginTime, endTime string, pageIndex, pageSize int) (recordCount int, list []pb.GameIncomeStat) {
  148. u := this.getUser(toUserId)
  149. if u == nil {
  150. log.Debug("incomemgr.GetGameIncomeStat userId=%d not exist", toUserId)
  151. return
  152. }
  153. recordCount, list = u.getGameIncomeStat(roomId, itemType, sortType, beginTime, endTime, pageIndex, pageSize)
  154. for i := 0; i < len(list); i++ {
  155. cfg := config.Mgr.GetGameInfo(list[i].GameId)
  156. list[i].ChineseName = cfg.Name
  157. }
  158. return
  159. }
  160. // 设置房间信息订阅
  161. func (this *incomemgr) setRoomInfoSubscriber(onRoomInfo func(roomId int) *pb.RoomInfo) {
  162. this.onRoomInfo = onRoomInfo
  163. }
  164. // 获取房间信息
  165. func (this *incomemgr) getRoomInfo(roomId int) *pb.RoomInfo {
  166. if this.onRoomInfo == nil {
  167. return nil
  168. }
  169. return this.onRoomInfo(roomId)
  170. }
  171. // 打印用户
  172. func (this *incomemgr) dumpUser(param1 string) {
  173. userId := 0
  174. if param1 != "" {
  175. userId, _ = strconv.Atoi(param1)
  176. }
  177. log.Debug(" ^_^ 开始打印用户收益数据,用户(%d)人", len(this.user_list))
  178. this.lock.RLock()
  179. for uid, v := range this.user_list {
  180. if userId > 0 && userId != uid {
  181. continue
  182. }
  183. v.DumpUser()
  184. }
  185. this.lock.RUnlock()
  186. log.Debug("完成用户数据打印 ^_^")
  187. }