prizepool.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package prizepool
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/rand"
  6. "os"
  7. "sync"
  8. "time"
  9. "bet24.com/log"
  10. )
  11. type PrizePool_config struct {
  12. MinBulletId int // 最低子弹ID
  13. AddPoolPercent int // 击中鱼后进入奖池的百分比
  14. AddEnergyPercent int // 击中Boss鱼如果没中奖池,子弹倍数万分比 增加到能量
  15. BingoPercent int // 中奖概率
  16. FullEnergy int // 能量上限
  17. PoolInit int // 初始奖池金额
  18. MaxPool int // 奖池封顶
  19. }
  20. type PrizePool struct {
  21. PrizePool_config
  22. roomName string
  23. pool int
  24. lock *sync.RWMutex
  25. userLock *sync.RWMutex
  26. lastUpdatePool int
  27. userEnergy map[int]int
  28. }
  29. func NewPrizePool(roomName string, roomMinBulletId int) *PrizePool {
  30. fileName := fmt.Sprintf("fish/prizepool_%v.json", roomName)
  31. data, err := os.ReadFile(fileName)
  32. if err != nil {
  33. log.Release("read prizepool config failed %v", fileName)
  34. return nil
  35. }
  36. var p PrizePool
  37. err = json.Unmarshal(data, &p)
  38. if err != nil {
  39. log.Release("Unmarshal prizepool config failed err:%v", err)
  40. return nil
  41. }
  42. if roomMinBulletId < p.MinBulletId {
  43. return nil
  44. }
  45. log.Debug("NewPrizePool %v", p)
  46. p.lock = &sync.RWMutex{}
  47. p.userLock = &sync.RWMutex{}
  48. p.roomName = roomName
  49. p.userEnergy = make(map[int]int)
  50. p.loadPool()
  51. ticket := time.NewTicker(time.Duration(5) * time.Second)
  52. go func(t *time.Ticker) {
  53. for { //循环
  54. select {
  55. case <-t.C:
  56. p.checkUpdatePool()
  57. }
  58. }
  59. }(ticket)
  60. return &p
  61. }
  62. func (p *PrizePool) loadPool() {
  63. // 从数据库读取奖池信息
  64. // TODO
  65. pool := getInfo(p.roomName)
  66. if pool < p.PoolInit {
  67. pool = p.PoolInit
  68. }
  69. p.lock.Lock()
  70. defer p.lock.Unlock()
  71. p.pool = pool
  72. }
  73. func (p *PrizePool) updatePool(pool int) {
  74. // 写入数据库
  75. // TODO
  76. update(p.roomName, p.pool)
  77. }
  78. func (p *PrizePool) checkUpdatePool() {
  79. p.lock.RLock()
  80. defer p.lock.RUnlock()
  81. if p.lastUpdatePool == p.pool {
  82. return
  83. }
  84. p.lastUpdatePool = p.pool
  85. go p.updatePool(p.pool)
  86. }
  87. func (p *PrizePool) AddPool(bulletOdds int) {
  88. p.lock.Lock()
  89. defer p.lock.Unlock()
  90. p.pool += bulletOdds / 100 * p.AddPoolPercent
  91. }
  92. func (p *PrizePool) Draw(userId int, bulletOdds int) (bool, int, int) {
  93. bingo := (rand.Intn(100) < p.BingoPercent)
  94. point := bulletOdds / 10000
  95. if point+p.GetUserEnergy(userId) >= 10000 {
  96. bingo = true
  97. }
  98. if bingo {
  99. p.lock.Lock()
  100. point = bulletOdds * p.pool / 1000000
  101. p.pool -= point
  102. if p.pool < p.PoolInit {
  103. p.pool = p.PoolInit
  104. }
  105. p.lock.Unlock()
  106. p.setEnergy(userId, 0)
  107. } else {
  108. p.addEnergy(userId, point)
  109. }
  110. log.Debug("PrizePool.Draw %d,%v,%d", userId, bingo, point)
  111. return bingo, point, p.pool
  112. }
  113. func (p *PrizePool) GetPoolValue() int {
  114. p.lock.RLock()
  115. defer p.lock.RUnlock()
  116. return p.pool
  117. }
  118. func (p *PrizePool) GetUserEnergy(userId int) int {
  119. p.userLock.RLock()
  120. ret, ok := p.userEnergy[userId]
  121. p.userLock.RUnlock()
  122. if ok {
  123. return ret
  124. }
  125. ret = getUserEnergy(userId, p.roomName)
  126. p.userLock.Lock()
  127. p.userEnergy[userId] = ret
  128. p.userLock.Unlock()
  129. return ret
  130. }
  131. func (p *PrizePool) setEnergy(userId, energy int) {
  132. if energy < 0 {
  133. energy = 0
  134. }
  135. p.userLock.Lock()
  136. p.userEnergy[userId] = energy
  137. p.userLock.Unlock()
  138. go setUserEnergy(userId, p.roomName, energy)
  139. }
  140. func (p *PrizePool) addEnergy(userId, energy int) {
  141. energy += p.GetUserEnergy(userId)
  142. if energy < 0 {
  143. energy = 0
  144. }
  145. p.userLock.Lock()
  146. p.userEnergy[userId] = energy
  147. p.userLock.Unlock()
  148. go setUserEnergy(userId, p.roomName, energy)
  149. }