roomconfig.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/rand"
  6. "os"
  7. "bet24.com/log"
  8. coreservice "bet24.com/servers/coreservice/client"
  9. )
  10. const config_key = "spinplay_rooms"
  11. const config_key_private = "private_spinplay_rooms"
  12. type RobotConfig struct {
  13. MinRobotGold int
  14. MaxRobotGold int
  15. RobotOnlineSec int
  16. RobotCount int
  17. MinRoom int
  18. MinActionSec int
  19. MaxActionSec int
  20. }
  21. type PrivateRoomConfig struct {
  22. TargetOptions []int // 初始携带分选项
  23. UserCountOptions []int // 玩家人数选项
  24. EnrollFeeOptions []int
  25. }
  26. type JackPot struct {
  27. Odd int // 倍数
  28. Probility int // 概率
  29. ControlProbility int // 控制概率
  30. }
  31. type RoomInfoParam struct {
  32. JackPots []JackPot // 奖池配置数据
  33. InitScore int // 初始携带分
  34. WinScore int // 游戏结束时赢牌玩家最小分数
  35. MinRobBankerScore int // 最小抢庄分
  36. MinRobBankerDifferScore int // 抢庄最小间隔
  37. MinBankerGiveScore int // 庄家最小给分
  38. BetLevel int // 投注挡位
  39. }
  40. type RoomInfoSec struct {
  41. SecFree int
  42. SecJackPot int
  43. SecSendCard int
  44. SecRobBanker int
  45. SecConfirmBanker int
  46. SecAction int
  47. SecCompareCard int
  48. SecRoundEnd int
  49. SecEnd int
  50. }
  51. type RoomInfoBase struct {
  52. RoomID int
  53. RoomName string
  54. RoomDesc string
  55. MinGold int
  56. MaxGold int
  57. BaseScore int
  58. ServerIP string
  59. ServerPort int
  60. LockLevel int
  61. LevelParam int
  62. RoomInfoSec
  63. RoomInfoParam
  64. PrivateRoomConfig
  65. }
  66. type RoomInfo struct {
  67. RoomInfoBase
  68. RobotConfig
  69. }
  70. func (ri *RoomInfo) GetJackpotAmount(poolValue int) int {
  71. total := 0
  72. max := 0
  73. l := len(ri.JackPots)
  74. if l > 0 {
  75. max = ri.JackPots[l-1].Odd
  76. }
  77. if poolValue < max {
  78. for i := 0; i < len(ri.JackPots); i++ {
  79. total += ri.JackPots[i].ControlProbility
  80. }
  81. r := rand.Intn(total)
  82. start := 0
  83. for i := 0; i < len(ri.JackPots); i++ {
  84. start += ri.JackPots[i].ControlProbility
  85. if r < start {
  86. return ri.JackPots[i].Odd
  87. }
  88. }
  89. return ri.JackPots[0].Odd
  90. }
  91. for i := 0; i < len(ri.JackPots); i++ {
  92. total += ri.JackPots[i].Probility
  93. }
  94. r := rand.Intn(total)
  95. start := 0
  96. for i := 0; i < len(ri.JackPots); i++ {
  97. start += ri.JackPots[i].Probility
  98. if r < start {
  99. return ri.JackPots[i].Odd
  100. }
  101. }
  102. return ri.JackPots[0].Odd
  103. }
  104. var Rooms struct {
  105. Rooms []RoomInfo
  106. }
  107. func getConfigKey() string {
  108. if Server.IsPrivateRoom > 0 {
  109. return config_key_private
  110. }
  111. return config_key
  112. }
  113. func loadRedisConfig() bool {
  114. data := coreservice.GetPlatformConfig(getConfigKey())
  115. if data == "" {
  116. log.Release(" config msg is null")
  117. return false
  118. }
  119. return marshalData([]byte(data))
  120. }
  121. func loadRoomConfig() {
  122. if loadRedisConfig() {
  123. log.Release("using remote config ignored rooms.json !!!")
  124. return
  125. }
  126. // 加载房间表
  127. data, err := os.ReadFile("spinplay/rooms.json")
  128. //fmt.Println(string(data))
  129. if err != nil {
  130. log.Release("read rooms failed spinplay/rooms.json")
  131. return
  132. }
  133. if marshalData(data) {
  134. // 写入redis
  135. coreservice.SetPlatformConfig(getConfigKey(), string(data))
  136. }
  137. }
  138. func marshalData(data []byte) bool {
  139. err := json.Unmarshal(data, &Rooms)
  140. if err != nil {
  141. log.Release("Unmarshal rooms failed err:%v", err)
  142. return false
  143. }
  144. if len(Rooms.Rooms) <= 0 {
  145. log.Release("Rooms.Rooms == 0")
  146. return false
  147. }
  148. if len(Server.ServerIP) > 0 {
  149. ws := "ws"
  150. if len(Server.CertFile) > 0 {
  151. ws = "wss"
  152. }
  153. for i := 0; i < len(Rooms.Rooms); i++ {
  154. Rooms.Rooms[i].ServerIP = fmt.Sprintf("%s://%s:%d", ws, Server.ServerIP, Server.ServerPort)
  155. }
  156. }
  157. return true
  158. }
  159. func OnConfigChanged(key string) {
  160. if key != config_key {
  161. return
  162. }
  163. log.Release("正在刷新配置信息")
  164. loadRedisConfig()
  165. }