newmanager.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package signinwheel
  2. import (
  3. "encoding/json"
  4. "math/rand"
  5. "os"
  6. "time"
  7. "bet24.com/log"
  8. item "bet24.com/servers/micros/item_inventory/proto"
  9. platformconfig "bet24.com/servers/micros/platformconfig/proto"
  10. )
  11. const config_key = "signinwheel_config"
  12. const refresh_config_sec = 600
  13. type config_awards struct {
  14. Inner []config_param
  15. Outer []config_param
  16. }
  17. type chance_level struct {
  18. Min int
  19. Max int
  20. Chances []int
  21. pools []int
  22. }
  23. func (cl *chance_level) isIn(gold int) bool {
  24. return gold >= cl.Min && gold <= cl.Max
  25. }
  26. func (cl *chance_level) initPool() {
  27. cl.pools = []int{}
  28. for k := 0; k < len(cl.Chances); k++ {
  29. v := cl.Chances[k]
  30. for i := 0; i < v; i++ {
  31. cl.pools = append(cl.pools, k)
  32. }
  33. }
  34. }
  35. func (cl *chance_level) getIndex() int {
  36. if len(cl.pools) == 0 {
  37. return 0
  38. }
  39. idx := rand.Intn(len(cl.pools))
  40. return cl.pools[idx]
  41. }
  42. type config_chance struct {
  43. Chance int
  44. Levels []chance_level
  45. }
  46. func (cc *config_chance) getIndex(userGold int) int {
  47. levelIndex := len(cc.Levels) - 1
  48. for i := 0; i < len(cc.Levels); i++ {
  49. if cc.Levels[i].isIn(userGold) {
  50. levelIndex = i
  51. break
  52. }
  53. }
  54. return cc.Levels[levelIndex].getIndex()
  55. }
  56. func (cc *config_chance) initPool() {
  57. for i := 0; i < len(cc.Levels); i++ {
  58. cc.Levels[i].initPool()
  59. }
  60. }
  61. type config_chances struct {
  62. Inner []config_chance
  63. Outer []config_chance
  64. innerPool []int
  65. outerPool []int
  66. }
  67. func (cc *config_chances) initPool() {
  68. cc.innerPool = []int{}
  69. cc.outerPool = []int{}
  70. for k := 0; k < len(cc.Inner); k++ {
  71. cc.Inner[k].initPool()
  72. v := cc.Inner[k]
  73. for i := 0; i < v.Chance; i++ {
  74. cc.innerPool = append(cc.innerPool, k)
  75. }
  76. }
  77. for k := 0; k < len(cc.Outer); k++ {
  78. cc.Outer[k].initPool()
  79. v := cc.Outer[k]
  80. for i := 0; i < v.Chance; i++ {
  81. cc.outerPool = append(cc.outerPool, k)
  82. }
  83. }
  84. }
  85. func (cc *config_chances) getInnerIndex(userGold int) int {
  86. // 先随机取一组
  87. if len(cc.innerPool) == 0 {
  88. return 0
  89. }
  90. idx := cc.innerPool[rand.Intn(len(cc.innerPool))]
  91. return cc.Inner[idx].getIndex(userGold)
  92. }
  93. func (cc *config_chances) getOuterIndex(userGold int) int {
  94. // 先随机取一组
  95. if len(cc.outerPool) == 0 {
  96. return 0
  97. }
  98. idx := cc.outerPool[rand.Intn(len(cc.outerPool))]
  99. return cc.Outer[idx].getIndex(userGold)
  100. }
  101. type config_struct struct {
  102. Cost int
  103. MsgLotteryAndGold string // 广播,兑换奖券、金币 (玩家在转盘游戏中赢得了100个金币和200个话费碎片)
  104. MsgLottery string // 广播,兑换奖券(玩家在转盘游戏中赢得了200个话费碎片)
  105. Awards config_awards
  106. Configs config_chances
  107. }
  108. func (cs *config_struct) initPool() {
  109. cs.Configs.initPool()
  110. }
  111. func (cs *config_struct) getItems(userGold int) []item.ItemPack {
  112. var ret []item.ItemPack
  113. // inner
  114. idx := cs.Configs.getInnerIndex(userGold)
  115. if idx >= len(cs.Awards.Inner) {
  116. idx = len(cs.Awards.Inner) - 1
  117. }
  118. if cs.Awards.Inner[idx].Count > 0 {
  119. ret = append(ret, item.ItemPack{ItemId: cs.Awards.Inner[idx].ItemId, Count: cs.Awards.Inner[idx].Count})
  120. }
  121. // outer
  122. idx = cs.Configs.getOuterIndex(userGold)
  123. if idx >= len(cs.Awards.Outer) {
  124. idx = len(cs.Awards.Outer) - 1
  125. }
  126. if cs.Awards.Outer[idx].Count > 0 {
  127. ret = append(ret, item.ItemPack{ItemId: cs.Awards.Outer[idx].ItemId, Count: cs.Awards.Outer[idx].Count})
  128. }
  129. return ret
  130. }
  131. type newmanager struct {
  132. configInfo config_struct
  133. }
  134. func newNewManager() *newmanager {
  135. ret := new(newmanager)
  136. ret.loadConfig()
  137. return ret
  138. }
  139. func (nm *newmanager) isAvailable() bool {
  140. return nm.configInfo.Cost > 0
  141. }
  142. func (nm *newmanager) getCost() int {
  143. return nm.configInfo.Cost
  144. }
  145. func (nm *newmanager) loadConfig() {
  146. defer func() {
  147. time.AfterFunc(refresh_config_sec*time.Second, nm.loadConfig)
  148. }()
  149. configString := platformconfig.GetConfig(config_key)
  150. if configString == "" {
  151. data, err := os.ReadFile("fishconf/signinwheel.json")
  152. if err != nil {
  153. log.Release("signinwheel.newmanager.loadData read signinwheel failed")
  154. return
  155. }
  156. configString = string(data)
  157. platformconfig.SetConfig(config_key, configString)
  158. } else {
  159. log.Debug("signinwheel.newmanager loading config from redis")
  160. }
  161. err := json.Unmarshal([]byte(configString), &nm.configInfo)
  162. if err != nil {
  163. log.Release("signinwheel.newmanager.loadData Unmarshal signinwheel failed err:%v", err)
  164. return
  165. }
  166. nm.configInfo.initPool()
  167. }
  168. func (nm *newmanager) getConfig() SigninWheelConfig {
  169. return SigninWheelConfig{Cost: nm.configInfo.Cost, Multiple: nm.configInfo.Awards.Inner, Base: nm.configInfo.Awards.Outer}
  170. }
  171. func (nm *newmanager) getResult(userGold int) (int, int, []item.ItemPack) {
  172. base, mul := 1, 1
  173. items := nm.configInfo.getItems(userGold)
  174. return mul, base, items
  175. }
  176. // base: outer
  177. // multiple: inner
  178. func (nm *newmanager) getGoldByLevel(level int) int {
  179. if level < 0 {
  180. level = 0
  181. }
  182. maxLevel := len(nm.configInfo.Configs.Inner[0].Levels) - 1
  183. if level > maxLevel {
  184. level = maxLevel
  185. }
  186. return nm.configInfo.Configs.Inner[0].Levels[level].Min + 1
  187. }
  188. func (nm *newmanager) getMaxLevel() int {
  189. return len(nm.configInfo.Configs.Inner[0].Levels)
  190. }