user.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package savingpot
  2. import (
  3. "context"
  4. "math/rand"
  5. "time"
  6. "bet24.com/log"
  7. "bet24.com/servers/common"
  8. pb "bet24.com/servers/micros/activityservice/proto"
  9. )
  10. const muitiply_start_hours = 48 // 三倍开始时间
  11. const default_muitiply_second = 12 * 60 * 60 // 默认三倍持续时间
  12. // 用户信息
  13. type userSavingPot struct {
  14. UserId int // 用户id
  15. timeStamp int // 当前时间戳
  16. startMultiplyTime int // 开始触发多倍领取时间
  17. isStartMultiply bool // 开始触发多倍领取
  18. accumulateLimit int // 存钱罐上限
  19. buyCount int // 购买次数
  20. buyAmount int // 老存钱罐的金额
  21. pb.SavingPotInfo // 存钱罐信息
  22. isOld bool
  23. }
  24. func newUserSavingPot(userId int) *userSavingPot {
  25. ret := new(userSavingPot)
  26. ret.UserId = userId
  27. ret.isStartMultiply = false
  28. ret.buyCount = 0
  29. ret.isOld = false
  30. ret.loadData()
  31. ret.updateTimeStamp()
  32. go ret.checkTime()
  33. return ret
  34. }
  35. func (this *userSavingPot) checkTime() {
  36. for {
  37. c, cancel := context.WithTimeout(context.Background(), time.Minute)
  38. select {
  39. case <-c.Done():
  40. dayIndex := common.GetDayIndex(this.timeStamp)
  41. if common.GetNowDayIndex() > dayIndex {
  42. //if common.GetTimeStamp() > this.timeStamp {
  43. this.updateTimeStamp()
  44. this.checkDecreseAmount()
  45. }
  46. cancel()
  47. }
  48. }
  49. }
  50. // 凌晨0点左右玩家存钱罐减少10%,同时刷新可买数据
  51. func (this *userSavingPot) checkDecreseAmount() {
  52. this.buyCount = 0
  53. if this.BuyAmount <= 0 {
  54. return
  55. }
  56. this.BuyAmount = this.BuyAmount * 9 / 10
  57. bAchieveGoal := false
  58. if this.CurrentLevel <= 1 {
  59. goalValue := this.accumulateLimit * 3 / 4
  60. if this.BuyAmount < goalValue {
  61. bAchieveGoal = true
  62. }
  63. }
  64. if bAchieveGoal {
  65. this.isStartMultiply = false
  66. this.IsMultiplyStatus = 0
  67. go trans_UpdateInfo(this.UserId, this.SavingPotInfo)
  68. }
  69. }
  70. func (this *userSavingPot) loadData() {
  71. info := trans_GetInfo(this.UserId)
  72. if info.CurrentLevel == 0 {
  73. info.CurrentLevel = 1
  74. }
  75. this.SavingPotInfo = info
  76. oldInfo := trans_GetOldInfo(this.UserId)
  77. this.buyAmount = oldInfo.BuyAmount
  78. }
  79. // 更新时间戳
  80. func (this *userSavingPot) updateTimeStamp() {
  81. this.timeStamp = common.GetTimeStamp()
  82. }
  83. // 是否过期
  84. func (this *userSavingPot) isExpire() bool {
  85. return this.timeStamp < common.GetTimeStamp()
  86. }
  87. // 获取信息
  88. func (this *userSavingPot) getInfo() pb.SavingPotInfo {
  89. return this.SavingPotInfo
  90. }
  91. // 购买
  92. func (this *userSavingPot) buy(maxLevel, maxCount int, confs []pb.SavingPotCfg, isOld bool) bool {
  93. if this.buyCount >= maxCount {
  94. return false
  95. }
  96. if isOld {
  97. this.buyCount++
  98. this.buyAmount = 0
  99. go trans_UpdateOldInfo(this.UserId, this.buyCount, this.buyAmount, this.timeStamp)
  100. return true
  101. }
  102. // 扣减购买
  103. this.BuyAmount = 0
  104. this.buyCount++
  105. // 等级上升
  106. if this.CurrentLevel < maxLevel {
  107. this.CurrentLevel++
  108. this.accumulateLimit = confs[this.CurrentLevel-1].AccumulateLimit
  109. }
  110. this.checkMultiply(true)
  111. // 存入数据库
  112. go trans_UpdateInfo(this.UserId, this.SavingPotInfo)
  113. return true
  114. }
  115. // 添加累计金币
  116. func (this *userSavingPot) addAccumulateAmount(gameId, winAmount int, cfg pb.SavingPotCfg) {
  117. log.Debug("savingpot.user.addAccumulateAmount userId=%d gameId=%d winAmount=%d cfg=%+v",
  118. this.UserId, gameId, winAmount, cfg)
  119. var isExist bool
  120. for _, id := range cfg.GameIds {
  121. if id != gameId {
  122. continue
  123. }
  124. isExist = true
  125. break
  126. }
  127. // 该游戏不计入存钱罐
  128. if !isExist {
  129. return
  130. }
  131. this.BuyAmount += winAmount
  132. if this.buyAmount > 150000 {
  133. this.buyAmount = 150000
  134. }
  135. go trans_UpdateOldInfo(this.UserId, this.buyCount, this.buyAmount, this.timeStamp)
  136. // 超过累计值上限
  137. if this.BuyAmount > cfg.AccumulateLimit {
  138. this.BuyAmount = cfg.AccumulateLimit
  139. }
  140. log.Debug("savingpot.user.addAccumulateAmount userId=%d BuyAmount=%d", this.UserId, this.BuyAmount)
  141. this.checkMultiply(false)
  142. // 存入数据库
  143. go trans_UpdateInfo(this.UserId, this.SavingPotInfo)
  144. }
  145. func (this *userSavingPot) checkMultiply(afterBuy bool) {
  146. if (this.IsMultiplyStatus != 0 && this.CurrentLevel == 1) || (this.isStartMultiply && this.CurrentLevel > 1) {
  147. return
  148. }
  149. bAchieveGoal := false
  150. if this.CurrentLevel <= 1 {
  151. goalValue := this.accumulateLimit * 3 / 4
  152. if this.BuyAmount >= goalValue {
  153. bAchieveGoal = true
  154. }
  155. } else {
  156. if this.BuyAmount > 0 {
  157. bAchieveGoal = true
  158. } else {
  159. if afterBuy {
  160. this.isStartMultiply = false
  161. this.startMultiplyTime = 0
  162. this.UpdateTime = 0
  163. this.IsMultiplyStatus = 0
  164. this.DuringTimeSec = 0
  165. }
  166. }
  167. }
  168. if bAchieveGoal {
  169. this.isStartMultiply = true
  170. this.startMultiplyTime = common.GetTimeStamp()
  171. this.UpdateTime = 0
  172. this.IsMultiplyStatus = 0
  173. this.DuringTimeSec = 0
  174. }
  175. }
  176. func (this *userSavingPot) checkMultiplyReceive(cfg pb.SavingPotCfg) {
  177. this.checkMultiply(false)
  178. if this.IsMultiplyStatus != 0 || !this.isStartMultiply {
  179. return
  180. }
  181. bAchieveGoal := false
  182. if this.CurrentLevel <= 1 {
  183. goalValue := this.accumulateLimit * 3 / 4
  184. if this.BuyAmount >= goalValue {
  185. bAchieveGoal = true
  186. }
  187. } else {
  188. if this.BuyAmount > 0 {
  189. sec := common.GetDifferSec(this.startMultiplyTime)
  190. if sec >= muitiply_start_hours*60*60 {
  191. bAchieveGoal = true
  192. }
  193. }
  194. }
  195. if bAchieveGoal {
  196. this.startMultiplyReceive(cfg)
  197. }
  198. go trans_UpdateInfo(this.UserId, this.SavingPotInfo)
  199. }
  200. func (this *userSavingPot) startMultiplyReceive(cfg pb.SavingPotCfg) {
  201. this.isStartMultiply = false
  202. this.startMultiplyTime = 0
  203. this.IsMultiplyStatus = 1
  204. duringTime := cfg.MinMultiplyDurringTime
  205. if cfg.MinMultiplyDurringTime < cfg.MaxMultiplyDurringTime {
  206. duringTime = duringTime + rand.Intn(cfg.MaxMultiplyDurringTime-cfg.MinMultiplyDurringTime)
  207. }
  208. if duringTime <= 0 {
  209. duringTime = default_muitiply_second
  210. }
  211. this.DuringTimeSec = duringTime
  212. this.UpdateTime = common.GetTimeStamp()
  213. time.AfterFunc(time.Duration(duringTime)*time.Second, this.endMultiplyRecive)
  214. }
  215. func (this *userSavingPot) endMultiplyRecive() {
  216. if this.IsMultiplyStatus == 0 {
  217. return
  218. }
  219. this.DuringTimeSec = 0
  220. this.IsMultiplyStatus = 0
  221. this.checkMultiply(false)
  222. go trans_UpdateInfo(this.UserId, this.SavingPotInfo)
  223. }
  224. func (this *userSavingPot) dump() {
  225. log.Release("UserInfo:%v", this.SavingPotInfo)
  226. }