subsidymgr.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package subsidy
  2. import (
  3. "bet24.com/log"
  4. "bet24.com/servers/common"
  5. "bet24.com/servers/coreservice/serviceconfig"
  6. inventory "bet24.com/servers/micros/item_inventory/proto"
  7. item "bet24.com/servers/micros/item_inventory/proto"
  8. cash "bet24.com/servers/micros/money/proto"
  9. chip "bet24.com/servers/micros/money/proto"
  10. notification "bet24.com/servers/micros/notification/proto"
  11. userlabel "bet24.com/servers/micros/userlabel/proto"
  12. vipservice "bet24.com/servers/micros/userservices/proto"
  13. "encoding/json"
  14. )
  15. type subsidymgr struct {
  16. }
  17. func newSubsidyMgr() *subsidymgr {
  18. log.Debug("subsidy manager running")
  19. return &subsidymgr{}
  20. }
  21. // 获取补助信息
  22. func (this *subsidymgr) getInfo(userId, lowerAmount, maxTimes int) *Subsidy {
  23. info := getInfo(userId)
  24. info.LowerAmount = lowerAmount
  25. info.MaxTimes = maxTimes
  26. now := common.GetTimeStamp()
  27. //判断是否是同一天
  28. if !common.IsSameDay(info.Crdate, now) {
  29. info.GiftTimes = 0
  30. info.CoolTime = 0
  31. }
  32. //去数据库取金币
  33. _, amount := cash.GetMoney(userId)
  34. // 获取额外破产补助
  35. bankruptcy := vipservice.GetExtraBankruptcy(userId)
  36. //if bankruptcy > 0 {
  37. info.Amount = lowerAmount * (100 + bankruptcy) / 100
  38. //}
  39. if info.GiftTimes < info.MaxTimes && amount <= info.LowerAmount && info.CoolTime < common.GetTimeStamp() {
  40. info.IsGift = true
  41. // 6=任务类(任务模块)(用户标签)
  42. go userlabel.TriggerEvent(userId, userlabel.Type_Bankruptcy, userlabel.Scope{Num: 1})
  43. }
  44. log.Debug("subsidymgr.getInfo %d :%v", userId, info)
  45. return info
  46. }
  47. // 领取补助信息
  48. func (this *subsidymgr) gift(userId, lowerAmount, maxTimes int, coolSeconds []int) (int, string) {
  49. //获取救济金信息
  50. info := this.getInfo(userId, lowerAmount, maxTimes)
  51. if info == nil {
  52. log.Debug("subsidy.gift userId=%d not exist", userId)
  53. return 0, "领取失败"
  54. }
  55. log.Debug("subsidymgr.gift userId=%d lowerAmount=%d maxTimes=%d coolSeconds=%+v info=%+v",
  56. userId, lowerAmount, maxTimes, coolSeconds, info)
  57. if !info.IsGift {
  58. log.Debug("subsidy.gift userId=%d info=%+v", userId, info)
  59. return 0, "领取失败"
  60. }
  61. var seconds int
  62. for i := 0; i < len(coolSeconds); i++ {
  63. seconds = coolSeconds[i]
  64. if i == info.GiftTimes {
  65. break
  66. }
  67. }
  68. // 冷却时间
  69. info.CoolTime = common.GetTimeStamp() + seconds
  70. //领取次数+1
  71. info.GiftTimes++
  72. info.Crdate = common.GetTimeStamp()
  73. //领取
  74. if ret := gift(userId, info.GiftTimes, info.Crdate, info.CoolTime); ret != 1 {
  75. log.Debug("subsidy.gift 领取救济金失败 userId=%d ret=%d", userId, ret)
  76. return 0, "领取失败"
  77. }
  78. var items []item.ItemPack
  79. items = append(items, item.ItemPack{
  80. ItemId: 1,
  81. Count: info.Amount,
  82. })
  83. //给道具
  84. if success := inventory.AddItems(userId, items, "领取救济金", common.LOGTYPE_SUBSIDY_GIFT); !success {
  85. log.Debug("subsidy.gift 领取救济金失败 userId=%d items=%+v", userId, items)
  86. return 0, "领取失败"
  87. }
  88. return 1, "领取成功"
  89. }
  90. func (this *subsidymgr) giftChip(userId, lowerAmount int, ipAddress string) bool {
  91. // 是否有低保金额
  92. if lowerAmount <= 0 {
  93. return false
  94. }
  95. // 获取元宝数量
  96. if ok, chip := chip.GetUserChip(userId); ok {
  97. // 判断元宝数量
  98. if chip >= lowerAmount {
  99. return false
  100. }
  101. }
  102. // 判断是否可以领取元宝
  103. if ok := giftChip(userId, lowerAmount, ipAddress); !ok {
  104. return false
  105. }
  106. // 给元宝
  107. go func(userId, amount int) {
  108. var items []item.ItemPack
  109. items = append(items, item.ItemPack{
  110. ItemId: item.Item_Chip,
  111. Count: amount,
  112. })
  113. // 给道具
  114. if success := inventory.AddItems(userId, items, "领取救济金", common.LOGTYPE_SUBSIDY_GIFT); !success {
  115. log.Debug("subsidy.giftChip 领取救济金失败 userId=%d items=%+v", userId, items)
  116. }
  117. }(userId, lowerAmount)
  118. return true
  119. }
  120. // 领取回归奖励
  121. func (this *subsidymgr) giftReturnAward(userId int) {
  122. if serviceconfig.Server.ReturnAwards == "" {
  123. return
  124. }
  125. var items []item.ItemPack
  126. if err := json.Unmarshal([]byte(serviceconfig.Server.ReturnAwards), &items); err != nil {
  127. log.Error("subsidymgr.giftReturnAward err %v", err)
  128. return
  129. }
  130. // 给道具
  131. if success := inventory.AddItems(userId, items, "回归奖励", common.LOGTYPE_RETURN); !success {
  132. log.Debug("subsidymgr.giftReturnAward 回归奖励失败 userId=%d items=%+v", userId, items)
  133. return
  134. }
  135. go notification.AddNotification(userId, notification.Notification_Return, serviceconfig.Server.ReturnAwards)
  136. return
  137. }