| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- package savingpot
- import (
- "context"
- "math/rand"
- "time"
- "bet24.com/log"
- "bet24.com/servers/common"
- pb "bet24.com/servers/micros/activityservice/proto"
- )
- const muitiply_start_hours = 48 // 三倍开始时间
- const default_muitiply_second = 12 * 60 * 60 // 默认三倍持续时间
- // 用户信息
- type userSavingPot struct {
- UserId int // 用户id
- timeStamp int // 当前时间戳
- startMultiplyTime int // 开始触发多倍领取时间
- isStartMultiply bool // 开始触发多倍领取
- accumulateLimit int // 存钱罐上限
- buyCount int // 购买次数
- buyAmount int // 老存钱罐的金额
- pb.SavingPotInfo // 存钱罐信息
- isOld bool
- }
- func newUserSavingPot(userId int) *userSavingPot {
- ret := new(userSavingPot)
- ret.UserId = userId
- ret.isStartMultiply = false
- ret.buyCount = 0
- ret.isOld = false
- ret.loadData()
- ret.updateTimeStamp()
- go ret.checkTime()
- return ret
- }
- func (this *userSavingPot) checkTime() {
- for {
- c, cancel := context.WithTimeout(context.Background(), time.Minute)
- select {
- case <-c.Done():
- dayIndex := common.GetDayIndex(this.timeStamp)
- if common.GetNowDayIndex() > dayIndex {
- //if common.GetTimeStamp() > this.timeStamp {
- this.updateTimeStamp()
- this.checkDecreseAmount()
- }
- cancel()
- }
- }
- }
- // 凌晨0点左右玩家存钱罐减少10%,同时刷新可买数据
- func (this *userSavingPot) checkDecreseAmount() {
- this.buyCount = 0
- if this.BuyAmount <= 0 {
- return
- }
- this.BuyAmount = this.BuyAmount * 9 / 10
- bAchieveGoal := false
- if this.CurrentLevel <= 1 {
- goalValue := this.accumulateLimit * 3 / 4
- if this.BuyAmount < goalValue {
- bAchieveGoal = true
- }
- }
- if bAchieveGoal {
- this.isStartMultiply = false
- this.IsMultiplyStatus = 0
- go trans_UpdateInfo(this.UserId, this.SavingPotInfo)
- }
- }
- func (this *userSavingPot) loadData() {
- info := trans_GetInfo(this.UserId)
- if info.CurrentLevel == 0 {
- info.CurrentLevel = 1
- }
- this.SavingPotInfo = info
- oldInfo := trans_GetOldInfo(this.UserId)
- this.buyAmount = oldInfo.BuyAmount
- }
- // 更新时间戳
- func (this *userSavingPot) updateTimeStamp() {
- this.timeStamp = common.GetTimeStamp()
- }
- // 是否过期
- func (this *userSavingPot) isExpire() bool {
- return this.timeStamp < common.GetTimeStamp()
- }
- // 获取信息
- func (this *userSavingPot) getInfo() pb.SavingPotInfo {
- return this.SavingPotInfo
- }
- // 购买
- func (this *userSavingPot) buy(maxLevel, maxCount int, confs []pb.SavingPotCfg, isOld bool) bool {
- if this.buyCount >= maxCount {
- return false
- }
- if isOld {
- this.buyCount++
- this.buyAmount = 0
- go trans_UpdateOldInfo(this.UserId, this.buyCount, this.buyAmount, this.timeStamp)
- return true
- }
- // 扣减购买
- this.BuyAmount = 0
- this.buyCount++
- // 等级上升
- if this.CurrentLevel < maxLevel {
- this.CurrentLevel++
- this.accumulateLimit = confs[this.CurrentLevel-1].AccumulateLimit
- }
- this.checkMultiply(true)
- // 存入数据库
- go trans_UpdateInfo(this.UserId, this.SavingPotInfo)
- return true
- }
- // 添加累计金币
- func (this *userSavingPot) addAccumulateAmount(gameId, winAmount int, cfg pb.SavingPotCfg) {
- log.Debug("savingpot.user.addAccumulateAmount userId=%d gameId=%d winAmount=%d cfg=%+v",
- this.UserId, gameId, winAmount, cfg)
- var isExist bool
- for _, id := range cfg.GameIds {
- if id != gameId {
- continue
- }
- isExist = true
- break
- }
- // 该游戏不计入存钱罐
- if !isExist {
- return
- }
- this.BuyAmount += winAmount
- if this.buyAmount > 150000 {
- this.buyAmount = 150000
- }
- go trans_UpdateOldInfo(this.UserId, this.buyCount, this.buyAmount, this.timeStamp)
- // 超过累计值上限
- if this.BuyAmount > cfg.AccumulateLimit {
- this.BuyAmount = cfg.AccumulateLimit
- }
- log.Debug("savingpot.user.addAccumulateAmount userId=%d BuyAmount=%d", this.UserId, this.BuyAmount)
- this.checkMultiply(false)
- // 存入数据库
- go trans_UpdateInfo(this.UserId, this.SavingPotInfo)
- }
- func (this *userSavingPot) checkMultiply(afterBuy bool) {
- if (this.IsMultiplyStatus != 0 && this.CurrentLevel == 1) || (this.isStartMultiply && this.CurrentLevel > 1) {
- return
- }
- bAchieveGoal := false
- if this.CurrentLevel <= 1 {
- goalValue := this.accumulateLimit * 3 / 4
- if this.BuyAmount >= goalValue {
- bAchieveGoal = true
- }
- } else {
- if this.BuyAmount > 0 {
- bAchieveGoal = true
- } else {
- if afterBuy {
- this.isStartMultiply = false
- this.startMultiplyTime = 0
- this.UpdateTime = 0
- this.IsMultiplyStatus = 0
- this.DuringTimeSec = 0
- }
- }
- }
- if bAchieveGoal {
- this.isStartMultiply = true
- this.startMultiplyTime = common.GetTimeStamp()
- this.UpdateTime = 0
- this.IsMultiplyStatus = 0
- this.DuringTimeSec = 0
- }
- }
- func (this *userSavingPot) checkMultiplyReceive(cfg pb.SavingPotCfg) {
- this.checkMultiply(false)
- if this.IsMultiplyStatus != 0 || !this.isStartMultiply {
- return
- }
- bAchieveGoal := false
- if this.CurrentLevel <= 1 {
- goalValue := this.accumulateLimit * 3 / 4
- if this.BuyAmount >= goalValue {
- bAchieveGoal = true
- }
- } else {
- if this.BuyAmount > 0 {
- sec := common.GetDifferSec(this.startMultiplyTime)
- if sec >= muitiply_start_hours*60*60 {
- bAchieveGoal = true
- }
- }
- }
- if bAchieveGoal {
- this.startMultiplyReceive(cfg)
- }
- go trans_UpdateInfo(this.UserId, this.SavingPotInfo)
- }
- func (this *userSavingPot) startMultiplyReceive(cfg pb.SavingPotCfg) {
- this.isStartMultiply = false
- this.startMultiplyTime = 0
- this.IsMultiplyStatus = 1
- duringTime := cfg.MinMultiplyDurringTime
- if cfg.MinMultiplyDurringTime < cfg.MaxMultiplyDurringTime {
- duringTime = duringTime + rand.Intn(cfg.MaxMultiplyDurringTime-cfg.MinMultiplyDurringTime)
- }
- if duringTime <= 0 {
- duringTime = default_muitiply_second
- }
- this.DuringTimeSec = duringTime
- this.UpdateTime = common.GetTimeStamp()
- time.AfterFunc(time.Duration(duringTime)*time.Second, this.endMultiplyRecive)
- }
- func (this *userSavingPot) endMultiplyRecive() {
- if this.IsMultiplyStatus == 0 {
- return
- }
- this.DuringTimeSec = 0
- this.IsMultiplyStatus = 0
- this.checkMultiply(false)
- go trans_UpdateInfo(this.UserId, this.SavingPotInfo)
- }
- func (this *userSavingPot) dump() {
- log.Release("UserInfo:%v", this.SavingPotInfo)
- }
|