poolinfo.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package bacwaterpool
  2. import (
  3. "context"
  4. "math/rand"
  5. "sort"
  6. "time"
  7. "bet24.com/log"
  8. pb "bet24.com/servers/micros/waterpool/proto"
  9. )
  10. var control_type = []string{"normal", "lose", "win"}
  11. func getControlType(t int) string {
  12. if t < 0 || t >= len(control_type) {
  13. t = 0
  14. }
  15. return control_type[t]
  16. }
  17. func newPoolInfo(config poolConfig) *poolInfo {
  18. ret := new(poolInfo)
  19. ret.poolConfig = config
  20. ret.PoolValue = config.InitPool
  21. ret.sortLevels()
  22. go ret.updateValue()
  23. return ret
  24. }
  25. type poolLevel struct {
  26. Value int
  27. Chance int // 调控百分比
  28. Type int // 调控模式
  29. }
  30. type poolConfig struct {
  31. PoolId int
  32. ReturnRate int // 返奖率百分比
  33. InitPool int // 初始金额
  34. UserMin int // 玩家最小 0 不限
  35. UserMax int // 玩家最大 0 不限
  36. IsSlots bool // 是否针对slots游戏
  37. GameId int // 游戏ID
  38. PoolName string // 奖池名字
  39. PoolType int // 奖池对应的房间类型
  40. Levels []poolLevel
  41. }
  42. type poolInfo struct {
  43. poolConfig
  44. PoolValue int
  45. lastValue int
  46. }
  47. func (p *poolInfo) dump() {
  48. t, c := p.getEffectiveLevel()
  49. //log.Release("poolInfo.dump,getEffectiveLevel = %d,%d", t, c)
  50. log.Release(" Pool[%d] Value[%d] MinMax[%d-%d] [%d%% of %s]", p.PoolId, p.PoolValue, p.UserMin, p.UserMax, c, getControlType(t))
  51. for i := 0; i < len(p.Levels); i++ {
  52. log.Release(" [%d][%d%% of %s]", p.Levels[i].Value, p.Levels[i].Chance, getControlType(p.Levels[i].Type))
  53. }
  54. }
  55. func (p *poolInfo) updateValue() {
  56. for {
  57. c, cancel := context.WithTimeout(context.Background(), sync_sec*time.Second)
  58. select {
  59. case <-c.Done():
  60. if p.PoolValue != p.lastValue {
  61. p.lastValue = p.PoolValue
  62. trans_updateWaterPoolList(p.GameId, p.PoolName, p.PoolType, p.PoolValue)
  63. }
  64. cancel()
  65. }
  66. }
  67. }
  68. func (p *poolInfo) sortLevels() {
  69. sort.Slice(p.Levels, func(i, j int) bool {
  70. return p.Levels[i].Value > p.Levels[j].Value
  71. })
  72. }
  73. func (p *poolInfo) addBet(betAmount int, userGold int, isSlots bool, gameId int, poolName string) bool {
  74. if !p.isConcerned(userGold, isSlots, gameId) || (poolName != "" && poolName == p.PoolName) {
  75. return false
  76. }
  77. reduceValue := (betAmount / 100 * p.ReturnRate)
  78. sysRecover := betAmount - reduceValue
  79. p.PoolValue += reduceValue
  80. go trans_waterPoolChangeRecord(gameId, p.PoolName, reduceValue, p.PoolType, sysRecover)
  81. return true
  82. }
  83. func (p *poolInfo) reducePool(value int, userGold int, isSlots bool, gameId int, poolName string) bool {
  84. if !p.isConcerned(userGold, isSlots, gameId) || (poolName != "" && poolName == p.PoolName) {
  85. return false
  86. }
  87. p.PoolValue -= value
  88. go trans_waterPoolChangeRecord(gameId, p.PoolName, -value, p.PoolType, 0)
  89. return true
  90. }
  91. func (p *poolInfo) isConcerned(gold int, isSlots bool, gameId int) bool {
  92. if isSlots != p.IsSlots {
  93. return false
  94. }
  95. if p.UserMin != 0 && gold < p.UserMin {
  96. return false
  97. }
  98. if p.UserMax != 0 && gold > p.UserMax {
  99. return false
  100. }
  101. if gameId != 0 && p.GameId != 0 && gameId != p.GameId {
  102. return false
  103. }
  104. return true
  105. }
  106. func (p *poolInfo) getControlType(userGold int, isSlots bool, gameId int) int {
  107. if !p.isConcerned(userGold, isSlots, gameId) {
  108. return pb.PoolControl_Invalid
  109. }
  110. r := rand.Intn(100)
  111. t, c := p.getEffectiveLevel()
  112. if r < c {
  113. return t
  114. }
  115. return pb.PoolControl_Normal
  116. }
  117. func (p *poolInfo) getControlProb(userGold int, isSlots bool, gameId int, poolName string) (int, int) {
  118. if !p.isConcerned(userGold, isSlots, gameId) {
  119. return 0, 0
  120. }
  121. t, value := p.getEffectiveLevel()
  122. value = value * 100
  123. if t == pb.PoolControl_Lose {
  124. value = -value
  125. }
  126. return t, value
  127. }
  128. func (p *poolInfo) getEffectiveLevel() (int, int) {
  129. // 先看放水的
  130. for i := 0; i < len(p.Levels); i++ {
  131. if p.PoolValue >= p.Levels[i].Value && p.Levels[i].Type == pb.PoolControl_Win {
  132. //log.Release("poolInfo.getEffectiveLevel poolValue = %d", p.PoolValue)
  133. //log.Release(" [%d][%d%% of %s]", p.Levels[i].Value, p.Levels[i].Chance, getControlType(p.Levels[i].Type))
  134. return pb.PoolControl_Win, p.Levels[i].Chance
  135. }
  136. }
  137. for i := len(p.Levels) - 1; i >= 0; i-- {
  138. if p.PoolValue <= p.Levels[i].Value && p.Levels[i].Type == pb.PoolControl_Lose {
  139. //log.Release("poolInfo.getEffectiveLevel2 poolValue = %d", p.PoolValue)
  140. //log.Release(" [%d][%d%% of %s]", p.Levels[i].Value, p.Levels[i].Chance, getControlType(p.Levels[i].Type))
  141. return pb.PoolControl_Lose, p.Levels[i].Chance
  142. }
  143. }
  144. return pb.PoolControl_Normal, 0
  145. }