user_room_giving.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package user
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/micros/audioroom/handler/config"
  5. "bet24.com/servers/micros/audioroom/handler/income"
  6. pb "bet24.com/servers/micros/audioroom/proto"
  7. "bet24.com/servers/micros/audioroom/transaction/database"
  8. badge "bet24.com/servers/micros/badge/proto"
  9. giftservice "bet24.com/servers/micros/giftservice/proto"
  10. task "bet24.com/servers/micros/task/proto"
  11. "fmt"
  12. )
  13. // 发送礼物
  14. func (this *UserRoom) SendGiving(roomId, roomOwner, toUserId, giftId, num int) (retCode int, message string) {
  15. log.Debug("user_giving.sendGiving userId=%d roomId=%d toUserId=%d giftId=%d num=%d", this.userId, roomId, toUserId, giftId, num)
  16. var (
  17. totalDiamond int // 总钻石数
  18. totalGold int // 总金币数
  19. sendDiamond int // 每人获得钻石数
  20. sendGold int // 每人获得金币数
  21. toUserIds []int // 接收礼物的id群
  22. )
  23. // 所有在线用户
  24. if toUserId == 1 {
  25. for _, v := range this.roomMgr.GetOnlineUsers(roomId) {
  26. if v.UserId == this.userId {
  27. continue
  28. }
  29. toUserIds = append(toUserIds, v.UserId)
  30. }
  31. } else if this.userId != toUserId {
  32. toUserIds = append(toUserIds, toUserId)
  33. }
  34. // 数量异常
  35. if len(toUserIds) <= 0 {
  36. message = fmt.Sprintf("user_giving.sendGiving userId=%d roomId=%d toUserId=%d giftId=%d num=%d is irregular",
  37. this.userId, roomId, toUserId, giftId, num)
  38. log.Debug(message)
  39. return
  40. }
  41. // 发送礼物
  42. retCode, message, result := giftservice.SendGiftBulk(this.userId, toUserIds, giftId, num)
  43. //log.Debug("user_giving.sendGiving userId=%d roomId=%d toUserId=%d giftId=%d num=%d retCode=%v message=%s result=%+v toUserIds=%+v",
  44. // this.userId, roomId, toUserId, giftId, num, retCode, message, result, toUserIds)
  45. // 判断是否成功
  46. if retCode != 1 {
  47. message = fmt.Sprintf("user_giving.sendGiving userId=%d roomId=%d toUserId=%d giftId=%d num=%d retCode=%d message=%s result=%+v",
  48. this.userId, roomId, toUserId, giftId, num, retCode, message, result)
  49. log.Debug(message)
  50. return
  51. }
  52. cfg := config.Mgr.GetRoomConfig()
  53. switch result.PayType {
  54. case giftservice.PayType_Diamond: // 赠送钻石
  55. sendDiamond = int(result.Price)
  56. totalDiamond = int(result.Price) * len(toUserIds)
  57. case giftservice.PayType_Gold: // 赠送金币
  58. sendGold = int(result.Price)
  59. totalGold = int(result.Price) * len(toUserIds)
  60. }
  61. log.Debug("user_giving.sendGiving userId=%d roomId=%d toUserId=%d giftId=%d num=%d retCode=%v message=%s result=%+v toUserIds=%+v totalDiamond=%d sendDiamond=%d",
  62. this.userId, roomId, toUserId, giftId, num, retCode, message, result, toUserIds, totalDiamond, sendDiamond)
  63. // 给所有在线用户赠送礼物
  64. for _, uid := range toUserIds {
  65. if uid == this.userId {
  66. continue
  67. }
  68. // 触发收益
  69. for _, v := range result.Items {
  70. income.TriggerGiftIncome(this.userId, pb.TriggerData{
  71. RoomId: roomId,
  72. Receiver: uid,
  73. RoomOwner: roomOwner,
  74. ItemType: v.ItemId,
  75. Amount: v.Count,
  76. })
  77. }
  78. if sendDiamond > 0 {
  79. go func(uid int) {
  80. // 触发用户任务
  81. this.roomMgr.DoUserTaskAction(uid, roomId, pb.UserTask_Action_AcceptGiving_Diamond, sendDiamond, 0)
  82. // 徽章进度
  83. badge.DoAction(uid, badge.Action_AudioRoom_ReceiveGivingDiamond, sendDiamond, badge.Scope{})
  84. badge.DoAction(uid, badge.Action_AudioRoom_ReceiveGivingNum, num, badge.Scope{GiftId: giftId})
  85. // 触发大厅任务
  86. task.DoTaskAction(uid, task.TaskAction_receiveGivingDiamond, sendDiamond, task.TaskScope{})
  87. }(uid)
  88. }
  89. // 添加礼物记录
  90. go database.AddGiftHistory(pb.GiftHistory{
  91. RoomId: roomId,
  92. Sender: this.userId,
  93. Receiver: uid,
  94. GiftId: giftId,
  95. GiftNum: num,
  96. DiamondAmount: sendDiamond,
  97. GoldAmount: sendGold,
  98. })
  99. }
  100. // 达到大礼物条件
  101. if totalDiamond >= cfg.GivingDiamondMax || result.AnimationType == giftservice.AnimationType_Global {
  102. // 发送礼物通知
  103. go this.givingNotify(roomId, toUserId, result.GiftId, num, totalDiamond >= cfg.GivingDiamondMax)
  104. }
  105. // 钻石
  106. if totalDiamond > 0 {
  107. go func() {
  108. // 触发房间任务
  109. this.roomMgr.DoRoomTaskAction(this.userId, roomId, pb.RoomTask_Action_Giving_Diamond, totalDiamond)
  110. // 触发用户任务
  111. this.roomMgr.DoUserTaskAction(this.userId, roomId, pb.UserTask_Action_SendGiving_Diamond, totalDiamond, 0)
  112. // 收集点数
  113. this.roomMgr.AddCollect(this.userId, roomId, totalDiamond)
  114. // 徽章进度
  115. badge.DoAction(this.userId, badge.Action_AudioRoom_SendGivingDiamond, totalDiamond, badge.Scope{})
  116. badge.DoAction(this.userId, badge.Action_AudioRoom_SendGivingNum, num, badge.Scope{GiftId: giftId})
  117. // 触发大厅任务
  118. task.DoTaskAction(this.userId, task.TaskAction_diamondGift, 1, task.TaskScope{})
  119. // 触发大厅任务
  120. task.DoTaskAction(this.userId, task.TaskAction_sendGivingDiamond, totalDiamond, task.TaskScope{})
  121. }()
  122. }
  123. // 金币
  124. if totalGold > 0 {
  125. go func() {
  126. // 触发任务
  127. this.roomMgr.DoRoomTaskAction(this.userId, roomId, pb.RoomTask_Action_Giving_Gold, totalGold)
  128. // 徽章进度
  129. badge.DoAction(this.userId, badge.Action_PropConsumeGold, totalGold, badge.Scope{})
  130. }()
  131. }
  132. retCode, message = 1, "success"
  133. return
  134. }