user_income.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package income
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/common"
  5. "bet24.com/servers/micros/audioroom/handler/config"
  6. pb "bet24.com/servers/micros/audioroom/proto"
  7. "bet24.com/servers/micros/audioroom/transaction/database"
  8. item "bet24.com/servers/micros/item_inventory/proto"
  9. "sync"
  10. "time"
  11. )
  12. const seconds = 600 // 过期时长(秒)
  13. // 收益信息
  14. type userIncome struct {
  15. userId int // 用户id
  16. incomeLevel int // 收益等级
  17. incomeList []pb.IncomeInfo
  18. timeStamp int // 时间戳(用于清理过期数据)
  19. lock *sync.RWMutex
  20. }
  21. func newUserIncome(userId int) *userIncome {
  22. u := new(userIncome)
  23. u.userId = userId
  24. u.timeStamp = common.GetTimeStamp() + seconds
  25. u.lock = &sync.RWMutex{}
  26. u.loadIncomeLevel()
  27. u.loadIncomeList()
  28. return u
  29. }
  30. // 加载收益等级
  31. func (this *userIncome) loadIncomeLevel() {
  32. incomeLevel := database.GetUserIncomeLevel(this.userId)
  33. this.lock.Lock()
  34. defer this.lock.Unlock()
  35. this.incomeLevel = incomeLevel
  36. }
  37. // 加载数据
  38. func (this *userIncome) loadIncomeList() {
  39. list := database.GetUserIncomeList(this.userId)
  40. this.lock.Lock()
  41. defer this.lock.Unlock()
  42. this.incomeList = list
  43. return
  44. }
  45. // 判断是否过期
  46. func (this *userIncome) isExpire() bool {
  47. if this.timeStamp >= common.GetTimeStamp() {
  48. return false
  49. }
  50. log.Debug("userIncome.IsExpire userId=%d 过期用户清理", this.userId)
  51. return true
  52. }
  53. // 更新时间戳
  54. func (this *userIncome) updateTimeStamp() {
  55. this.timeStamp = common.GetTimeStamp() + seconds
  56. return
  57. }
  58. // 获取收益信息
  59. func (this *userIncome) getIncomeInfo(gameId int) (incomeLevel int, goldIncomeRatio, chipIncomeRatio float64) {
  60. // 收益等级
  61. incomeLevel = this.incomeLevel
  62. // 金币收益比率
  63. goldIncomeRatio = config.Mgr.GetGameIncomeRatio(gameId, item.Item_Gold, this.incomeLevel)
  64. // 钻石收益比率
  65. chipIncomeRatio = config.Mgr.GetGameIncomeRatio(gameId, item.Item_Chip, this.incomeLevel)
  66. return
  67. }
  68. // 触发游戏收益(注意并发)
  69. func (this *userIncome) triggerGameIncome(fromUserId, roomId, gameId, itemType, amount int) {
  70. // 获取收益比率
  71. incomeRatio := config.Mgr.GetGameIncomeRatio(gameId, itemType, this.incomeLevel)
  72. // 增加收益
  73. go this.addProfit(roomId, gameId, pb.UserType_RoomOwner, fromUserId, itemType, amount, pb.IncomeType_Game, incomeRatio)
  74. }
  75. // 触发礼物收益(注意并发)
  76. func (this *userIncome) triggerGiftIncome(fromUserId, roomId, userType, itemType, amount int) {
  77. // 获取收益比率
  78. var incomeRatio float64
  79. receiver, roomOwner := config.Mgr.GetGiftIncomeRatio(this.incomeLevel, itemType)
  80. switch userType {
  81. case pb.UserType_Receiver:
  82. incomeRatio = receiver
  83. case pb.UserType_RoomOwner:
  84. incomeRatio = roomOwner
  85. }
  86. // 增加收益
  87. go this.addProfit(roomId, 0, userType, fromUserId, itemType, amount, pb.IncomeType_Gift, incomeRatio)
  88. }
  89. // 增加收益
  90. func (this *userIncome) addProfit(roomId, gameId, userType, fromUserId, itemType, amount, incomeType int, incomeRatio float64) {
  91. log.Debug("user_income.addProfit userId=%d roomId=%d gameId=%d userType=%d fromUserId=%d itemType=%d amount=%d incomeType=%d incomeRatio=%v",
  92. this.userId, roomId, gameId, userType, fromUserId, itemType, amount, incomeType, incomeRatio)
  93. // 计算收益
  94. profit := incomeRatio * float64(amount)
  95. if profit <= 0 {
  96. log.Release("income.triggerGiftIncome incomeRatio invalid userId=%d roomId=%d incomeLevel=%d fromUserId=%d itemType=%d amount=%d ratio=%v profit=%v",
  97. this.userId, roomId, this.incomeLevel, fromUserId, itemType, amount, incomeRatio, profit)
  98. return
  99. }
  100. var (
  101. itemCount int // 数量
  102. balance float64 // 结余
  103. stillProfit float64 // 剩余收益
  104. totalProfit float64 // 总收益
  105. isUpdate bool // 是否修改
  106. )
  107. this.lock.Lock()
  108. defer this.lock.Unlock()
  109. // 加收益
  110. for i := 0; i < len(this.incomeList); i++ {
  111. if this.incomeList[i].ItemType != itemType {
  112. continue
  113. }
  114. this.incomeList[i].Balance += profit
  115. stillProfit = this.incomeList[i].Balance
  116. this.incomeList[i].TotalProfit += profit
  117. itemCount = int(this.incomeList[i].Balance)
  118. this.incomeList[i].Balance -= float64(itemCount)
  119. balance = this.incomeList[i].Balance
  120. totalProfit = this.incomeList[i].TotalProfit
  121. isUpdate = true
  122. break
  123. }
  124. // 新增数据
  125. if !isUpdate {
  126. stillProfit = profit
  127. itemCount = int(profit)
  128. balance = profit - float64(itemCount)
  129. totalProfit = profit
  130. this.incomeList = append(this.incomeList, pb.IncomeInfo{
  131. ItemType: itemType,
  132. Balance: balance,
  133. TotalProfit: totalProfit,
  134. })
  135. }
  136. go func() {
  137. // 给道具
  138. if itemCount > 0 {
  139. var items []item.ItemPack
  140. items = append(items, item.ItemPack{
  141. ItemId: itemType,
  142. Count: itemCount,
  143. })
  144. item.AddItems(this.userId, items, "income profit", common.LOGTYPE_AUDIOROOM_INCOME)
  145. }
  146. // 保存收益
  147. database.AddUserIncome(this.userId, itemType, balance, totalProfit)
  148. // 添加收益记录
  149. database.AddUserIncomeLog(this.userId, roomId, gameId, fromUserId, itemType, incomeType,
  150. userType, amount, itemCount, profit, stillProfit)
  151. }()
  152. }
  153. // 获取收益记录
  154. func (this *userIncome) getIncomeLog(roomId, fromUserId, itemType int, beginTime, endTime string, pageIndex, pageSize int) (recordCount int,
  155. list []pb.IncomeLog) {
  156. recordCount, list = database.GetIncomeLog(this.userId, roomId, fromUserId, itemType, beginTime, endTime, pageIndex, pageSize)
  157. return
  158. }
  159. // 获取用户收益统计
  160. func (this *userIncome) getUserIncomeStat(roomId, fromUserId, itemType, sortType int, beginTime, endTime string, pageIndex, pageSize int) (recordCount int,
  161. totalGameProfit, totalGiftProfit float64, list []pb.UserIncomeStat) {
  162. recordCount, totalGameProfit, totalGiftProfit, list = database.GetUserIncomeStat(this.userId, roomId, fromUserId, itemType, sortType, beginTime, endTime,
  163. pageIndex, pageSize)
  164. return
  165. }
  166. // 获取游戏收益统计
  167. func (this *userIncome) getGameIncomeStat(roomId, itemType, sortType int, beginTime, endTime string, pageIndex, pageSize int) (recordCount int, list []pb.GameIncomeStat) {
  168. recordCount, list = database.GetGameIncomeStat(this.userId, roomId, itemType, sortType, beginTime, endTime, pageIndex, pageSize)
  169. return
  170. }
  171. // 打印用户
  172. func (this *userIncome) DumpUser() {
  173. log.Debug("用户[%d]信息,收益等级(%d),过期时间[%s]:", this.userId, this.incomeLevel, time.Unix(int64(this.timeStamp), 0).Format(common.Layout))
  174. log.Debug("收益列表打印(%d)个:", len(this.incomeList))
  175. for _, info := range this.incomeList {
  176. log.Debug(" %+v", info)
  177. }
  178. log.Debug("++++++++++++++++++++++++++++++++++++++++++++++")
  179. }