| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- package savingpot
- import (
- "encoding/json"
- "fmt"
- "os"
- "strconv"
- "sync"
- "time"
- "bet24.com/log"
- pb "bet24.com/servers/micros/activityservice/proto"
- platformconfig "bet24.com/servers/micros/platformconfig/proto"
- )
- const config_key = "activity_savingpot"
- func getManager() *savingPotMgr {
- if mgr == nil {
- mgr = new(savingPotMgr)
- mgr.ctor()
- }
- return mgr
- }
- var mgr *savingPotMgr
- type SavingPotCfgs struct {
- MaxLevel int
- MaxCount int
- Confs []pb.SavingPotCfg
- }
- type savingPotMgr struct {
- configString string
- userList map[int]*userSavingPot
- lock *sync.RWMutex
- SavingPotCfgs
- }
- func (this *savingPotMgr) ctor() {
- this.lock = &sync.RWMutex{}
- this.userList = make(map[int]*userSavingPot)
- this.readConf()
- //this.checkExpiredUsers()
- }
- func (this *savingPotMgr) readConf() {
- time.AfterFunc(time.Second*600, this.readConf)
- configString := platformconfig.GetConfig(config_key)
- //if configString == "" {
- data, err := os.ReadFile("serviceconf/activity_savingPot.json")
- if err != nil {
- log.Release("savingPot.readConf read config failed")
- return
- }
- configString = string(data)
- if configString != "" {
- platformconfig.SetConfig(config_key, configString)
- }
- //}
- if configString == this.configString {
- return
- }
- this.configString = configString
- // 读取配置
- err = json.Unmarshal([]byte(configString), &this.SavingPotCfgs)
- if err != nil {
- log.Release("savingPot.readConf Unmarshal failed %s", configString)
- }
- }
- func (this *savingPotMgr) checkExpiredUsers() {
- time.AfterFunc(time.Second*600, this.checkExpiredUsers)
- var toRemove []int
- this.lock.RLock()
- for k, v := range this.userList {
- if v.isExpire() {
- toRemove = append(toRemove, k)
- }
- }
- this.lock.RUnlock()
- if len(toRemove) == 0 {
- return
- }
- log.Debug("savingPot.checkExpiredUsers removing %v", toRemove)
- this.lock.Lock()
- for _, v := range toRemove {
- delete(this.userList, v)
- }
- this.lock.Unlock()
- }
- // 获取配置信息
- func (this *savingPotMgr) getConfigInfo(level int) pb.SavingPotCfg {
- if level > this.SavingPotCfgs.MaxLevel {
- log.Debug("error, MaxLevel:%d, level:%d", this.SavingPotCfgs.MaxLevel, level)
- fmt.Printf("SavingPotCfgs====", this.SavingPotCfgs.MaxLevel, level)
- return this.SavingPotCfgs.Confs[this.SavingPotCfgs.MaxLevel-1]
- }
- for i := 0; i < len(this.SavingPotCfgs.Confs); i++ {
- if this.SavingPotCfgs.Confs[i].Level == level {
- return this.SavingPotCfgs.Confs[i]
- }
- }
- return this.SavingPotCfgs.Confs[0]
- }
- // 获取用户
- func (this *savingPotMgr) getUser(userId int) *userSavingPot {
- this.lock.RLock()
- u, ok := this.userList[userId]
- this.lock.RUnlock()
- if ok {
- u.updateTimeStamp()
- return u
- }
- u = newUserSavingPot(userId)
- u.accumulateLimit = this.getConfigInfo(u.CurrentLevel).AccumulateLimit
- this.lock.Lock()
- this.userList[userId] = u
- this.lock.Unlock()
- return u
- }
- // 获取用户信息
- func (this *savingPotMgr) getUserSavingPot(userId int) pb.SavingPotInfo {
- u := this.getUser(userId)
- if u == nil {
- log.Error("savingPot.getUserSavingPot userId[%d] is not exist", userId)
- return pb.SavingPotInfo{}
- }
- return u.getInfo()
- }
- func (this *savingPotMgr) getUserBuyCount(userId int) int {
- u := this.getUser(userId)
- if u == nil {
- log.Error("savingPot.getUserSavingPot userId[%d] is not exist", userId)
- return this.MaxCount
- }
- return u.buyCount
- }
- // 获取存钱罐金币
- func (this *savingPotMgr) getBuyAmount(userId int, isOld bool) int {
- u := this.getUser(userId)
- if u == nil {
- log.Error("savingPot.getBuyAmount userId[%d] is not exist", userId)
- return 0
- }
- if isOld {
- return u.buyAmount
- }
- // 用户信息
- info := u.getInfo()
- amount := info.BuyAmount
- if info.IsMultiplyStatus != 0 {
- amount = info.BuyAmount * 3
- }
- cfg := this.getConfigInfo(info.CurrentLevel)
- if amount > cfg.MultiplyLimit {
- amount = cfg.MultiplyLimit
- }
- return amount
- }
- // 存钱罐购买
- func (this *savingPotMgr) buy(userId int, isOld bool) bool {
- u := this.getUser(userId)
- if u == nil {
- log.Error("savingPot.buy userId[%d] is not exist", userId)
- return false
- }
- return u.buy(this.MaxLevel, this.MaxCount, this.Confs, isOld)
- }
- // 添加累计金币
- func (this *savingPotMgr) addAccumulateAmount(userId, gameId, winAmount int) {
- u := this.getUser(userId)
- if u == nil {
- log.Error("savingPot.addAccumulateAmount userId[%d] is not exist", userId)
- return
- }
- u.addAccumulateAmount(gameId, winAmount, this.getConfigInfo(u.CurrentLevel))
- return
- }
- func (this *savingPotMgr) checkUserMultiplyReceive(userId int) {
- u := this.getUser(userId)
- if u == nil {
- log.Error("savingPot.addAccumulateAmount userId[%d] is not exist", userId)
- return
- }
- cfg := this.getConfigInfo(u.CurrentLevel)
- u.checkMultiplyReceive(cfg)
- }
- func (this *savingPotMgr) setIsOldSavingPot(userId int) {
- u := this.getUser(userId)
- if u != nil {
- u.isOld = true
- }
- }
- func (this *savingPotMgr) dumpSys() {
- log.Release("-------------------------------")
- log.Release("savingPotConfig.dumpSys")
- defer func() {
- log.Release("+++++++++++++++++++++++++++++++")
- log.Release("")
- }()
- d, _ := json.Marshal(this.SavingPotCfgs)
- log.Release(string(d))
- return
- }
- func (this *savingPotMgr) dumpUser(param string) {
- log.Release("-------------------------------")
- log.Release("savingPotConfig.dumpUser %s", param)
- defer func() {
- log.Release("+++++++++++++++++++++++++++++++")
- log.Release("")
- }()
- if param == "" {
- this.lock.RLock()
- log.Release(" Total User Count:%d", len(this.userList))
- this.lock.RUnlock()
- return
- }
- var userId int
- var err error
- if userId, err = strconv.Atoi(param); err != nil {
- log.Release("atoi error %v", err)
- return
- }
- this.lock.RLock()
- u, ok := this.userList[userId]
- this.lock.RUnlock()
- if !ok {
- log.Release("User does not exist!")
- return
- }
- u.dump()
- }
|