manager.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package savingpot
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "sync"
  8. "time"
  9. "bet24.com/log"
  10. pb "bet24.com/servers/micros/activityservice/proto"
  11. platformconfig "bet24.com/servers/micros/platformconfig/proto"
  12. )
  13. const config_key = "activity_savingpot"
  14. func getManager() *savingPotMgr {
  15. if mgr == nil {
  16. mgr = new(savingPotMgr)
  17. mgr.ctor()
  18. }
  19. return mgr
  20. }
  21. var mgr *savingPotMgr
  22. type SavingPotCfgs struct {
  23. MaxLevel int
  24. MaxCount int
  25. Confs []pb.SavingPotCfg
  26. }
  27. type savingPotMgr struct {
  28. configString string
  29. userList map[int]*userSavingPot
  30. lock *sync.RWMutex
  31. SavingPotCfgs
  32. }
  33. func (this *savingPotMgr) ctor() {
  34. this.lock = &sync.RWMutex{}
  35. this.userList = make(map[int]*userSavingPot)
  36. this.readConf()
  37. //this.checkExpiredUsers()
  38. }
  39. func (this *savingPotMgr) readConf() {
  40. time.AfterFunc(time.Second*600, this.readConf)
  41. configString := platformconfig.GetConfig(config_key)
  42. //if configString == "" {
  43. data, err := os.ReadFile("serviceconf/activity_savingPot.json")
  44. if err != nil {
  45. log.Release("savingPot.readConf read config failed")
  46. return
  47. }
  48. configString = string(data)
  49. if configString != "" {
  50. platformconfig.SetConfig(config_key, configString)
  51. }
  52. //}
  53. if configString == this.configString {
  54. return
  55. }
  56. this.configString = configString
  57. // 读取配置
  58. err = json.Unmarshal([]byte(configString), &this.SavingPotCfgs)
  59. if err != nil {
  60. log.Release("savingPot.readConf Unmarshal failed %s", configString)
  61. }
  62. }
  63. func (this *savingPotMgr) checkExpiredUsers() {
  64. time.AfterFunc(time.Second*600, this.checkExpiredUsers)
  65. var toRemove []int
  66. this.lock.RLock()
  67. for k, v := range this.userList {
  68. if v.isExpire() {
  69. toRemove = append(toRemove, k)
  70. }
  71. }
  72. this.lock.RUnlock()
  73. if len(toRemove) == 0 {
  74. return
  75. }
  76. log.Debug("savingPot.checkExpiredUsers removing %v", toRemove)
  77. this.lock.Lock()
  78. for _, v := range toRemove {
  79. delete(this.userList, v)
  80. }
  81. this.lock.Unlock()
  82. }
  83. // 获取配置信息
  84. func (this *savingPotMgr) getConfigInfo(level int) pb.SavingPotCfg {
  85. if level > this.SavingPotCfgs.MaxLevel {
  86. log.Debug("error, MaxLevel:%d, level:%d", this.SavingPotCfgs.MaxLevel, level)
  87. fmt.Printf("SavingPotCfgs====", this.SavingPotCfgs.MaxLevel, level)
  88. return this.SavingPotCfgs.Confs[this.SavingPotCfgs.MaxLevel-1]
  89. }
  90. for i := 0; i < len(this.SavingPotCfgs.Confs); i++ {
  91. if this.SavingPotCfgs.Confs[i].Level == level {
  92. return this.SavingPotCfgs.Confs[i]
  93. }
  94. }
  95. return this.SavingPotCfgs.Confs[0]
  96. }
  97. // 获取用户
  98. func (this *savingPotMgr) getUser(userId int) *userSavingPot {
  99. this.lock.RLock()
  100. u, ok := this.userList[userId]
  101. this.lock.RUnlock()
  102. if ok {
  103. u.updateTimeStamp()
  104. return u
  105. }
  106. u = newUserSavingPot(userId)
  107. u.accumulateLimit = this.getConfigInfo(u.CurrentLevel).AccumulateLimit
  108. this.lock.Lock()
  109. this.userList[userId] = u
  110. this.lock.Unlock()
  111. return u
  112. }
  113. // 获取用户信息
  114. func (this *savingPotMgr) getUserSavingPot(userId int) pb.SavingPotInfo {
  115. u := this.getUser(userId)
  116. if u == nil {
  117. log.Error("savingPot.getUserSavingPot userId[%d] is not exist", userId)
  118. return pb.SavingPotInfo{}
  119. }
  120. return u.getInfo()
  121. }
  122. func (this *savingPotMgr) getUserBuyCount(userId int) int {
  123. u := this.getUser(userId)
  124. if u == nil {
  125. log.Error("savingPot.getUserSavingPot userId[%d] is not exist", userId)
  126. return this.MaxCount
  127. }
  128. return u.buyCount
  129. }
  130. // 获取存钱罐金币
  131. func (this *savingPotMgr) getBuyAmount(userId int, isOld bool) int {
  132. u := this.getUser(userId)
  133. if u == nil {
  134. log.Error("savingPot.getBuyAmount userId[%d] is not exist", userId)
  135. return 0
  136. }
  137. if isOld {
  138. return u.buyAmount
  139. }
  140. // 用户信息
  141. info := u.getInfo()
  142. amount := info.BuyAmount
  143. if info.IsMultiplyStatus != 0 {
  144. amount = info.BuyAmount * 3
  145. }
  146. cfg := this.getConfigInfo(info.CurrentLevel)
  147. if amount > cfg.MultiplyLimit {
  148. amount = cfg.MultiplyLimit
  149. }
  150. return amount
  151. }
  152. // 存钱罐购买
  153. func (this *savingPotMgr) buy(userId int, isOld bool) bool {
  154. u := this.getUser(userId)
  155. if u == nil {
  156. log.Error("savingPot.buy userId[%d] is not exist", userId)
  157. return false
  158. }
  159. return u.buy(this.MaxLevel, this.MaxCount, this.Confs, isOld)
  160. }
  161. // 添加累计金币
  162. func (this *savingPotMgr) addAccumulateAmount(userId, gameId, winAmount int) {
  163. u := this.getUser(userId)
  164. if u == nil {
  165. log.Error("savingPot.addAccumulateAmount userId[%d] is not exist", userId)
  166. return
  167. }
  168. u.addAccumulateAmount(gameId, winAmount, this.getConfigInfo(u.CurrentLevel))
  169. return
  170. }
  171. func (this *savingPotMgr) checkUserMultiplyReceive(userId int) {
  172. u := this.getUser(userId)
  173. if u == nil {
  174. log.Error("savingPot.addAccumulateAmount userId[%d] is not exist", userId)
  175. return
  176. }
  177. cfg := this.getConfigInfo(u.CurrentLevel)
  178. u.checkMultiplyReceive(cfg)
  179. }
  180. func (this *savingPotMgr) setIsOldSavingPot(userId int) {
  181. u := this.getUser(userId)
  182. if u != nil {
  183. u.isOld = true
  184. }
  185. }
  186. func (this *savingPotMgr) dumpSys() {
  187. log.Release("-------------------------------")
  188. log.Release("savingPotConfig.dumpSys")
  189. defer func() {
  190. log.Release("+++++++++++++++++++++++++++++++")
  191. log.Release("")
  192. }()
  193. d, _ := json.Marshal(this.SavingPotCfgs)
  194. log.Release(string(d))
  195. return
  196. }
  197. func (this *savingPotMgr) dumpUser(param string) {
  198. log.Release("-------------------------------")
  199. log.Release("savingPotConfig.dumpUser %s", param)
  200. defer func() {
  201. log.Release("+++++++++++++++++++++++++++++++")
  202. log.Release("")
  203. }()
  204. if param == "" {
  205. this.lock.RLock()
  206. log.Release(" Total User Count:%d", len(this.userList))
  207. this.lock.RUnlock()
  208. return
  209. }
  210. var userId int
  211. var err error
  212. if userId, err = strconv.Atoi(param); err != nil {
  213. log.Release("atoi error %v", err)
  214. return
  215. }
  216. this.lock.RLock()
  217. u, ok := this.userList[userId]
  218. this.lock.RUnlock()
  219. if !ok {
  220. log.Release("User does not exist!")
  221. return
  222. }
  223. u.dump()
  224. }