odds.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package common
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "bet24.com/log"
  6. )
  7. /*
  8. 大苹果 大西瓜 大葡萄 大香蕉 大橘子 1:50 1:20 1:10 1:8 1:5
  9. 苹果 西瓜 葡萄 香蕉 橘子 1:2 1:2 1:2 1:2 1:2
  10. BigApple 50
  11. BigWatermelon 20
  12. BigGrape 10
  13. BigBanana 8
  14. BigOrange 5
  15. Apple 2
  16. Watermelon 2
  17. Grape 2
  18. Banana 2
  19. Orange 2
  20. */
  21. type Fruit int
  22. const (
  23. Orange Fruit = iota
  24. Banana
  25. Grape
  26. Watermelon
  27. Apple
  28. BigOrange
  29. BigBanana
  30. BigGrape
  31. BigWatermelon
  32. BigApple
  33. BidTypeMax
  34. )
  35. var fruitNames = []string{
  36. "橙子",
  37. "香蕉",
  38. "葡萄",
  39. "西瓜",
  40. "苹果",
  41. "大橙子",
  42. "大香蕉",
  43. "大葡萄",
  44. "大西瓜",
  45. "大苹果",
  46. }
  47. var fruitProbability = []int{
  48. 1183, // Orange
  49. 1183, // Banana
  50. 1183, // Grape
  51. 1183, // Watermelon
  52. 1183, // Apple
  53. 1651, // BigOrange
  54. 1031, // BigBanana
  55. 825, // BigGrape
  56. 413, // BigWatermelon
  57. 165, // BigApple
  58. }
  59. var fruitWinningOdds = map[Fruit]float64{
  60. Orange: 2,
  61. Banana: 2,
  62. Grape: 2,
  63. Watermelon: 2,
  64. Apple: 2,
  65. BigOrange: 5,
  66. BigBanana: 8,
  67. BigGrape: 10,
  68. BigWatermelon: 20,
  69. BigApple: 50,
  70. }
  71. func Spin() Fruit {
  72. // 计算水果的总概率
  73. var total float64
  74. for _, p := range fruitProbability {
  75. total += float64(p) / 10
  76. }
  77. // 生成一个随机数
  78. r := rand.Float64() * total
  79. // 根据随机数返回中奖结果
  80. var sum float64
  81. for i, p := range fruitProbability {
  82. sum += float64(p) / 10
  83. if r < sum {
  84. return Fruit(i)
  85. }
  86. }
  87. // 如果程序执行到这里,说明出现了意外情况,返回默认值
  88. return Orange
  89. }
  90. func convertBig(fruit Fruit) Fruit {
  91. switch fruit {
  92. case BigOrange:
  93. return Orange
  94. case BigBanana:
  95. return Banana
  96. case BigGrape:
  97. return Grape
  98. case BigWatermelon:
  99. return Watermelon
  100. case BigApple:
  101. return Apple
  102. default:
  103. return fruit
  104. }
  105. }
  106. func GetWinArea(spinResult Fruit) (result Fruit) {
  107. result = convertBig(spinResult)
  108. return
  109. }
  110. func GetOdds(spinResult Fruit) (odds float64) {
  111. odds = fruitWinningOdds[spinResult]
  112. return
  113. }
  114. func GetResultOdds(betFruit int, spinResult Fruit) (odds float64) {
  115. result := GetWinArea(spinResult)
  116. odds = 0
  117. if int(result) == betFruit {
  118. odds = GetOdds(spinResult)
  119. }
  120. return
  121. }
  122. // 刷新获胜赔率
  123. func ResetWinningOdds(winningOdds []float64) {
  124. for i, o := range winningOdds {
  125. f := Fruit(i)
  126. if f < Orange || f > BigApple {
  127. continue // Invalid fruit index, skip
  128. }
  129. if fruitWinningOdds[f] != o {
  130. fruitWinningOdds[f] = o
  131. }
  132. }
  133. }
  134. // 刷新水果概率
  135. func ResetFruitProbability(probability []int) {
  136. for i, p := range probability {
  137. f := Fruit(i)
  138. if f < Orange || f > BigApple {
  139. continue // Invalid fruit index, skip
  140. }
  141. if fruitProbability[i] != p {
  142. fruitProbability[i] = p
  143. }
  144. }
  145. }
  146. func GetBetDesc(betId int) string {
  147. max := int(Apple) + 1
  148. if betId >= max || betId < 0 {
  149. log.Release("common.GetDesc failed %d,%d", betId, max)
  150. return "invalid bet"
  151. }
  152. return fruitNames[betId]
  153. }
  154. func GetResultDesc(spinResult Fruit) string {
  155. return fmt.Sprintf("[%s:%s]", fruitNames[spinResult], fruitNames[GetWinArea(spinResult)])
  156. }